File size: 2,174 Bytes
b330d94
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fff4c94
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
52
53
54
55
56
57
58
59
60
61
62
63
64
import os
from PIL import Image
import pyheif
import io

class ImageConverter:
    def __init__(self):
        self.supported_formats = {
            'jpg': 'JPEG',
            'jpeg': 'JPEG',
            'png': 'PNG',
            'bmp': 'BMP',
            'tiff': 'TIFF',
            'gif': 'GIF',
            'webp': 'WEBP',
            'ico': 'ICO',
            'heic': 'HEIC',
            'heiv': 'HEIC',
            'heif': 'HEIC',
        }

    def open_heif_image(self, input_image):
        heif_file = pyheif.read(input_image)
        return Image.frombytes(
            heif_file.mode, 
            heif_file.size, 
            heif_file.data,
            "raw",
            heif_file.mode,
            heif_file.stride,
        )

    def convert_image(self, input_image, output_format, output_path=None):
        try:
            if not os.path.exists(input_image):
                raise FileNotFoundError(f"The input image '{input_image}' does not exist.")

            input_extension = input_image.split('.')[-1].lower()

            if input_extension not in self.supported_formats:
                raise ValueError(f"The input format '{input_extension}' is not supported.")

            if output_format.lower() not in self.supported_formats:
                raise ValueError(f"The output format '{output_format}' is not supported.")

            if input_extension in ['heic', 'heiv', 'heif']:
                image = self.open_heif_image(input_image)
            else:
                image = Image.open(input_image)

            if output_path is None:
                output_image = '.'.join(input_image.split('.')[:-1]) + f'.{output_format}'
            else:
                output_image = output_path

            image.save(output_image, self.supported_formats[output_format.lower()])
            print(f"Image converted and saved as {output_image}")
            return output_image, "Image converted and saved as {output_image}"
        except Exception as e:
            None, print(f"Error: {e}")

def convert_image(input_image, output_format):
    converter = ImageConverter()
    return converter.convert_image(input_image.name, output_format)