diff --git a/run.py b/run.py index 3677344..52ecdcd 100644 --- a/run.py +++ b/run.py @@ -16,6 +16,7 @@ CONFIG_ROOT = json.load(open(CONFIG_FILE, "r", encoding="utf-8")) CONFIG_HOST = CONFIG_ROOT["server"]["host"] CONFIG_PORT = CONFIG_ROOT["server"]["port"] PAGINATION_COUNT = CONFIG_ROOT["server"]["count_page"] +TARGET_MIN_SIZE = CONFIG_ROOT["server"]["thumb_size"] GB_ID = CONFIG_ROOT["gb"]["id"] GB_HASH = CONFIG_ROOT["gb"]["hash"] GB_HEADERS = CONFIG_ROOT["gb"]["headers"] @@ -23,6 +24,7 @@ FILE_FOLDERS = CONFIG_ROOT["file"]["repos"] FILE_DW_FOLDERS = CONFIG_ROOT["file"]["dw"] THUMB_FOLDER = CONFIG_ROOT["file"]["thumb"] + gb = gelbooru.GB(GB_ID, GB_HASH, GB_HEADERS, FILE_DW_FOLDERS) log = logger.Logger() database = db.DB(True) @@ -82,6 +84,7 @@ def dw_api(): for chunk in r.iter_content(chunk_size=8192): f.write(chunk) database.add_raw(data_get) + image_processor.generate_thumb(FILE_DW_FOLDERS+"/"+file_name, THUMB_FOLDER, TARGET_MIN_SIZE) return jsonify({'status': 'success'}) except requests.exceptions.RequestException as e: error_message = f"Network error {image_id}: {e}" diff --git a/system_module/image_processor.py b/system_module/image_processor.py index 012f142..b4a683b 100644 --- a/system_module/image_processor.py +++ b/system_module/image_processor.py @@ -15,7 +15,7 @@ def check_ffmpeg(): except (FileNotFoundError, subprocess.CalledProcessError): return False -def generate_video_thumbnail(input_path_file, output_path_file, TARGET_MIN_SIZE): +def video_gen(input_path_file, output_path_file, TARGET_MIN_SIZE): vf_scale = f"scale='if(gt(iw,ih),-1,{TARGET_MIN_SIZE})':'if(gt(iw,ih),{TARGET_MIN_SIZE},-1)'" cmd = ["ffmpeg", "-y", "-i", input_path_file, "-ss", "00:00:00", "-vframes", "1", "-vf", vf_scale, "-q:v", "2", output_path_file] if os.name == 'nt': creationflags = subprocess.CREATE_NO_WINDOW @@ -24,11 +24,41 @@ def generate_video_thumbnail(input_path_file, output_path_file, TARGET_MIN_SIZE) if result.returncode != 0: raise Exception(f"FFmpeg error: {result.stderr.decode('utf-8', errors='ignore')}") -def video_gen(): - pass +def image_and_gif_gen(input_path_file, output_path_file, TARGET_MIN_SIZE): + with Image.open(input_path_file) as img: + img_rgba = img.convert("RGBA") -def image_and_gif_gen(): - pass + background = Image.new("RGB", img_rgba.size, (255, 255, 255)) + + background.paste(img_rgba, mask=img_rgba) + + final_img = background + + width, height = final_img.size + + if width <= TARGET_MIN_SIZE and height <= TARGET_MIN_SIZE: + final_img.save(output_path_file, "JPEG", quality=90) + else: + if width < height: + new_width = TARGET_MIN_SIZE + new_height = int(TARGET_MIN_SIZE * (height / width)) + else: + new_height = TARGET_MIN_SIZE + new_width = int(TARGET_MIN_SIZE * (width / height)) + + resized_img = final_img.resize((new_width, new_height), Image.Resampling.LANCZOS) + resized_img.save(output_path_file, "JPEG", quality=85) -def generate_thumb(source_image, thumb_folder, TARGET_MIN_SIZE): - pass \ No newline at end of file +def generate_thumb(input_path_file, THUMB_FOLDER, TARGET_MIN_SIZE): + ext_file = input_path_file.split(".")[-1] + if ext_file not in VALID_EXTENSIONS: + return {"status":False, "message":"Расширение не поддерживаеться!"} + thumb_filename = f"{input_path_file.split(".")[-2]}.jpg" + output_path_file = os.path.join(THUMB_FOLDER, thumb_filename) + if ext_file in EXTENSIONS_IMG: + image_and_gif_gen(input_path_file, output_path_file, TARGET_MIN_SIZE) + return {"status":True} + else: + if not check_ffmpeg(): return {"status":False, "message":"ffmpeg не установлен!"} + video_gen(input_path_file, output_path_file, TARGET_MIN_SIZE) + return {"status":True} \ No newline at end of file