Skip to content
This repository was archived by the owner on Dec 21, 2023. It is now read-only.

Commit 626c2d3

Browse files
committed
fix requirements.txt and add qr source
1 parent 8114f31 commit 626c2d3

File tree

3 files changed

+56
-28
lines changed

3 files changed

+56
-28
lines changed

gadio/crawlers/crawler.py

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import urllib.request
66

77
import requests
8+
from MyQR import myqr
89

910
from gadio.configs.config import config
1011
from gadio.models.asset import Image, Audio
@@ -21,9 +22,9 @@ class Crawler():
2122
@staticmethod
2223
def crawl(gadio_id: int):
2324
"""Get timeline and corresponding contents from Gcores website
24-
25+
2526
Arguments:
26-
gadio_id {int} -- gadio id in gcores websites.
27+
gadio_id {int} -- gadio id in gcores websites.
2728
"""
2829
url = api['radio_api_template'].format(radio_id=gadio_id)
2930
print("Extracting information from ", gadio_id)
@@ -45,7 +46,7 @@ def crawl(gadio_id: int):
4546
#print(cache_dir)
4647
json.dump(parsed, outfile, ensure_ascii=False, indent=4)
4748
return parsed
48-
49+
4950
@staticmethod
5051
def download_image(image: Image, file_dir: str):
5152
try:
@@ -81,9 +82,10 @@ def download_assets(radio: Radio, file_dir: str):
8182
Crawler.download_audio(radio.audio, file_dir + os.sep + 'audio')
8283
for user in radio.users:
8384
Crawler.download_image(user.portrait, file_dir + os.sep + 'users')
84-
85+
8586
for page in radio.timeline.values():
8687
Crawler.download_image(page.image, file_dir)
88+
Crawler.make_quote_qr_image(page.quote_href, page.image.local_name, file_dir + os.sep + "qr_quotes")
8789

8890
@staticmethod
8991
def get_latest():
@@ -94,7 +96,7 @@ def get_latest():
9496
parsed = json.loads(content)
9597
id = parsed['data'][0]['id']
9698
return int(id)
97-
99+
98100
@staticmethod
99101
def get_headers(radio: Radio):
100102
offset = config['start_offset']
@@ -116,4 +118,23 @@ def get_headers(radio: Radio):
116118
links.write("\n\n")
117119
length=len(line)
118120
links.writelines(line)
119-
links.close()
121+
links.close()
122+
123+
@staticmethod
124+
def make_quote_qr_image(text, name, file_dir):
125+
if not os.path.exists(file_dir):
126+
print("Folder", file_dir, 'does not exist. Creating...')
127+
os.makedirs(file_dir)
128+
print("Saving qr_quotes", name)
129+
if text:
130+
name = name.split('.')[0] + ".png"
131+
myqr.run(
132+
text,
133+
version=2,
134+
level="H",
135+
picture=None,
136+
colorized=False,
137+
contrast=1.0,
138+
save_name=name,
139+
save_dir=file_dir,
140+
)

gadio/media/frame.py

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ def __init__(self, *args, **kwargs):
2828
@staticmethod
2929
def create_cover(radio: Radio):
3030
"""create a cover page for start of video. No text pasted on this page.
31-
31+
3232
Arguments:
3333
radio {Radio} -- Radio
34-
34+
3535
Returns:
3636
image -- a cv2 frame.
3737
"""
@@ -43,21 +43,21 @@ def create_cover(radio: Radio):
4343
return image
4444

4545
@staticmethod
46-
def create_page(page: Page, radio:Radio):
46+
def create_page(page: Page, radio: Radio):
4747
"""Create a gadio page frame.
48-
Pipeline:
48+
Pipeline:
4949
1. Load image with opencv, use opencv to resize and blur.
5050
2. Convert opencv image to Pillow image
5151
3. Draw text on Pillow image
5252
4. Convert back to opencv image for opencv VideoWriter
5353
54-
Beware that Pillow image and opencv channel orders are different.
54+
Beware that Pillow image and opencv channel orders are different.
5555
Arguments:
5656
page {Page} -- Gadio page
57-
57+
5858
Keyword Arguments:
5959
radio {Radio} -- radio
60-
60+
6161
Returns:
6262
np.array -- An numpy array representing cv2 image.
6363
"""
@@ -67,6 +67,8 @@ def create_page(page: Page, radio:Radio):
6767
image_dir = os.sep.join(['cache', str(radio.radio_id), radio.cover.local_name])
6868
else:
6969
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+
7072
image = cv2.imread(image_dir)
7173
image_suffix = page.image.suffix
7274
background_image = Frame.expand_frame(image, Frame.width, Frame.height)
@@ -77,15 +79,15 @@ def create_page(page: Page, radio:Radio):
7779
background_rgb = cv2.cvtColor(background_image, cv2.COLOR_BGR2RGB)
7880
content_rgb = cv2.cvtColor(content_image, cv2.COLOR_BGR2RGB)
7981

