Seregon/PkgToolBox

Toolbox for analyzing and editing pkg application files for psp,ps3, ps4 and ps5, includes the most useful functions you might need.

Python/57.3 KB/No license
GraphicUserInterface/utils/image_utils.py
1from PIL import Image
2from PyQt5.QtGui import QImage, QPixmap
3import io
4 
5class ImageUtils:
6 @staticmethod
7 def load_and_scale_image(image_data, max_size):
8 """Load and scale an image from bytes data"""
9 try:
10 image = Image.open(io.BytesIO(image_data))
11
12 # Convert to RGB if needed
13 if image.mode != 'RGB':
14 image = image.convert('RGB')
15
16 # Scale image
17 image.thumbnail((max_size, max_size), Image.Resampling.LANCZOS)
18
19 return image
20 except Exception as e:
21 raise Exception(f"Error loading/scaling image: {str(e)}")
22
23 @staticmethod
24 def convert_to_qpixmap(pil_image):
25 """Convert PIL Image to QPixmap"""
26 try:
27 qimage = QImage(pil_image.tobytes(),
28 pil_image.width,
29 pil_image.height,
30 3 * pil_image.width,
31 QImage.Format_RGB888)
32 return QPixmap.fromImage(qimage)
33 except Exception as e:
34 raise Exception(f"Error converting to QPixmap: {str(e)}")
35 
36 @staticmethod
37 def create_thumbnail(image_data, size=(300, 300)):
38 """Create a thumbnail from image data"""
39 try:
40 image = ImageUtils.load_and_scale_image(image_data, max(size))
41 return ImageUtils.convert_to_qpixmap(image)
42 except Exception as e:
43 raise Exception(f"Error creating thumbnail: {str(e)}")