fix: don't use local files

Signed-off-by: Frost Ming <me@frostming.com>
This commit is contained in:
Frost Ming 2023-12-12 11:50:19 +08:00
parent 56a0175cc2
commit 3007ed67a9
No known key found for this signature in database
GPG Key ID: 5BFA9CB4DDA943BF

View File

@ -2,7 +2,9 @@ import argparse
import gc import gc
import io import io
import random import random
import shutil
import subprocess import subprocess
import traceback
import numpy as np import numpy as np
import PIL import PIL
@ -16,8 +18,6 @@ from telebot import TeleBot # type: ignore
from telebot.types import Message # type: ignore from telebot.types import Message # type: ignore
PIL.Image.MAX_IMAGE_PIXELS = 933120000 PIL.Image.MAX_IMAGE_PIXELS = 933120000
file_in = "map.jpg"
file_out = "map_out.jpg"
class Plot(PrettyPlot): class Plot(PrettyPlot):
@ -57,28 +57,34 @@ def sizeof_image(image):
return buff.tell() return buff.tell()
def compress_image(input_path, output_path, target_size): def compress_image(input_image, target_size):
quality = 95 quality = 95
factor = 1.0 factor = 1.0
with Image.open(input_path) as img: input_image.seek(0)
target_bytes = 10 * 1024 * 1024 output = io.BytesIO()
with Image.open(input_image) as img:
while sizeof_image(img) > target_bytes: while sizeof_image(img) > target_size:
factor -= 0.05 factor -= 0.05
width, height = img.size width, height = img.size
img = img.resize( img = img.resize(
(int(width * factor), int(height * factor)), (int(width * factor), int(height * factor)),
PIL.Image.Resampling.LANCZOS, PIL.Image.Resampling.LANCZOS,
) )
img.save(output_path, format="JPEG", quality=quality) img.save(output, format="JPEG", quality=quality)
output.seek(0)
return output
def draw_pretty_map(location, file_name, style): def draw_pretty_map(location, style):
aoi = get_aoi(address=location, radius=1100, rectangular=True) aoi = get_aoi(address=location, radius=1100, rectangular=True)
df = get_osm_geometries(aoi=aoi) df = get_osm_geometries(aoi=aoi)
fig = Plot(df=df, aoi_bounds=aoi.bounds, draw_settings=STYLES[style]).plot_all() fig = Plot(df=df, aoi_bounds=aoi.bounds, draw_settings=STYLES[style]).plot_all()
fig.savefig(file_name) buffer = io.BytesIO()
compress_image(file_in, file_out, 9) # telegram tog need png less than 10MB fig.savefig(buffer, format="jpeg")
return compress_image(
buffer,
10 * 1024 * 1024, # telegram tog need png less than 10MB
)
def main(): def main():
@ -144,16 +150,18 @@ def main():
style = random.choice(styles_list) style = random.choice(styles_list)
try: try:
# TODO why this memory leak? # TODO why this memory leak?
draw_pretty_map(location, file_in, style) out_image = draw_pretty_map(location, style)
# tg can only send image less than 10MB # tg can only send image less than 10MB
with open(file_out, "rb") as photo: with open("map_out.jpg", "wb") as f:
bot.send_photo( shutil.copyfileobj(out_image, f)
message.chat.id, photo, reply_to_message_id=message.message_id out_image.seek(0)
) bot.send_photo(
message.chat.id, out_image, reply_to_message_id=message.message_id
)
except Exception as e: except Exception:
traceback.print_exc()
bot.reply_to(message, "Something wrong please check") bot.reply_to(message, "Something wrong please check")
print(str(e))
bot.delete_message(reply_message.chat.id, reply_message.message_id) bot.delete_message(reply_message.chat.id, reply_message.message_id)
# we need this, fuck it # we need this, fuck it
gc.collect() gc.collect()