3

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?

yopines
  • 49
  • 5
  • 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()` – Mark Setchell Dec 21 '19 at 05:16
  • https://stackoverflow.com/questions/52436652/how-to-randomize-image-pixels-in-python/52437794#52437794 – Joe Dec 21 '19 at 06:19
  • Does this answer your question? [How to randomize image pixels in python](https://stackoverflow.com/questions/52436652/how-to-randomize-image-pixels-in-python) – Joe Dec 21 '19 at 06: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. – yopines Dec 21 '19 at 08: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? – Mark Setchell Dec 21 '19 at 09:55
  • What technology/environment were you planning to use for displaying it? Qt? X11? – Mark Setchell Dec 21 '19 at 10:01
  • Mark you got it right. I would like to first he able to see it in Python perhaps on Repl.it or Jupyter notebook or Pycharn IDE. The output would display or a webpage or an app opening up. I have some ideas on how to do this. Will post soon. Thanks – yopines Dec 21 '19 at 17:07
  • You could make an animated GIF of it too... – Mark Setchell Dec 21 '19 at 22:20
  • Using Ubuntu latest and PyCharm 2019.3 – yopines Dec 21 '19 at 22:33

1 Answers1

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.

Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
  • 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. – Mark Setchell Dec 23 '19 at 12:58
  • 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: https://nirklars.files.wordpress.com/2015/02/exomoon_of_binary_red_dwarfs__2_by_nirklars-d86asrr.png and it is indeed random, but a bit slow to populate. THANKS!!! – yopines Dec 23 '19 at 18:11
  • Change the 500 to 2000 or something so it updates less often but more pixels at a time. – Mark Setchell Dec 23 '19 at 18:32
  • I will adjust various values to optimize the output. PNG seems to handle better than JPG. – yopines Dec 23 '19 at 18:54