Binary in Pixel Art Lesson

from random import randint
from PIL import Image
from IPython.display import Image as ImgDisplay

# create a new image with a black background
img = Image.new('1', (500, 500), 0)

# create a pixel access object for the image
pixels = img.load()

# set random pixel values
for i in range(img.size[0]):
    for j in range(img.size[1]):
        pixels[i, j] = randint(0, 1)

# save the image as a PNG file
img.save('random_pixel_art.png')

# display the image in the notebook
ImgDisplay(filename='random_pixel_art.png')