Skip to content Skip to sidebar Skip to footer

Corrupted Image Is Being Saved With Pil

I am having an issue whereby manipulating image pixels is causing a corrupted image to be saved... So, I open an image using PIL, and then convert it to a NumPy array: image = Imag

Solution 1:

The default data type created by numpy.zeros is floating point, so

images = np.zeros([10, 3, 50, 50])

creates a floating point array. Then in the assignment

images[0] = pixels

the values in pixels are cast to floating point in order to store them in images, so image3 is a floating point array. This affects the values that are stored in the PNG file when the corresponding image is saved. I don't know the rules that PIL/Pillow follow when given a floating point array, but apparently it is not the desired behavior here.

To fix this, create images using the same data type as np_image (most likely this is numpy.uint8):

images = np.zeros([10, 3, 50, 50], dtype=np_image.dtype)

Post a Comment for "Corrupted Image Is Being Saved With Pil"