@@ -28,10 +28,10 @@ def __init__(self, *args, **kwargs):
28
28
@staticmethod
29
29
def create_cover (radio : Radio ):
30
30
"""create a cover page for start of video. No text pasted on this page.
31
-
31
+
32
32
Arguments:
33
33
radio {Radio} -- Radio
34
-
34
+
35
35
Returns:
36
36
image -- a cv2 frame.
37
37
"""
@@ -43,21 +43,21 @@ def create_cover(radio: Radio):
43
43
return image
44
44
45
45
@staticmethod
46
- def create_page (page : Page , radio :Radio ):
46
+ def create_page (page : Page , radio : Radio ):
47
47
"""Create a gadio page frame.
48
- Pipeline:
48
+ Pipeline:
49
49
1. Load image with opencv, use opencv to resize and blur.
50
50
2. Convert opencv image to Pillow image
51
51
3. Draw text on Pillow image
52
52
4. Convert back to opencv image for opencv VideoWriter
53
53
54
- Beware that Pillow image and opencv channel orders are different.
54
+ Beware that Pillow image and opencv channel orders are different.
55
55
Arguments:
56
56
page {Page} -- Gadio page
57
-
57
+
58
58
Keyword Arguments:
59
59
radio {Radio} -- radio
60
-
60
+
61
61
Returns:
62
62
np.array -- An numpy array representing cv2 image.
63
63
"""
@@ -67,6 +67,8 @@ def create_page(page: Page, radio:Radio):
67
67
image_dir = os .sep .join (['cache' , str (radio .radio_id ), radio .cover .local_name ])
68
68
else :
69
69
image_dir = os .sep .join (['cache' , str (radio .radio_id ), page .image .local_name ])
70
+ qr_dir = os .sep .join (['cache' , str (radio .radio_id ), 'qr_quotes' , page .image .local_name .split ('.' )[0 ] + ".png" ])
71
+
70
72
image = cv2 .imread (image_dir )
71
73
image_suffix = page .image .suffix
72
74
background_image = Frame .expand_frame (image , Frame .width , Frame .height )
@@ -77,15 +79,15 @@ def create_page(page: Page, radio:Radio):
77
79
background_rgb = cv2 .cvtColor (background_image , cv2 .COLOR_BGR2RGB )
78
80
content_rgb = cv2 .cvtColor (content_image , cv2 .COLOR_BGR2RGB )
79
81
80
- #Convert to RGBA for transparency rendering
82
+ # Convert to RGBA for transparency rendering
81
83
frame = Image .fromarray (background_rgb ).convert ('RGBA' )
82
84
83
85
mask = Image .new ('RGBA' , (Frame .width , Frame .height ), color = (0 , 0 , 0 , 128 ))
84
86
frame .paste (mask , (0 , 0 ), mask = mask )
85
87
86
88
left_offset = int (round (245 / 1920 * Frame .width )) + int (round ((550 - content_image .shape [1 ])/ 2 ))
87
89
top_offset = int (round (210 / 1080 * Frame .height )) + int (round ((550 - content_image .shape [0 ])/ 2 ))
88
-
90
+
89
91
content_frame = Image .fromarray (content_rgb )
90
92
content_image_mask = Image .new ('RGBA' , (content_image .shape [1 ], content_image .shape [0 ]), color = (0 , 0 , 0 , 26 ))
91
93
if (image_suffix == "" or image_suffix .lower () == '.gif' ):
@@ -104,13 +106,18 @@ def create_page(page: Page, radio:Radio):
104
106
qr_top_offset = int (round (917 / 1080 * Frame .height ))
105
107
frame .paste (logo_image , (logo_left_offset , logo_top_offset ), mask = logo_image )
106
108
frame .paste (qr_image , (qr_left_offset , qr_top_offset ), mask = qr_image )
109
+ if os .path .exists (qr_dir ):
110
+ qr_right_offset = int (round (1700 / 1920 * Frame .width ))
111
+ page_qr_image = Image .open (qr_dir ).convert ('RGBA' )
112
+ page_qr_image = page_qr_image .resize ((86 , 86 ))
113
+ frame .paste (page_qr_image , (qr_right_offset , qr_top_offset ), mask = page_qr_image )
107
114
except :
108
115
print ("Passing logo rendering due to file error" )
109
116
110
117
draw = ImageDraw .Draw (frame )
111
118
112
119
text_width_limit = int (round (770 / 1920 * Frame .width ))
113
-
120
+
114
121
title_string = Frame .title_wrapper .wrap_string (page .title , text_width_limit )
115
122
print ('Title:' , title_string )
116
123
raw_content = page .content
@@ -123,7 +130,7 @@ def create_page(page: Page, radio:Radio):
123
130
title_height = Frame .title_font .getsize_multiline (title_string )[1 ]
124
131
title_space_bottom = int (round (Frame .title_font .size * 0.9 ))
125
132
content_height_limit = int (round (574 / 1080 * Frame .height )) - title_height - title_space_bottom
126
-
133
+
127
134
content_space = int (round (Frame .content_font .size * 0.8 ))
128
135
actual_content_height = Frame .content_font .getsize_multiline (content_string , spacing = content_space )[1 ]
129
136
while (actual_content_height > content_height_limit ):
@@ -132,25 +139,25 @@ def create_page(page: Page, radio:Radio):
132
139
content_wrapper = Wrapper (Frame .content_font )
133
140
content_string = content_wrapper .wrap_string (raw_content , text_width_limit )
134
141
actual_content_height = Frame .content_font .getsize_multiline (content_string , spacing = content_space )[1 ]
135
- #print(actual_content_height)
136
-
142
+ # print(actual_content_height)
143
+
137
144
print (content_string )
138
145
draw .text ((text_left_offset , text_top_offset ), title_string , config ['gcores_title_color' ], font = Frame .title_font )
139
146
draw .text ((text_left_offset , text_top_offset + title_height + title_space_bottom ), content_string , config ['gcores_content_color' ], font = Frame .content_font , spacing = content_space )
140
-
141
- #Reset content_wrapper and content_font
147
+
148
+ # Reset content_wrapper and content_font
142
149
Frame .content_font = ImageFont .truetype (config ['content_font' ], config ['content_font_size' ], encoding = "utf-8" )
143
150
Frame .content_wrapper = Wrapper (Frame .content_font )
144
151
145
152
cv2charimg = np .array (frame )
146
153
result = cv2 .cvtColor (cv2charimg , cv2 .COLOR_RGB2BGR )
147
- #cv2.imwrite('test.jpg',result)
148
- #cv2.waitKey()
154
+ # cv2.imwrite('test.jpg',result)
155
+ # cv2.waitKey()
149
156
return result
150
157
151
158
@staticmethod
152
159
def expand_frame (image , target_width , target_height ):
153
- """Expand a frame so it is larger than the rectangle
160
+ """Expand a frame so it is larger than the rectangle
154
161
155
162
Arguments:
156
163
image {Image} -- cv2 image
@@ -171,7 +178,7 @@ def expand_frame(image, target_width, target_height):
171
178
actual_width = max (int (image .shape [1 ] / ratio ), target_width )
172
179
actuai_height = max (int (image .shape [0 ] / ratio ), target_height )
173
180
result = cv2 .resize (image , (actual_width , actuai_height ),
174
- interpolation = cv2 .INTER_CUBIC )
181
+ interpolation = cv2 .INTER_CUBIC )
175
182
left = int ((result .shape [1 ] - target_width ) / 2 )
176
183
right = left + target_width
177
184
top = int ((result .shape [0 ] - target_height ) / 2 )
@@ -181,12 +188,12 @@ def expand_frame(image, target_width, target_height):
181
188
@staticmethod
182
189
def shrink_frame (image , target_width , target_height ):
183
190
"""Shrink a frame so it is smaller than the rectangle
184
-
191
+
185
192
Arguments:
186
193
image {Image} -- np array
187
194
target_width {int} -- target width of rectangle
188
195
target_height {int} -- target height of rectangle
189
-
196
+
190
197
Returns:
191
198
np.array -- resized image
192
199
"""
@@ -203,4 +210,4 @@ def shrink_frame(image, target_width, target_height):
203
210
@staticmethod
204
211
def shrink_font (font , font_family ):
205
212
result_font = ImageFont .truetype (font_family , font .size - 2 , encoding = "utf-8" )
206
- return result_font
213
+ return result_font
0 commit comments