I am trying to put my image in list form in order to insert pixels to the image. However, I'm running into issues on converting back to an image once I've done the list manipulation. Turns out just going from image -> array -> list -> array -> image messes up the image pretty good.
However, going from image -> array -> image seems to work just fine. It's that extra step of going to a list that's causing issues.
Below is the python3 code I'm running. Can anyone tell me what I'm doing wrong and how to get the 3D list converted back into an RGB image that is identical to the original? Both the original and final arrays appear to be the same so not sure what PIL doesn't like...
import numpy as np
from PIL import Image
img = Image.open('1.jpg')
array = np.array(img)
list1 = array.tolist()
arrayF = np.array(list1)
img2 = Image.fromarray(arrayF,"RGB")
img2.save('trial.jpg')
print(array)
print(arrayF)
If you inspect the dtype
of your arrays, with print(array.dtype)
, you will see that array
is 8-bit and arrayF
is 64-bit... which is unacceptable to PIL for an RGB image.
Make arrayF
back into 8-bit with:
arrayF = arrayF.astype(np.uint8)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.