-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsquares1_9.py
105 lines (80 loc) · 2.71 KB
/
squares1_9.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
"""
Makes NFT Style image
ready for another image to be merged with it
and adds text
"""
import random
import svgwrite
import svgutils.transform as st
from pathlib import Path
from svglib.svglib import svg2rlg
from reportlab.graphics import renderPM
class Dwg:
"""create class to make rectangles and text"""
def __init__(self, height=10, width=10, fill="rgb(255,255,44)"):
self.width = height
self.height = width
self.dwg = svgwrite.Drawing(
"test.svg",
size=(400, 400),
profile="tiny",
stroke_width=0.5,
stroke="black",
stroke_opacity=1.0,
fill=fill,
)
def draw_rectangle(self):
for x in range(0, self.width):
for y in range(0, self.height):
pos_mod_x = (random.random() - 0.5) * (x + (y * self.width))
pos_mod_y = (random.random() - 0.5) * (x + (y * self.height))
self.dwg.add(
self.dwg.rect(
(x * 10 + pos_mod_x, y * 10 + pos_mod_y),
(self.width, self.height),
)
)
def draw_text(self):
self.dwg.add(
self.dwg.text(
"Dr Pi",
insert=(55, 125),
stroke="none",
fill="#900",
font_size="90px",
font_weight="bold",
font_family="Arial",
)
)
def save_as(self, filename):
self.dwg.saveas(filename)
# main
if __name__ == "__main__":
r1, r2, r3 = random.randint(1, 255), random.randint(1, 255), random.randint(1, 255)
path = Path("source_images/pi_400.svg")
assert path.is_file(), "pi_400 missing"
d1 = Dwg()
d1.draw_rectangle()
d1.save_as("first.svg")
d2 = Dwg(height=15, fill=f"rgb({r2},{r1},{r3})")
d2.draw_rectangle()
d2.save_as("second.svg")
d3 = Dwg(width=15, fill=f"rgb({r1},{r2},{r3})")
d3.draw_rectangle()
d3.draw_text() # Add Some Text
d3.save_as("third.svg")
template = st.fromfile("source_images/pi_400.svg")
second_svg = st.fromfile("second.svg")
third_svg = st.fromfile("third.svg")
template.append(second_svg)
template.append(third_svg)
# save merged file
template.save("merged.svg")
# # save source SVG files
# drawing = svg2rlg("first.svg")
# renderPM.drawToFile(drawing, "source_images/first.png", fmt="PNG")
# drawing = svg2rlg("second.svg")
# renderPM.drawToFile(drawing, "source_images/second.png", fmt="PNG")
# drawing = svg2rlg("third.svg")
# renderPM.drawToFile(drawing, "source_images/third.png", fmt="PNG")
print("\n\tDone - now check merged.svg\n\n")