mirror of
				https://github.com/cdryzun/tg_bot_collections.git
				synced 2025-10-31 05:46:42 +08:00 
			
		
		
		
	fix: don't use local files
Signed-off-by: Frost Ming <me@frostming.com>
This commit is contained in:
		
							
								
								
									
										44
									
								
								hy_tg.py
									
									
									
									
									
								
							
							
						
						
									
										44
									
								
								hy_tg.py
									
									
									
									
									
								
							| @ -2,7 +2,9 @@ import argparse | ||||
| import gc | ||||
| import io | ||||
| import random | ||||
| import shutil | ||||
| import subprocess | ||||
| import traceback | ||||
|  | ||||
| import numpy as np | ||||
| import PIL | ||||
| @ -16,8 +18,6 @@ from telebot import TeleBot  # type: ignore | ||||
| from telebot.types import Message  # type: ignore | ||||
|  | ||||
| PIL.Image.MAX_IMAGE_PIXELS = 933120000 | ||||
| file_in = "map.jpg" | ||||
| file_out = "map_out.jpg" | ||||
|  | ||||
|  | ||||
| class Plot(PrettyPlot): | ||||
| @ -57,28 +57,34 @@ def sizeof_image(image): | ||||
|         return buff.tell() | ||||
|  | ||||
|  | ||||
| def compress_image(input_path, output_path, target_size): | ||||
| def compress_image(input_image, target_size): | ||||
|     quality = 95 | ||||
|     factor = 1.0 | ||||
|     with Image.open(input_path) as img: | ||||
|         target_bytes = 10 * 1024 * 1024 | ||||
|  | ||||
|         while sizeof_image(img) > target_bytes: | ||||
|     input_image.seek(0) | ||||
|     output = io.BytesIO() | ||||
|     with Image.open(input_image) as img: | ||||
|         while sizeof_image(img) > target_size: | ||||
|             factor -= 0.05 | ||||
|             width, height = img.size | ||||
|             img = img.resize( | ||||
|                 (int(width * factor), int(height * factor)), | ||||
|                 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) | ||||
|     df = get_osm_geometries(aoi=aoi) | ||||
|     fig = Plot(df=df, aoi_bounds=aoi.bounds, draw_settings=STYLES[style]).plot_all() | ||||
|     fig.savefig(file_name) | ||||
|     compress_image(file_in, file_out, 9)  # telegram tog need png less than 10MB | ||||
|     buffer = io.BytesIO() | ||||
|     fig.savefig(buffer, format="jpeg") | ||||
|     return compress_image( | ||||
|         buffer, | ||||
|         10 * 1024 * 1024,  # telegram tog need png less than 10MB | ||||
|     ) | ||||
|  | ||||
|  | ||||
| def main(): | ||||
| @ -144,16 +150,18 @@ def main(): | ||||
|         style = random.choice(styles_list) | ||||
|         try: | ||||
|             # 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 | ||||
|             with open(file_out, "rb") as photo: | ||||
|                 bot.send_photo( | ||||
|                     message.chat.id, photo, reply_to_message_id=message.message_id | ||||
|                 ) | ||||
|             with open("map_out.jpg", "wb") as f: | ||||
|                 shutil.copyfileobj(out_image, f) | ||||
|             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") | ||||
|             print(str(e)) | ||||
|         bot.delete_message(reply_message.chat.id, reply_message.message_id) | ||||
|         # we need this, fuck it | ||||
|         gc.collect() | ||||
|  | ||||
		Reference in New Issue
	
	Block a user