# Computer generated contemporary art

Just a quick selection of images randomly-generated by the following 10 lines or so of Python.

import sys
import numpy as np
from PIL import Image

try:
filename = sys.argv[1]
except IndexError:
filename = 'img.png'

rshift = 3
width, height = 600, 450
arr = np.ones((height, width, 3)) * 128

for y in range(1,height):
arr[y,0] = arr[y-1,0] + np.random.randint(-rshift, rshift+1, size=3)

for x in range(1, width):
for y in range(1,height-1):
arr[y,x] = ((arr[y-1, x-1] + arr[y,x-1] + arr[y+1,x-1])/3 +
np.random.randint(-rshift, rshift+1, size=3))

im = Image.fromarray(arr.astype(np.uint8)).convert('RGBA')
im.save(filename)


Current rating: 4.5

#### Joseph C. Slater 1 year, 3 months ago

Dr. Hill,
I'm working through your book, playing with examples, and this one drew my attention. However, I cannot figure out where the Image module is (with a capital "I"). There is an image package on pypi, but it's clearly not the same. Would you please clarify where this module can be found?

As an aside: my compliments on the text. I used to think I knew Python. I have two packages on pypi. I now realize that I have much more to learn. Thank you for such a wonderful text.

Currently unrated

#### christian 1 year, 3 months ago

Hi Joseph,
To answer your question, the most popular recent imaging library for Python is Pillow (https://python-pillow.org/), a fork of the older PIL library, and this contains the Image module.
I've updated the code above to do the import properly with this library: you can install it with pip or conda if you don't have it already.
Cheers,
Christian