3

I have a set of RGB values. I need to put them in individual pixels. I did this with PIL, but I need to plot pixel one by one and look at the progress instead of getting the final image.

from PIL import Image
im = Image.open('suresh-pokharel.jpg')
pixels = im.load()
width, height = im.size

for i in range(width):
    for j in range(height):
        print(pixels[i,j])  # I want to put this pixels in a blank image and see the progress in image
5
  • Ok. Please add the first 4-5 pixels to your code. Do you mean you want to make an animation? What's the point of this please? Commented Feb 5, 2019 at 13:14
  • You say you want to put the pixels on a blank canvas, so why do you open a JPEG? Commented Feb 5, 2019 at 13:15
  • @MarkSetchell Yes, animation
    – psuresh
    Commented Feb 5, 2019 at 13:17
  • I need to make program that read all pixel values from the existing image and plot each pixel to visualize.
    – psuresh
    Commented Feb 5, 2019 at 13:27
  • You understand that the pixels will just be drawn from top-left to bottom-right? So objects won't start to appear and gradually fill out and become visible. They'll just appear in a top-bottom linescan way... Commented Feb 5, 2019 at 13:30

1 Answer 1

2

You can generate something like this:

enter image description here

with following code (thx @Mark Setchell for numpy hint):

import imageio
import numpy as np
from PIL import Image

img = Image.open('suresh-pokharel.jpg')
pixels = img.load()
width, height = img.size
img2 = Image.new('RGB', img.size, color='white')
pixels2 = img2.load()

i = 0
images = []
for y in range(height):
    for x in range(width):
        pixels2[x, y] = pixels[x, y]
        if i % 500 == 0:
            images.append(np.array(img2))
        i += 1

imageio.mimsave('result.gif', images)

Or this:

enter image description here

with following code:

import random
import imageio
import numpy as np
from PIL import Image

img = Image.open('suresh-pokharel.jpg')
pixels = img.load()
width, height = img.size
img2 = Image.new('RGB', img.size, color='white')
pixels2 = img2.load()

coord = []
for x in range(width):
    for y in range(height):
        coord.append((x, y))

images = []
while coord:
    x, y = random.choice(coord)
    pixels2[x, y] = pixels[x, y]
    coord.remove((x, y))
    if len(coord) % 500 == 0:
        images.append(np.array(img2))

imageio.mimsave('result.gif', images)
4
  • Neat! Why write them to disk though, then re-read them, then delete them? Commented Feb 5, 2019 at 13:54
  • Don't now how to directly send images from PIL to imageio
    – Alderven
    Commented Feb 5, 2019 at 13:56
  • You can make a PIL Image into a Numpy array with numpyarray= np.array(PILimage) Commented Feb 5, 2019 at 13:58
  • Didn't know about that! Thanks! I've updated my answer.
    – Alderven
    Commented Feb 5, 2019 at 14:05

Not the answer you're looking for? Browse other questions tagged or ask your own question.