Create image_processor.py
Browse files- image_processor.py +40 -0
image_processor.py
ADDED
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import cv2
|
2 |
+
import numpy as np
|
3 |
+
from PIL import Image, ImageEnhance
|
4 |
+
from typing import Optional, Dict, Any
|
5 |
+
import logging
|
6 |
+
|
7 |
+
class ImageProcessor:
|
8 |
+
def __init__(self):
|
9 |
+
self.cache = {}
|
10 |
+
logging.info("Image processor initialized")
|
11 |
+
|
12 |
+
def enhance_image(self, image: Image.Image, params: Dict[str, Any]) -> Image.Image:
|
13 |
+
"""Amélioration optimisée de la qualité d'image"""
|
14 |
+
try:
|
15 |
+
# Paramètres de base
|
16 |
+
sharpness = params.get('detail_level', 7) / 5
|
17 |
+
contrast = params.get('contrast', 5) / 5
|
18 |
+
saturation = params.get('saturation', 5) / 5
|
19 |
+
|
20 |
+
# Application des améliorations
|
21 |
+
# 1. Netteté
|
22 |
+
if sharpness != 1:
|
23 |
+
enhancer = ImageEnhance.Sharpness(image)
|
24 |
+
image = enhancer.enhance(sharpness)
|
25 |
+
|
26 |
+
# 2. Contraste
|
27 |
+
if contrast != 1:
|
28 |
+
enhancer = ImageEnhance.Contrast(image)
|
29 |
+
image = enhancer.enhance(contrast)
|
30 |
+
|
31 |
+
# 3. Saturation
|
32 |
+
if saturation != 1:
|
33 |
+
enhancer = ImageEnhance.Color(image)
|
34 |
+
image = enhancer.enhance(saturation)
|
35 |
+
|
36 |
+
return image
|
37 |
+
|
38 |
+
except Exception as e:
|
39 |
+
logging.error(f"Erreur lors de l'amélioration de l'image: {str(e)}")
|
40 |
+
return image
|