fastmen111
fastmen111

Reputation: 71

integer list of rgb to image python

So I have a list of integer r,g,b ,r,g,b ,r ,g,b ,...

[255, 0, 0, 255, 0, 0, 255, 0, 0, 255, 0, 0, 127, 0, 255, 127, 0, 255, 127, 0, 255, 127, 0, 255, 127, 0, 255, 0, 127, 255, 0, 127, 255, 0, 127, 255, 0, 127, 255, 0, 127, 255, 0, 127, 255, 0, 127, 255]

I also have a size of image width: 4 and height: 4 of the image

So how can I use the library Pil to show the image

(note: I may have a longer list width and height)

Upvotes: 0

Views: 302

Answers (1)

Sin Han Jinn
Sin Han Jinn

Reputation: 684

Probably something like this,

from PIL import Image
import numpy as np

pixels =[255, 0, 0, 255, 0, 0, 255, 0, 0, 255, 0, 0, 127, 0, 255, 127, 0, 255, 127, 0, 255, 127, 0, 255, 127, 0, 255, 0, 127, 255, 0, 127, 255, 0, 127, 255, 0, 127, 255, 0, 127, 255, 0, 127, 255, 0, 127, 255]
image_out = Image.new(mode = "RGB", size = (200, 200))
image_out.putdata(pixels)
image_out.save('test_out.png')

If you want to show your image, this should do it:

image_out.show()

It's all in the documentation, go check it out for more. https://pillow.readthedocs.io/en/3.1.x/reference/Image.html

Using 4x4 pixels & converting to 200x200

from PIL import Image
import numpy as np

pixels =[(255,0,0),(255,0,0),(255,0,0),(255,0,0),(127,0,255),(127,0,255),(127,0,255),(127,0,255),(127,0,255),(0,127,255),(0,127,255),(0,127,255),(0,127,255),(0,127,255),(0,127,255),(0,127,255)]
image_out = Image.new("RGB",(4,4))
image_out.putdata(pixels)
image_out.save('test_out.png')
#
img = image_out.resize((200, 200), Image.ANTIALIAS)
img.save('testtest.png')

Initial 4x4 image gives (test_out.png):

enter image description here

Resized 200x200 image gives (testtest.png):

enter image description here

Upvotes: 1

Related Questions