GS2VT
GS2VT

Reputation: 39

How to create a RGB image using PIL from values in a list?

I have a list of RGB values that I want to create into a 32 x 32 image. I have tried: Code1

From the list: List Etc. for 1024 RGB values

I have no experience with this library so sorry for misunderstanding something.

Upvotes: 0

Views: 131

Answers (1)

Mathias Pfeil
Mathias Pfeil

Reputation: 86

Here is how we can create a 32x32 RGB image with a list of tuples.

import numpy as np
from PIL import Image

img = []

for i in range(1024):
    tup = (np.random.randint(0,255),np.random.randint(0,255),np.random.randint(0,255))
    img.append(tup)

im2 = Image.new(mode = 'RGB', size = (32,32))
im2.putdata(img)
im2.save('myimg.png')

We can also make it a little more compact, like this:

import numpy as np
from PIL import Image

img = [(np.random.randint(0,255),np.random.randint(0,255),np.random.randint(0,255)) for _ in range(1024)]

im2 = Image.new(mode = 'RGB', size = (32,32))
im2.putdata(img)
im2.save('myimg.png')

Upvotes: 1

Related Questions