I'm looking for a script that generates random images using the PIL Image module by assigning random RGB values to each pixel on a blank canvas. Does anyone have one of those lying around? (I want to write my own just for practice, but if it's already been done by a pro I'd also like to study the pro's version.)

Recommended Answers

All 6 Replies

How do you think you could do it? If you understand the capabilities of the module, then try some experimentation and don't steal from the experts until you have some understanding of what you are doing.

Well, it's not "stealing from the experts" to study the snippets written by experienced programmers as opposed to reinventing the wheel on all code, but I see what you mean.

I did bone up on some modules and wrote a relatively simple script that generates random 800x800 images:

#! /usr/bin/env python3

from PIL import Image
import random

def drawImage():
    testImage = Image.new("RGB", (600,600), (255,255,255))
    pixel = testImage.load()

    for x in range(600):
        for y in range(600):
            red = random.randrange(0,255)
            blue = random.randrange(0,255)
            green = random.randrange(0,255)
            pixel[x,y]=(red,blue,green)
    return testImage

def main():
    finalImage = drawImage()
    finalImage.save("finalImage.jpg")

if __name__ == "__main__":
    main()

The problem is that every image this script generates looks like snow on an old tube television screen (if you remember those). It can be used as a Rorsharch blot, but has no other real uses. Here is what pretty much all images generated by that script look like:

https://www.dropbox.com/s/n4amfovz9zjpcrr/finalImage.jpg?dl=0

I played around with that image in GiMP by converting it to grayscale and doing some other manipulations, and I came up with what one person told me looks like a pattern used in floor tiles:

https://www.dropbox.com/s/bgfyhtqhk1leh27/finalimage2.png?dl=0

I plan to try to develop algorithms that either create more useful random images or test random images and save only the ones that look like something. All suggestions would be appreciated.

This is perhaps a more efficient way to generate the same kind of random image

>>> from PIL import Image
>>> import os
>>> size = (600, 600)
>>> im = Image.new('RGB', size)
>>> def ran():
...  return os.urandom(600*600)
... 
>>> pixels = zip(ran(), ran(), ran())
>>> im.putdata(list(pixels))
>>> im.show()

Of course, it does not look like something.

On my computer, im.show() opens the image with a GUI named imagemagick display where there are commands to apply various algorithms to the image. Consider installing imagemagick if you don't have it.

Also take a look at the turtle demo files in
C:\Python34\Lib\turtledemo
for instance

GUI toolkits like Tkinter can be used to draw strange three dimensional objects. Just play with it ...

''' tk_canvas_multi_circles101.py
draw a series of circles to form 3D objects

'''

try:
    # Python2
    import Tkinter as tk
except ImportError:
    # Python3
    import tkinter as tk

def draw_circle(x, y, radius, color):
    '''
    draw a circle with given radius, center coordinates x,y and color

    '''
    # to draw a circle you need to get the upper left
    # and lower right corner coordinates of a square
    rect = get_square(x, y, radius)
    # draw the cicle that fits into the rect/square
    circle = cv.create_oval(rect, fill=color)

def get_square(x, y, radius):
    '''
    given the center=(x, y) and radius
    calculate the square for a circle to fit into
    return x1, y1, x2, y2 of the square's ulc=(x1, y1) and
    lrc=(x2, y2) diagonal corner coordinates
    '''
    x1 = x - radius
    y1 = y - radius
    x2 = x + radius
    y2 = y + radius
    return x1, y1, x2, y2

# create the basic window, let's call it 'root'
root = tk.Tk()
# only set x=100, y=80 position of root window upper left corner
root.geometry("+%d+%d" % (100, 80))
root.title("a series of circles")

# create a canvas to draw on
cv = tk.Canvas(root, width=600, height=600, bg='green')
# position the canvas via a grid layout
cv.grid()

color = 'red'
radius = 20
limit = 15
for k in range(1, limit):
    x = 25 * k
    y = 25 * k
    radius = radius + 2 * k
    draw_circle(x, y, radius, color)

for k in range(limit-1, 1, -1):
    x = 25 * k + int(150 * 1/k)
    y = 25 * k
    #color = 'blue'
    radius = radius - 2 * k
    draw_circle(x, y, radius, color)

# start the event loop
root.mainloop()
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.