4

1.Load image.jpg into array 2.randomly choose coordinates from array and display that pixel with all it's attributes 3. Pop coordinate used from array. 4. Repeat #2 until array is empty

This would display an image with random pixels populated.

The end result would always be the original image, but each time it would populate in a different manner.

How can this be done in Python3?

9
  • Seems not too difficult. Which part are you having trouble with? Sounds like i=Image.open(); n=np.array(i); s=np.shuffle(...); Image.fromarray(s).show() Commented Dec 21, 2019 at 5:16
  • stackoverflow.com/questions/52436652/… Commented Dec 21, 2019 at 6:19
  • Does this answer your question? How to randomize image pixels in python Commented Dec 21, 2019 at 6:20
  • I am not trying to create a random image, but rather display random pixels one at a time which form my original picture once all the pixels are displayed. Thus from the array that represents the image, choose one pixel randomly and display it. Remove that coordinate from the array and choose again another pixel randomly from the area and display. If this is done with a delay you will see an image form from individual pixels added to screen. Commented Dec 21, 2019 at 8:25
  • Oh, I see. Your image starts off completely black and then fades in, one pixel at a time in a random order till the whole image is displayed. Correct? What OS are you using? Commented Dec 21, 2019 at 9:55

1 Answer 1

2

Here's one way of doing it...

#!/usr/bin/env python3

import numpy as np
import cv2

# Open the image and make black version, same size, to fill randomly
im = cv2.imread('paddington.png')
fade = np.zeros_like(im)

# Generate a randomly shuffled array of the coordinates
X,Y = np.where(im[...,0]>=0)
coords = np.column_stack((X,Y))
np.random.shuffle(coords)

for n, c in enumerate(list(coords)):
    # Copy one original pixel across to image we are fading in
    x, y = c
    fade[x,y] = im[x,y]
    # It takes 1ms to update the image, so we don't update after every pixel
    if n % 500 == 0:
        cv2.imshow('Fading in...', fade)
        cv2.waitKey(1)

# Image should now be completely faded in
cv2.imshow('Fading in...', fade)
cv2.waitKey()

enter image description here

Keywords: Python, OpenCV, fade, fade in, fade from black, fade from white, image processing, video.

Sign up to request clarification or add additional context in comments.

4 Comments

The actual image fades more smoothly than this implies, I just made an animated GIF with a slow 2 fps update rate to fit inside the StackOverflow limit of 2MB per image.
This works really nicely and exactly what I was looking for. Paddington seems to reveal himself fairly quickly (features) and I wonder how to make it more random and perhaps a bit faster. I am using a large image from: nirklars.files.wordpress.com/2015/02/… and it is indeed random, but a bit slow to populate. THANKS!!!
Change the 500 to 2000 or something so it updates less often but more pixels at a time.
I will adjust various values to optimize the output. PNG seems to handle better than JPG.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.