File size: 2,276 Bytes
fc8bf44
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
from PIL import Image
from pathlib import Path
import logging

def resize_crop_image(img_path, mode, width, height, maintain_ratio, quality):
    if not img_path:
        return None, "❌ Please upload an image"
    try:
        img = Image.open(img_path)
        original_size = img.size
        quality_filter = getattr(Image, quality, Image.LANCZOS)
        if mode == "Resize":
            if maintain_ratio:
                img.thumbnail((int(width), int(height)), quality_filter)
            else:
                img = img.resize((int(width), int(height)), quality_filter)
        elif mode == "Crop":
            crop_width, crop_height = int(width), int(height)
            left = (img.width - crop_width) // 2
            top = (img.height - crop_height) // 2
            right = left + crop_width
            bottom = top + crop_height
            img = img.crop((left, top, right, bottom))
        elif mode == "Smart Crop":
            target_ratio = width / height
            current_ratio = img.width / img.height
            if current_ratio > target_ratio:
                new_width = int(img.height * target_ratio)
                left = (img.width - new_width) // 2
                img = img.crop((left, 0, left + new_width, img.height))
            else:
                new_height = int(img.width / target_ratio)
                top = (img.height - new_height) // 2
                img = img.crop((0, top, img.width, top + new_height))
            img = img.resize((int(width), int(height)), quality_filter)
        elif mode == "Canvas Resize":
            canvas = Image.new("RGB", (int(width), int(height)), (255, 255, 255))
            paste_x = (int(width) - img.width) // 2
            paste_y = (int(height) - img.height) // 2
            if img.mode == "RGBA":
                canvas.paste(img, (paste_x, paste_y), img)
            else:
                canvas.paste(img, (paste_x, paste_y))
            img = canvas
        out_path = Path(img_path).with_name(f"{mode.lower()}_{Path(img_path).name}")
        img.save(out_path)
        return str(out_path), f"✅ {mode} completed: {original_size}{img.size}"
    except Exception as e:
        logging.exception("Resize/crop failed")
        return None, f"❌ Processing error: {str(e)}"