-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdoodler.py
executable file
·62 lines (53 loc) · 1.66 KB
/
doodler.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/usr/bin/python
import sys
import random
from PIL import Image
from PIL import ImageOps
# get_alpha returns alpha layer of image or None if there is no alpha layer
def get_alpha(image):
try:
return image.split()[image.getbands().index('A')]
except ValueError:
return None
# doodle doodles given images on canvas of given size
# and returns PIL.Image object
def doodle(size, images, background_color="white"):
X_OVERLAP=0.2
Y_OVERLAP=0.35
if type(size) is not tuple or size <= (0, 0):
raise ValueError('size must be type like this: (width, height)')
if len(images) == 0:
raise ValueError('images list is empty')
d = Image.new('RGBA', size)
d.paste(background_color)
off_x, off_y = 0, 0
while True:
img = random.choice(images)
n_w = int(img.width*(1-X_OVERLAP))
n_x = off_x + n_w
n_y = int(off_y + img.height*(1-Y_OVERLAP))
if d.height < n_y:
break
if d.width < n_x:
off_x = 0
off_y = n_y
if random.choice([True, False]):
img = ImageOps.mirror(img)
mask = get_alpha(img)
d.paste(img, (off_x, off_y), mask)
off_x += n_w
return d
def open_images(names):
imgs = []
for n in names:
imgs.append(Image.open(n))
return imgs
if __name__ == '__main__':
try:
args = sys.argv[1:]
imgs = open_images(args[2:])
d = doodle((int(args[0]), int(args[1])), imgs)
d.save('doodle.png')
except:
print("Usage: doodler.py <width> <height> <image1>[,<image2>[,<image3>,...]]")
print("Resulting image will be saved as 'doodle.png'")