File size: 1,257 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
import logging
from PIL import Image
import numpy as np
from pathlib import Path

class ImageAnalysis:
    @staticmethod
    def analyze_image(img_path):
        if not img_path:
            return {"error": "No image provided"}
        try:
            img = Image.open(img_path)
            analysis = {
                "dimensions": f"{img.width} x {img.height}",
                "format": img.format,
                "mode": img.mode,
                "file_size": f"{Path(img_path).stat().st_size / 1024:.1f} KB",
                "has_transparency": img.mode in ("RGBA", "LA") or "transparency" in img.info,
                "color_palette": "Analyzed" if img.mode == "P" else "N/A"
            }
            if img.mode == "RGB":
                img_array = np.array(img)
                analysis["average_color"] = {
                    "red": int(np.mean(img_array[:,:,0])),
                    "green": int(np.mean(img_array[:,:,1])),
                    "blue": int(np.mean(img_array[:,:,2]))
                }
                analysis["brightness"] = int(np.mean(img_array))
            return analysis
        except Exception as e:
            logging.exception("Image analysis failed")
            return {"error": f"Analysis failed: {str(e)}"}