Skip to content

Commit

Permalink
added latest code changes; some cleanup; updated README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
miezekatze committed Oct 27, 2014
1 parent d949b0c commit 82483e4
Show file tree
Hide file tree
Showing 22 changed files with 395 additions and 84 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
.DS_Store
.Python
bin/
include/
lib/
share/
share/

*.py[cod]
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
requirements
============

* libjpeg

brew install libjpeg

* PIL

bin/pip install PIL --allow-external PIL --allow-unverified PIL --upgrade

* https://stackoverflow.com/questions/9166400/convert-rgba-png-to-rgb-with-pil
Binary file added img/Ngoc4.JPG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/apple.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/apple2.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/bs.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/bs.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/bs2.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/bs2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/cat.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes
File renamed without changes
File renamed without changes
Binary file added img/ml2.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes
File renamed without changes
File renamed without changes
Binary file added img/owl.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file added src/__init__.py
Empty file.
88 changes: 88 additions & 0 deletions src/draw.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
from PIL import Image
from PIL import ImageDraw
import random
import tempfile
import signal
import sys

BACKGROUND = (255, 255, 255, 255)


def generate_image(size, dna):
img = Image.new('RGBA', size, BACKGROUND)

for (points, colour) in dna:
draw = Image.new('RGBA', size)
pdraw = ImageDraw.Draw(draw)

outline = (colour[0], colour[1], colour[2], colour[3])
for p in points:

pdraw.polygon(points,
fill=colour,
outline=outline)

img.paste(draw, mask=draw)
return img


def convert_image_to_rgb(img):
temp_dir = tempfile.gettempdir()
# png = Image.open(object.logo.path)
# png.load() # required for png.split()
img.load()
bg = Image.new("RGB", img.size, (255, 255, 255))
bg.paste(img, mask=img.split()[3]) # 3 is the alpha channel

tf = tempfile.NamedTemporaryFile(delete=False)
tf_name = "%s.jpg" % tf.name
bg.save(tf_name, 'JPEG', quality=100)
# tmp = Image.open(tf_name)
return bg


def main():
# step 1:
# figure out how to draw transparent polygons on top of each other
size = (512, 512)
# base image might need to be rgb, not rgba!
# (https://stackoverflow.com/questions/359706/how-do-you-draw-transparent-polygons-with-python)
img = Image.new('RGBA', size, (255, 255, 255, 0))

draw = Image.new('RGBA', size)
pdraw = ImageDraw.Draw(draw)
colour = (128, 0, 0, 128)
points = [(64, 64), (128, 64), (128, 128), (64, 128), ]
pdraw.polygon(points, fill=colour, outline=colour)
img.paste(draw, mask=draw)

colour = (64, 64, 64, 64)
points = [(96, 96), (160, 96), (160, 160), (96, 160), ]
pdraw.polygon(points, fill=colour, outline=colour)
img.paste(draw, mask=draw)

colour = (0, 196, 0, 128)
points = [(256, 256), (320, 256), (320, 320), (256, 320), ]
pdraw.polygon(points, fill=colour, outline=colour)
img.paste(draw, mask=draw)

colour = (0, 0, 196, 64)
points = [(192, 192), (384, 192), (384, 384), (192, 384), ]
pdraw.polygon(points, fill=colour, outline=colour)
img.paste(draw, mask=draw)

colour = (128, 0, 0, 96)
points = [(224, 224), (352, 224), (352, 352), (224, 352), ]
pdraw.polygon(points, fill=colour, outline=colour)
img.paste(draw, mask=draw)

# read one pixel value
r, g, b, a = img.getpixel((192, 192))
print r, g, b, a
# img = convert_image_to_rgb(img)

img.show()


if __name__ == "__main__":
main()
152 changes: 69 additions & 83 deletions src/evolisa.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
from PIL import Image
from PIL import ImageDraw
import random
import tempfile
import signal
import sys

BACKGROUND = (255, 255, 255, 255)
# BACKGROUND = (0, 0, 0, 255)
Expand All @@ -14,23 +17,27 @@
# previous painting did, then overwrite the current DNA with the new DNA
# 5) repeat from 1

