How can i read an image with Pyplot from memory instead of a file?

Question:

This piece of code is working, but I want to avoid the use of the temporal file, i have tried differents ways but not working. Does anyone knows how to do it? or the temp file is mandatory?

from PIL import Image
import matplotlib.pyplot as plt
import numpy as np

...
data = Image.fromarray(np.array(image))
data.save('output/temp.png')
img = plt.imread('output/temp.png')
...

the complete function:

            data = pickle.load(datafile)
            # IMAGES
            image = data['img']
            # LABELS
            label = data['label']
            # SHOW
            data = Image.fromarray(np.array(image))
            data.save('output/temp.png')
            img = plt.imread('output/temp.png')

            # Create a figure. Equal aspect so circles look circular
            fig, ax = plt.subplots(1)
            ax.set_aspect('equal')

            # Show the image
            ax.imshow(img)

            # Now, loop through coord arrays, and create a circle at each x,y pair
            for xx, yy in label:
                circle = plt.Circle((xx, yy), 10)
                ax.add_patch(circle)

            # Show the image
            plt.show()

Because I want to draw circles in an image loaded with numpy:
Drawing circles on image with Matplotlib and NumPy

But I just want to know how to avoid the use of the temporal file. Is it possible?

Asked By: cppsvx

||

Answers:

you asked the wrong question
I think what you mean is how to show it. because you read it to import it from file to memory. so you can’t read it from memory because it’s already there.
and for that, you just need to use plt.imshow(data, *args)

Answered By: No.BoD

According to Matplotlib’s documentation on imread, it functions similarly to Image.open.

This means that you should already be able to pass data into what you need instead of img since they are both Image object.

Also, you should explained what you want to do more if possible. The current code is too vague, so this is all I could give.

Answered By: Opposite34