-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageComposer.py
More file actions
148 lines (103 loc) · 3.29 KB
/
Copy pathImageComposer.py
File metadata and controls
148 lines (103 loc) · 3.29 KB
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
from PIL import Image, ImageDraw
import math
import numpy
import requests
import random
pageHeight = 2480
pageWidth = 3508
dpi = 300
cardHeight = int(round(3.5 * dpi))
cardWidth = int(round(2.5 * dpi))
xBorder = int((pageWidth - (3 * cardWidth)) / 4)
yBorder = int((pageHeight - (2 * cardHeight)) / 3)
# def ComposeImage():
# createCanvas()
# drawLines()
# drawCards()
def createCanvas():
im = Image.new("RGB", (pageWidth, pageHeight), color=(255, 255, 255))
drawHorizontalLines(im)
drawVerticleLines(im)
return im
def addImages(imgs):
im = createCanvas()
j = 0
pages = []
for i in range(len(imgs)):
if (needNewImage(i)):
j = j + 1
im.save("cardSheet-" + str(j) + ".jpg", format="JPEG")
pages.append("cardSheet-" + str(j) + ".jpg")
im = createCanvas()
response = requests.get(imgs[i], stream=True)
col = getCol(i)
row = getRow(i)
image = Image.open(response.raw)
image = image.resize((cardWidth, cardHeight))
im.paste(image, cords(col, row))
drawCorners(im, col, row)
im.save("cardSheet-" + str(j + 1) + ".jpg", format="JPEG")
pages.append("cardSheet-" + str(j + 1) + ".jpg")
return pages
def cords(col, row):
return ((xBorder + (col * cardWidth) + (col * xBorder)),
(yBorder + (row * cardHeight) + (row * yBorder)))
def drawCorners(im, col, row):
draw = ImageDraw.Draw(im)
squareSize = cardWidth / 25
corners = []
left = cords(col, row)[0]
right = left + (cardWidth - squareSize)
top = cords(col, row)[1]
bottom = top + (cardHeight - squareSize)
start = (left, top)
end = (left + squareSize, top + squareSize)
topLeft = (start, end)
corners.append(topLeft)
start = (right, top)
end = (right + squareSize, top + squareSize)
topRight = (start, end)
corners.append(topRight)
start = (left, bottom)
end = (left + squareSize, bottom + squareSize)
bottomLeft = (start, end)
corners.append(bottomLeft)
start = (right, bottom)
end = (right + squareSize, bottom + squareSize)
bottomRight = (start, end)
corners.append(bottomRight)
for corner in corners:
draw.rectangle(corner, fill=(24, 21, 16))
def drawLine():
return
def drawVerticleLines(im):
for col in range(3):
x = cords(col, 0)[0] - 3
draw = ImageDraw.Draw(im)
draw.line((x, 0, x, pageHeight), width=6, fill=(0, 0, 0))
x = x + cardWidth + 5
draw = ImageDraw.Draw(im)
draw.line((x, 0, x, pageHeight), width=6, fill=(0, 0, 0))
def drawHorizontalLines(im):
for row in range(2):
y = cords(0, row)[1] - 3
draw = ImageDraw.Draw(im)
draw.line((0, y, pageWidth, y), width=6, fill=(0, 0, 0))
y = y + cardHeight + 5
draw = ImageDraw.Draw(im)
draw.line((0, y, pageWidth, y), width=6, fill=(0, 0, 0))
def getCol(i):
return i % 3
def getRow(i):
return 1 if ((i % 6) > 2) else 0
def needNewImage(i):
return ((i > 0) and (i % 6 == 0))
# def getCorner(x, y):
# left = cords(col, row)[0]
# right = left + (cardWidth - squareSize)
# top = cords(col, row)[1]
# bottom = top + (cardHeight - squareSize)
# if (x == 'left'):
# else:
# if(y == 'top'):
# else:\