80-
#Convert to RGBA for transparency rendering
82+
# Convert to RGBA for transparency rendering
8183
frame = Image.fromarray(background_rgb).convert('RGBA')
8284

8385
mask = Image.new('RGBA', (Frame.width, Frame.height), color=(0, 0, 0, 128))
8486
frame.paste(mask, (0, 0), mask=mask)
8587

8688
left_offset = int(round(245/1920 * Frame.width)) + int(round((550 - content_image.shape[1])/2))
8789
top_offset = int(round(210/1080 * Frame.height)) + int(round((550 - content_image.shape[0])/2))
88-
90+
8991
content_frame = Image.fromarray(content_rgb)
9092
content_image_mask = Image.new('RGBA', (content_image.shape[1], content_image.shape[0]), color=(0, 0, 0, 26))
9193
if (image_suffix == "" or image_suffix.lower() == '.gif'):
@@ -104,13 +106,18 @@ def create_page(page: Page, radio:Radio):
104106
qr_top_offset = int(round(917/1080 * Frame.height))
105107
frame.paste(logo_image, (logo_left_offset, logo_top_offset), mask=logo_image)
106108
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)
107114
except:
108115
print("Passing logo rendering due to file error")
109116

110117
draw = ImageDraw.Draw(frame)
111118

112119
text_width_limit = int(round(770 / 1920 * Frame.width))
113-
120+
114121
title_string = Frame.title_wrapper.wrap_string(page.title, text_width_limit)
115122
print('Title:', title_string)
116123
raw_content = page.content
@@ -123,7 +130,7 @@ def create_page(page: Page, radio:Radio):
123130
title_height = Frame.title_font.getsize_multiline(title_string)[1]
124131
title_space_bottom = int(round(Frame.title_font.size * 0.9))
125132
content_height_limit = int(round(574 / 1080 * Frame.height)) - title_height - title_space_bottom
126-
133+
127134
content_space = int(round(Frame.content_font.size * 0.8))
128135
actual_content_height = Frame.content_font.getsize_multiline(content_string, spacing=content_space)[1]
129136
while (actual_content_height > content_height_limit):
@@ -132,25 +139,25 @@ def create_page(page: Page, radio:Radio):
132139
content_wrapper = Wrapper(Frame.content_font)
133140
content_string = content_wrapper.wrap_string(raw_content, text_width_limit)
134141
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+
137144
print(content_string)
138145
draw.text((text_left_offset, text_top_offset), title_string, config['gcores_title_color'], font=Frame.title_font)
139146
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
142149
Frame.content_font = ImageFont.truetype(config['content_font'], config['content_font_size'], encoding="utf-8")
143150
Frame.content_wrapper = Wrapper(Frame.content_font)
144151

145152
cv2charimg = np.array(frame)
146153
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()
149156
return result
150157

151158
@staticmethod
152159
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
154161
155162
Arguments:
156163
image {Image} -- cv2 image
@@ -171,7 +178,7 @@ def expand_frame(image, target_width, target_height):
171178
actual_width = max(int(image.shape[1] / ratio), target_width)
172179
actuai_height = max(int(image.shape[0] / ratio), target_height)
173180
result = cv2.resize(image, (actual_width, actuai_height),
174-
interpolation=cv2.INTER_CUBIC)
181+
interpolation=cv2.INTER_CUBIC)
175182
left = int((result.shape[1] - target_width) / 2)
176183
right = left + target_width
177184
top = int((result.shape[0] - target_height) / 2)
@@ -181,12 +188,12 @@ def expand_frame(image, target_width, target_height):
181188
@staticmethod
182189
def shrink_frame(image, target_width, target_height):
183190
"""Shrink a frame so it is smaller than the rectangle
184-
191+
185192
Arguments:
186193
image {Image} -- np array
187194
target_width {int} -- target width of rectangle
188195
target_height {int} -- target height of rectangle
189-
196+
190197
Returns:
191198
np.array -- resized image
192199
"""
@@ -203,4 +210,4 @@ def shrink_frame(image, target_width, target_height):
203210
@staticmethod
204211
def shrink_font(font, font_family):
205212
result_font = ImageFont.truetype(font_family, font.size-2, encoding="utf-8")
206-
return result_font
213+
return result_font

requirements.txt

58 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)