yopines
yopines

Reputation: 59

Python: how to display an image pixel by pixel using random coordinates

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?

Upvotes: 4

Views: 1494

Answers (1)

Mark Setchell
Mark Setchell

Reputation: 207435

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.

Upvotes: 2

Related Questions