I need to make a copy of an image to manipulate it, however saving the original image and opening up the copied one seems to differ in their pixel values:
from PIL import Image
# Open original image
img = Image.open("mountain.jpg")
data = img.load()
# Display individual pixels
print("Pixel 1: {}".format(data[0,0]))
print("Pixel 2: {}".format(data[0,1]))
print("Pixel 3: {}".format(data[0,2]))
# Makes a copy of the input image and loads the copied image's pixel map
copyImage = img.copy()
copyImage.save('copy.jpg')
copyImage.close()
# Opens the copied image that was saved earlier and its pixel map
copy = Image.open("copy.jpg")
copy_data = copy.load()
print()
# Display copied images' individual pixels
print("Pixel 1 (copy): {}".format(copy_data[0,0]))
print("Pixel 2 (copy): {}".format(copy_data[0,1]))
print("Pixel 3 (copy): {}".format(copy_data[0,2]))
copy.close()
This outputs as:
Pixel 1: (72, 102, 112)
Pixel 2: (75, 105, 115)
Pixel 3: (71, 101, 111)
Pixel 1 (copy): (70, 100, 110)
Pixel 2 (copy): (77, 107, 117)
Pixel 3 (copy): (74, 104, 114)
Initially, I thought PIL might be changing all pixel values by 2 values for each of the R, G, and B channels (as can be seen with the first two pixels), however the third pixel has a change of 3 values to each channel.
How can I make a reliable copy of the image, in order to change its pixels, where the starting pixels of the copied image are the same as its original?
NOTE: I have tried other images other than my 'mountain.jpg', but all seem to be causing the same issues.