1

I am using imageio to write png images to file.

import numpy as np
import matplotlib.cm as cm
import imageio # for saving the image
import matplotlib as mpl

hm_colors = ['blue', 'white','red']
cmap = mpl.colors.LinearSegmentedColormap.from_list('bwr', hm_colors)
data = np.array([[1,2,3],[5,6,7]])
norm = mpl.colors.Normalize(vmin=-3, vmax=3)
colormap = cm.ScalarMappable(norm=norm, cmap=cmap)
im = colormap.to_rgba(data)
# scale the data to a width of w pixels
im = np.repeat(im, w, axis=1)
im = np.repeat(im, h, axis=0)
# save the picture
imageio.imwrite("my_img.png", im)

This process is performed automatically and I noticed some Error messages saying:

Error closing: 'Image' object has no attribute 'fp'.

Before this message I get warning:

/usr/local/lib/python2.7/dist-packages/imageio/core/util.py:78: UserWarning: Lossy conversion from float64 to uint8, range [0, 1] dtype_str, out_type.__name__))

However, the images seem to be generated and saved just fine. I can't find data to recreate this message.

Any idea why I get this error and why it doesn't noticeably affect the results? I don't use PIL. One possible reason could come from using this in Celery.

Thanks! L.

Liis Kolberg
  • 153
  • 12

1 Answers1

0

I encountered the same issue using imageio.imwrite in Python 3.5. It's a fairly harmless except for the fact that that it's stopping garbage collection and leading to excessive memory usage when writing thousands of images. The solution was to use the PIL module, which is a dependency of imageio. The last line of your code should read:

from PIL import Image
image = Image.fromarray(im)
image.save('my_img.png')
Alex Kaszynski
  • 1,817
  • 2
  • 17
  • 17