0.0.10s
This commit is contained in:
@@ -2,8 +2,8 @@ import subprocess
|
||||
from PIL import Image
|
||||
import os
|
||||
|
||||
EXTENSIONS_IMG = ('.jpg', '.jpeg', '.png', '.gif', '.webp', '.bmp')
|
||||
EXTENSIONS_VID = ('.mp4', '.webm')
|
||||
EXTENSIONS_IMG = ('jpg', 'jpeg', 'png', 'gif', 'webp', 'bmp')
|
||||
EXTENSIONS_VID = ('mp4', 'webm')
|
||||
VALID_EXTENSIONS = EXTENSIONS_IMG + EXTENSIONS_VID
|
||||
|
||||
def check_ffmpeg():
|
||||
@@ -27,15 +27,10 @@ def video_gen(input_path_file, output_path_file, TARGET_MIN_SIZE):
|
||||
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")
|
||||
|
||||
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:
|
||||
@@ -45,7 +40,6 @@ def image_and_gif_gen(input_path_file, output_path_file, TARGET_MIN_SIZE):
|
||||
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)
|
||||
|
||||
@@ -53,7 +47,7 @@ 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"
|
||||
thumb_filename = f"{os.path.basename(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)
|
||||
|
||||
Reference in New Issue
Block a user