POLYGONS = 50
POLYGONS = 200
GO = True
SHOW_EVERY = 10
LIMIT = 5000
ALPHA = 127
SHOW_EVERY = 5
LIMIT = 100000
ALPHA = 256
MOAR = 20
SAVED = None
# SAVED = [([(180, 211), (395, 376), (111, 182), (220, 270)], (67, 243, 131, 152)), ([(377, 128), (90, 100), (80, -5), (297, 375)], (59, 43, 107, 147)), ([(38, 141), (400, 10), (71, 185), (74, 144)], (197, 95, 175, 174)), ([(510, 22), (172, 126), (145, 302), (530, 330)], (43, 165, 57, 67)), ([(524, 373), (392, 57), (84, 144), (536, 292)], (207, 140, 97, 82)), ([(447, 243), (511, -17), (82, 157), (182, 56)], (239, 116, 9, 228)), ([(370, 33), (33, 176), (438, 164), (41, 181)], (127, 40, 18, 22)), ([(137, 237), (265, 399), (387, 411), (64, 360)], (13, 88, 158, 229)), ([(510, 234), (366, 65), (347, 392), (458, 156)], (29, 76, 151, 125)), ([(385, -17), (82, 43), (346, 341), (55, 94)], (222, 61, 148, 147)), ([(-1, 351), (219, 371), (234, 262), (164, 78)], (176, 216, 136, 235)), ([(487, 21), (339, 129), (81, 420), (73, 77)], (165, 156, 204, 243)), ([(526, 248), (1, -12), (493, 5), (487, 233)], (127, 161, 78, 245)), ([(117, 133), (117, 121), (410, 282), (130, 229)], (178, 197, 231, 133)), ([(553, 310), (95, 317), (280, 338), (504, 71)], (82, 32, 118, 212)), ([(269, 332), (186, 212), (458, 335), (294, 14)], (34, 223, 202, 148)), ([(510, 403), (129, 132), (327, 31), (73, 58)], (73, 220, 247, 81)), ([(536, 411), (276, 123), (375, 223), (272, 384)], (18, 23, 53, 212)), ([(30, 63), (427, 237), (417, 357), (-18, 332)], (52, 118, 205, 228)), ([(50, 14), (363, 198), (117, -5), (229, 199)], (74, 213, 210, 30)), ([(-18, 419), (380, 401), (487, 111), (142, 247)], (74, 28, 20, 226)), ([(548, 352), (288, 144), (95, 98), (346, 187)], (144, 68, 113, 241)), ([(172, 332), (206, 367), (535, 322), (424, -20)], (136, 211, 59, 221)), ([(308, 263), (244, 68), (216, 150), (63, 410)], (100, 115, 143, 3)), ([(-10, -14), (223, 28), (115, 295), (18, 284)], (124, 34, 19, 197)), ([(321, 281), (550, 84), (391, 319), (263, 238)], (226, 148, 124, 53)), ([(544, 388), (331, 31), (256, -1), (93, 233)], (17, 141, 17, 220)), ([(478, 336), (552, 40), (351, 62), (102, 361)], (93, 107, 17, 252)), ([(245, 6), (300, 281), (371, 247), (74, 110)], (84, 103, 65, 57)), ([(253, 348), (349, 285), (349, 248), (211, 418)], (104, 168, 3, 34))]

def generate_dna(img_size):
dna = []
for i in range(POLYGONS):
points = []
for j in range(5):
points.append((random.randrange(0, img_size[0], 1),
random.randrange(0, img_size[1], 1)))
for j in range(4):
points.append((random.randrange(0 - MOAR, img_size[0] + MOAR, 1),
random.randrange(0 - MOAR, img_size[1] + MOAR, 1)))
fill = (random.randrange(0, 256, 1),
random.randrange(0, 256, 1),
random.randrange(0, 256, 1), ALPHA)
# fill = (0, 0, 0, ALPHA)
fill = (0, 0, 0, random.randrange(0, 256))
points = sorted(points)
dna.append((points, fill))

return dna
Expand All @@ -42,7 +49,7 @@ def generate_image(size, dna):
draw = Image.new('RGBA', size)
pdraw = ImageDraw.Draw(draw)

outline = (colour[0], colour[1], colour[2], ALPHA)
outline = (colour[0], colour[1], colour[2], colour[3])
for p in points:

