0

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?

cppsvx
  • 123
  • 2
  • 10

2 Answers2

1

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)

No.BoD
  • 151
  • 9
  • I know how to show it, what I need is to pass the image from np.array to plt. I will complete the piece of code – cppsvx Jan 12 '23 at 09:28
  • if you look closely in plt.imshow(**data**, *args). I passed the data you used for storing image – No.BoD Jan 12 '23 at 09:33
  • the `plt.imread()` method also returns a `ndarray` as you have in `data` – No.BoD Jan 12 '23 at 09:35
  • Right, that is the key. I can use "data" directly without the use of plt.imread. Thanks! – cppsvx Jan 12 '23 at 09:38
1

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.

Ping34
  • 206
  • 1
  • 10