File size: 3,492 Bytes
3e3154d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4eb7efb
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import os
from datetime import datetime
from PIL import Image
from pillow_heif import register_heif_opener



def read_files(dir_path):
    try:
        # Ensure the path is absolute and normalized
        dir_path = os.path.abspath(dir_path)
        print(f"Checking directory: {dir_path}")
        
        # Check if directory exists
        if not os.path.isdir(dir_path):
            raise ValueError(f"Directory {dir_path} does not exist")
        
        # Check if we have read permissions
        if not os.access(dir_path, os.R_OK):
            raise PermissionError(f"No read permission for directory {dir_path}")
        
        files_info = []
        # Iterate through directory entries
        print (dir_path)
        all_files=os.scandir(dir_path)
        
        for entry in all_files:
            if entry.is_file():
                try:
                    mod_time = datetime.fromtimestamp(entry.stat().st_mtime)
                    files_info.append(entry.name)
                    # print(f"Found file: {entry.name}, Modified: {mod_time}")
                except Exception as e:
                    print(f"Error processing file {entry.name}: {str(e)}")
        
        if not files_info:
            print(f"No files found in directory {dir_path}")
        
        # Sort files by modification time (newest first)
        files_info.sort(key=lambda x: x[1], reverse=True)
        print (len(files_info))
        return files_info
    
    except Exception as e:
        print(f"Error accessing directory {dir_path}: {str(e)}")
        return []




def convert_heic_to_jpg(input_image_path):
    """
    Convert a .heic image to JPEG format and delete the original .heic file.
    Args:
        input_image_path (str): Path to the input .heic file.
    Returns:
        str: Path to the output JPEG file, or None if conversion fails.
    """
    try:
        # Normalize input path
        input_image_path = os.path.abspath(input_image_path)
        print(f"Processing file: {input_image_path}")

        # Check if file exists
        if not os.path.isfile(input_image_path):
            print(f"Error: File '{input_image_path}' does not exist")
            return None

        # Check if file has .heic extension
        if not input_image_path.lower().endswith('.heic'):
            print(f"Error: File '{input_image_path}' is not a .heic file")
            return None

        # Register HEIF opener to enable Pillow to read .heic files
        register_heif_opener()

        # Open the image
        img = Image.open(input_image_path)
        print(f"Image opened: {img.size}, {img.mode}")

        # Generate output path (replace .heic with .jpg)
        output_image_path = os.path.splitext(input_image_path)[0] + '.jpg'

        # Convert to RGB and save as JPEG
        img_rgb = img.convert('RGB')
        img_rgb.save(output_image_path, format='JPEG', quality=95)
        print(f"Converted {input_image_path} to {output_image_path}")

        # Delete the original .heic file
        try:
            os.remove(input_image_path)
            print(f"Deleted original file: {input_image_path}")
        except Exception as e:
            print(f"Error deleting {input_image_path}: {str(e)}")
            # Still return the output path since conversion was successful
            return output_image_path

        return output_image_path

    except Exception as e:
        print(f"Error converting {input_image_path}: {str(e)}")
        return None