pdraw.polygon(points,
Expand All @@ -52,42 +59,63 @@ def generate_image(size, dna):
img.paste(draw, mask=draw)
return img

def convert_image_to_rgb(img):
temp_dir = tempfile.gettempdir()
# png = Image.open(object.logo.path)
# png.load() # required for png.split()
img.load()
bg = Image.new("RGB", img.size, (255, 255, 255))
bg.paste(img, mask=img.split()[3]) # 3 is the alpha channel

tf = tempfile.NamedTemporaryFile(delete=False)
tf_name = "%s.jpg" % tf.name
bg.save(tf_name, 'JPEG', quality=100)
# tmp = Image.open(tf_name)
return bg

def main():
# image = Image.open('src/mlbig.png')
# image = Image.open('src/mlbig2.png')
image = Image.open('src/ml2.png')
# image = Image.open('src/ml.jpg')
# image = Image.open('src/ml2.jpg')
image = Image.open('src/bs2.jpg')
image.show()
size = image.size

# step 0
dna = generate_dna(size)
if SAVED is None:
dna = generate_dna(size)
else:
dna = SAVED
# print dna
img = generate_image(size, dna)
img = convert_image_to_rgb(generate_image(size, dna))
if not GO:
img.show()

# step 1 - copy and mutate current dna seq
# dna_mut = mutate(size, dna)
# print dna_mut
step = 0
current = img
while step < LIMIT and GO:
if step % 20 == 0:
print "Step %d" % step

if step % 10 == 0:
print "Generation %d" % step
dna_mut = mutate(image.size, dna)
img_mut = generate_image(image.size, dna_mut)
img_rgb = convert_image_to_rgb(img_mut)

if fitness(image, current) < fitness(image, img_mut):
dna = dna
current = current
fit_1 = fitness(image, current)
fit_2 = fitness(image, img_rgb)

if fit_1 < fit_2:
pass
else:
dna = dna_mut
current = img_mut
dna = dna_mut[:]
current = img_rgb
img_rgb.show()
print dna
print "\n"

step = step + 1
if step % SHOW_EVERY == 0:
current.show()
# print dna
# if step % SHOW_EVERY == 0:
# current.show()
# # print dna

def mutate(size, dna):
dna_mut = [mutate_part(size, x) for x in dna]
Expand All @@ -100,74 +128,32 @@ def mutate(size, dna):
def mutate_part(size, part):
points = part[0]
colour = part[1]
# chance to mutate

rnd = random.randrange(0, 1000, 1) / 1000.0
if rnd > 0.650:
if rnd > 0.500:
# do nothing
return (points, colour)

# mutate points
points_mut = []
# try changing only one point in the polygon
rnd_idx = random.randrange(0, len(points))
p = points[rnd_idx]
# for p in points:
rnd_val_x = random.randrange(0, 11, 1)
rnd_val_y = random.randrange(0, 11, 1)
if random.randint(0, 1) == 1:
rnd_val_x = rnd_val_x * -1
if random.randint(0, 1) == 1:
rnd_val_y = rnd_val_y * -1
x = p[0] + rnd_val_x
y = p[1] + rnd_val_y
if x < 0:
x = 0
if x > size[0]:
x = size[0]
if y < 0:
y = 0
if y > size[1]:
y = size[1]
# points_mut.append((x, y))
points(rnd_idx) = (x, y)
points_mut = points

if rnd < 0.350:
for j in range(4):
points_mut.append((random.randrange(0 - MOAR, size[0] + MOAR, 1),
random.randrange(0 - MOAR, size[1] + MOAR, 1)))

if rnd < 0.450:
# mutate colour
r = random.randrange(0, 11, 1)
g = random.randrange(0, 11, 1)
b = random.randrange(0, 11, 1)
if random.randint(0, 1) == 1:
r = r * -1
if random.randint(0, 1) == 1:
g = g * -1
if random.randint(0, 1) == 1:
b = b * -1
r = colour[0] + r
g = colour[1] + g
b = colour[2] + b
if r > 255:
r = 255 - r
if r < 0:
r = 0
if g > 255:
g = 255 - g
if g < 0:
g = 0
if b > 255:
b = 255 - b
if b < 0:
b = 0
colour = (r, g, b, ALPHA)
colour = (random.randrange(0, 256),
random.randrange(0, 256),
random.randrange(0, 256),
random.randrange(0, ALPHA))

return (points_mut, colour)

def fitness(img_1, img_2):
fitness = 0
for y in range(0, img_1.size[1]):
for x in range(0, img_1.size[0]):
r1, g1, b1, a = img_1.getpixel((x, y)) # TODO ALPHA MODE?
r2, g2, b2, a = img_2.getpixel((x, y))
r1, g1, b1 = img_1.getpixel((x, y))
r2, g2, b2 = img_2.getpixel((x, y))
# get delta per color
d_r = r1 - r2
d_b = b1 - b2
Expand All @@ -179,4 +165,4 @@ def fitness(img_1, img_2):
return fitness

if __name__ == "__main__":
main()
main()
Loading

0 comments on commit 82483e4

Please sign in to comment.