I am trying to save an image with negative values using PIL, however, after saving, the image files has all negative values clipped to 0.
from PIL import Image
import numpy as np
# generate random image for demo
img_arr = np.random.random_integers(-1000,1000, size=[10,10]).astype(np.int32)
print "original min {}, max: {}".format(img_arr.min(),img_arr.max())
# create PIL image
img1 = Image.fromarray(img_arr)
print "PIL min {}, max: {}".format(np.array(img1.getdata()).min(),np.array(img1.getdata()).max())
# save image
img1.save("test_file.png", "PNG")
# reload image
img_file = Image.open("test_file.png")
print "img_file min {}, max: {}".format(np.array(img_file.getdata()).min(),np.array(img_file.getdata()).max())
This results in output:
original min -983, max: 965
PIL min -983, max: 965
img_file min 0, max: 965
How can I save this image and maintain the negative values?