Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
137 views
in Technique[技术] by (71.8m points)

Python: reverse of transferring image into list of pixels?

im = Image.open('segmentace_zkusebni_pi.jpg')
pixel = list(im.getdata())

gets me a list of RGB of every pixel in the image. For my thesis (not programming, math) I need also to reverse this process, after I do some calculations with RGB values of the pixels. So I need to get the list of 3-elemental vectors (variable pixel) transfer into image. Is there any way? Thank you very much

question from:https://stackoverflow.com/questions/65851446/python-reverse-of-transferring-image-into-list-of-pixels

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
import numpy as np

im = Image.open('segmentace_zkusebni_pi.jpg')
n = np.asarray(im)
shape = n.shape

pixel = list(im.getdata())
# pixel = n.reshape((shape[0]*shape[1], shape[2])).tolist()

# do your work, but make sure the image dimension is kept same
# preferably you should work with numpy array itself
..
..

im2 = Image.fromarray((np.array(pixel, dtype='uint8')).reshape(shape))
im2.show()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...