Post date: Jun 3, 2013 6:00:29 PM
My son posted on Facebook that if a program existed that would generate random pictures, eventually you would have pictures of everything that has ever happened as well as anything that will happen. Similar to a thousand monkeys on a thousand typewriters, just with pictures.
I'm certain this program has been written countless times before, just not by me. So I surprised him by writing one for him.
Not performance-tuned, but it's Python. I use Python 2.7. You'll also need to install Python Image Library (PIL). You can get the 64-bit Windows version of PIL from here: http://www.lfd.uci.edu/~gohlke/pythonlibs/
This spits out a 640x480 RGB image in 256 colors. You could make it higher resolution or color depth if you want, but why waste the CPU cycles? It increments the filename automatically. Images are placed in the Images subdirectory. To stop generating, hit CTL-C.
Save the code below into a file named MonkeyDraw.py. After you install PIL, usage is: python MonkeyDraw.py
Needless to say, you'd better like pictures of noise...
import osfrom PIL import Image, ImageDrawfrom random import randint# size of the image to createx = 640y = 480# Function stolen from stackexchange.comdef getNextFilePath(output_folder): highest_num = 0 for f in os.listdir(output_folder): if os.path.isfile(os.path.join(output_folder, f)): file_name = os.path.splitext(f)[0] try: file_num = int(file_name) if file_num > highest_num: highest_num = file_num except ValueError: 'The file name "%s" is not an integer. Skipping' % file_name output_file = os.path.join(output_folder, str(highest_num + 1)) return output_file# My stuff - Could be improved greatly, this was a quick and dirty proof of concept. IO could be maximally reduced by sending the array pre-write to# a pattern-recognition algorithm.
while True:image = Image.new('RGB', (x,y), None) # create an image
draw = ImageDraw.Draw(image) # create a draw object
# Draw, pardner!
for i in range(0,x):
for j in range (0,y):
draw.point((i,j), fill=(randint(0,255),randint(0,255),randint(0,255))) #pseudo-random color
del draw # Free object
#increment the file number and save as a jpg. Change to "PNG" if you prefer .png
image.save( getNextFilePath('.\Images') + '.jpg', 'JPEG')