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

Commit 55d35c5

Browse files
authored
Merge pull request #16 from yihong0618/master
fix requirements.txt and add qr source Add arg -q, default no qr code, use -q to generate qr
2 parents 8114f31 + 4e88f0c commit 55d35c5

File tree

4 files changed

+72
-34
lines changed

4 files changed

+72
-34
lines changed

gadio/crawlers/crawler.py

Lines changed: 32 additions & 7 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:
@@ -74,16 +75,18 @@ def download_audio(audio: Audio, file_dir: str):
7475
return
7576

7677
@staticmethod
77-
def download_assets(radio: Radio, file_dir: str):
78+
def download_assets(radio: Radio, file_dir: str, with_quote: bool):
7879
id = str(radio.radio_id)
7980
file_dir = file_dir + os.sep + id
8081
Crawler.download_image(radio.cover, file_dir)
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+
if with_quote:
89+
Crawler.make_quote_qr_image(page.quote_href, page.image.local_name, file_dir + os.sep + "qr_quotes")
8790

8891
@staticmethod
8992
def get_latest():
@@ -94,7 +97,7 @@ def get_latest():
9497
parsed = json.loads(content)
9598
id = parsed['data'][0]['id']
9699
return int(id)
97-
100+
98101
@staticmethod
99102
def get_headers(radio: Radio):
100103
offset = config['start_offset']
@@ -116,4 +119,26 @@ def get_headers(radio: Radio):
116119
links.write("\n\n")
117120
length=len(line)
118121
links.writelines(line)
119-
links.close()
122+
links.close()
123+
124+
@staticmethod
125+
def make_quote_qr_image(text, name, file_dir):
126+
if not os.path.exists(file_dir):
127+
print("Folder", file_dir, 'does not exist. Creating...')
128+
os.makedirs(file_dir)
129+
print("Saving qr_quotes", name)
130+
if text:
131+
name = name.split('.')[0] + ".png"
132+
try:
133+
myqr.run(
134+
text,
135+
version=2,
136+
level="H",
137+
picture=None,
138+
colorized=False,
139+
contrast=1.0,
140+
save_name=name,
141+
save_dir=file_dir,
142+
)
143+
except:
144+
print("wrong qr code")

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

gcores.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,33 @@
44
from gadio.text.text import *
55
import sys
66

7-
def main(id: int, skip_crawling: bool):
7+
8+
def main(id: int, skip_crawling: bool, with_quote: bool):
89
parsed_json = Crawler.crawl(id)
910
cache_dir = os.sep.join([os.curdir, 'cache', str(id), 'data.json'])
1011
with open(cache_dir, 'r', encoding='utf-8') as file:
1112
radio = Radio.load_from_json(parsed_json)
1213
if (not skip_crawling):
13-
Crawler.download_assets(radio, os.curdir+os.sep+'cache')
14+
Crawler.download_assets(radio, os.curdir+os.sep+'cache', with_quote)
1415
Video.create_video(radio)
1516

1617
if __name__ == "__main__":
1718
skip_crawling = False
18-
if (len(sys.argv) == 1 or sys.argv[1]=='-s'):
19+
with_quote = False
20+
if (len(sys.argv) == 1 or sys.argv[1] == '-s' or sys.argv[1] == '-q'):
21+
if "-q" in sys.argv:
22+
with_quote = True
1923
print("----------")
2024
print("Start to create the latest gadio video...")
2125
id = Crawler.get_latest()
2226
print(id)
23-
main(id, False)
27+
main(id, False, with_quote)
2428
else:
2529
title = sys.argv[1]
2630
skip_crawling = False
2731
if (len(sys.argv) > 2):
2832
if ("-s" in sys.argv):
2933
skip_crawling = True
30-
main(int(title), skip_crawling)
34+
elif "-q" in sys.argv:
35+
with_quote = True
36+
main(int(title), skip_crawling, with_quote)

requirements.txt

58 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)