Computer generated contemporary art

(2 comments)

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)

Computer-generated art image 1

Computer-generated art image 2

Computer-generated art image 3

Computer-generated art image 4

Computer-generated art image 5

Computer-generated art image 6

Current rating: 4

Comments

Joseph C. Slater 4 months, 3 weeks 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.

Link | Reply
Currently unrated

christian 4 months, 3 weeks ago

Hi Joseph,
Thanks for your kind comments: I'm constantly finding new things to learn about Python.
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

Link | Reply
Currently unrated

New Comment

required

required (not published)

optional

required