-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathpanel_layout.py
executable file
·168 lines (156 loc) · 5.79 KB
/
panel_layout.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#!/usr/bin/env python
from sketchify import *
import os, sys
from PIL import Image, ImageOps, ImageChops, ImageFont, ImageDraw, ImageFilter
from random import Random
random=Random()
try:
font=ImageFont.truetype("arialbi.ttf", 64)
except:
try:
font=ImageFont.truetype("arial.ttf", 64)
except:
font=ImageFont.load_default()
candidates=[]
def walk_helper(arg, dirname, fnames):
for item in fnames:
fullpath=os.path.join(dirname, item)
if not (os.path.isdir(fullpath)):
candidates.append(fullpath)
for path in sys.argv[1:]:
if(os.path.isdir(path)):
os.path.walk(path, walk_helper, None)
else:
candidates.append(path)
def pickCandidate():
random.shuffle(candidates)
bg=None
for item in candidates:
try:
bg=Image.open(item).convert("RGB")
return bg
except:
pass
return bg
def newPage():
return Image.new("RGBA", (10*72, 15*72), "#ffff")
def layoutRect(bbox, page, bordersize=5, colorize=True, sketchOnly=False):
width=bbox[2]-bbox[0]
height=bbox[3]-bbox[1]
if(width<=bordersize*2 or height<=bordersize*2):
return []
ok=False
while not ok:
try:
img=pickCandidate()
panel=panelify(img, (img.size[0]-bordersize*2, img.size[1]-bordersize*2))
ok=True
print("Panel successful")
except Exception as e:
print(e)
print("Could not panelify")
pass
if(img.size[0]>width):
img=img.resize((width, int(img.size[1]*(1.0*width/img.size[0]))))
if(img.size[1]>height):
img=img.resize((int(img.size[0]*(1.0*height/img.size[1])), height))
targetWidth=width
if(abs(width-(1.0*img.size[0]))/width>0.2):
if abs(width-img.size[0])>bordersize*2:
targetWidth=img.size[0]
targetHeight=height
if(abs(height-(1.0*img.size[1]))/height>0.2):
if abs(height-img.size[1])>bordersize*2:
targetHeight=img.size[1]
if(targetWidth<=bordersize*2):
targetWidth=width
if(targetHeight<=bordersize*2):
targetHeight=height
panel=panelify(img, (targetWidth, targetHeight), bordersize, colorize, sketchOnly)
page.paste(panel, (bbox[0], bbox[1]))
leftovers=[]
dWidth=width-targetWidth
dHeight=height-targetHeight
if targetWidth<width:
if targetHeight<height:
leftovers.append((bbox[2]-dWidth, bbox[1], bbox[2], bbox[3]-dHeight))
leftovers.append((bbox[2]-dWidth, bbox[3]-dHeight, bbox[2], bbox[3]))
leftovers.append((bbox[0], bbox[3]-dHeight, bbox[2]-dWidth, bbox[3]))
else:
leftovers.append((bbox[2]-dWidth, bbox[1], bbox[2], bbox[3]))
else:
if targetHeight<height:
leftovers.append((bbox[0], bbox[3]-dHeight, bbox[2], bbox[3]))
return leftovers
def layoutPage(bordersize=5, colorize=True, sketchOnly=False):
page=newPage()
topBoundaries=[]
bottomBoundaries=[]
leftovers=[(0, 0, page.size[0], page.size[1])]
while(len(leftovers)>0):
chunk=leftovers.pop()
topBoundaries.append((chunk[0], chunk[1]))
bottomBoundaries.append((chunk[0], chunk[3]))
leftovers.extend(layoutRect(chunk, page, bordersize, colorize, sketchOnly))
return (page, topBoundaries, bottomBoundaries)
def generateBox(line,colorize=True, maxwidth=720):
size=font.getsize(line)
if(size[0]>maxwidth):
boxes=[]
words=line.split()
i=1
while font.getsize(" ".join(words[:-i]))[0]>maxwidth and i<len(words):
i+=1
boxes.append(generateBox(" ".join(words[:-i]), colorize, maxwidth))
boxes.append(generateBox(" ".join(words[-i:]), colorize, maxwidth))
scratch=Image.new("RGBA", (max(boxes[0].size[0], boxes[1].size[0]), boxes[0].size[1]+boxes[1].size[1]), "#ffffcc")
scratch.paste(boxes[0], (0, 0))
scratch.paste(boxes[1], (0, boxes[0].size[1]))
return scratch
if(colorize):
color="#ffffcc"
else:
color="#ffffff"
box=Image.new("RGB", (size[0]+10, size[1]+10), color=color)
ImageDraw.Draw(box).text((5, 5), line, font=font, fill="#000000")
return box.convert("RGBA")
def genPage(lines, bordersize=5, colorize=True, sketchOnly=False):
(page, topBoundaries, bottomBoundaries)=layoutPage(bordersize, colorize, sketchOnly)
i=0
for line in lines:
i+=1
if(random.choice([True, False])):
# Align to the top of the frame
position=random.choice(topBoundaries)
topBoundaries.remove(position)
page.paste(generateBox(line, colorize, maxwidth=720-position[0]), (position[0]+2*bordersize, position[1]+2*bordersize))
else:
# Align to the bottom of the frame
position=random.choice(topBoundaries)
topBoundaries.remove(position)
box=generateBox(line, colorize, maxwidth=720-position[0])
page.paste(box, (position[0]+2*bordersize, position[1]-(2*bordersize+box.size[1])))
if(len(topBoundaries)==0 or len(bottomBoundaries)==0):
return (page, lines[i:])
if(i>5):
return (page, lines[i:])
return (page, [])
def genPages(lines, pfx, bordersize=5, colorize=True, sketchOnly=False):
pages=[]
while len(lines)>0:
try:
if(len(pages)==24):
colorize=False
if(len(pages)==94):
sketchOnly=True
if(len(pages)==101):
return
(page, lines2)=genPage(lines, bordersize, colorize, sketchOnly)
pages.append(page)
pages[-1].save(pfx+"-"+str(len(pages))+".png")
print("Page successful")
lines=lines2
except Exception as e:
print(e)
lines=list(sys.stdin.readlines())
genPages(lines, "comic")