files
Browse files
utils.py
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from datetime import datetime
|
| 3 |
+
from PIL import Image
|
| 4 |
+
from pillow_heif import register_heif_opener
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
def read_files(dir_path):
|
| 9 |
+
try:
|
| 10 |
+
# Ensure the path is absolute and normalized
|
| 11 |
+
dir_path = os.path.abspath(dir_path)
|
| 12 |
+
print(f"Checking directory: {dir_path}")
|
| 13 |
+
|
| 14 |
+
# Check if directory exists
|
| 15 |
+
if not os.path.isdir(dir_path):
|
| 16 |
+
raise ValueError(f"Directory {dir_path} does not exist")
|
| 17 |
+
|
| 18 |
+
# Check if we have read permissions
|
| 19 |
+
if not os.access(dir_path, os.R_OK):
|
| 20 |
+
raise PermissionError(f"No read permission for directory {dir_path}")
|
| 21 |
+
|
| 22 |
+
files_info = []
|
| 23 |
+
# Iterate through directory entries
|
| 24 |
+
print (dir_path)
|
| 25 |
+
all_files=os.scandir(dir_path)
|
| 26 |
+
|
| 27 |
+
for entry in all_files:
|
| 28 |
+
if entry.is_file():
|
| 29 |
+
try:
|
| 30 |
+
mod_time = datetime.fromtimestamp(entry.stat().st_mtime)
|
| 31 |
+
files_info.append(entry.name)
|
| 32 |
+
# print(f"Found file: {entry.name}, Modified: {mod_time}")
|
| 33 |
+
except Exception as e:
|
| 34 |
+
print(f"Error processing file {entry.name}: {str(e)}")
|
| 35 |
+
|
| 36 |
+
if not files_info:
|
| 37 |
+
print(f"No files found in directory {dir_path}")
|
| 38 |
+
|
| 39 |
+
# Sort files by modification time (newest first)
|
| 40 |
+
files_info.sort(key=lambda x: x[1], reverse=True)
|
| 41 |
+
print (len(files_info))
|
| 42 |
+
return files_info
|
| 43 |
+
|
| 44 |
+
except Exception as e:
|
| 45 |
+
print(f"Error accessing directory {dir_path}: {str(e)}")
|
| 46 |
+
return []
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
def convert_heic_to_jpg(input_image_path):
|
| 52 |
+
"""
|
| 53 |
+
Convert a .heic image to JPEG format and delete the original .heic file.
|
| 54 |
+
Args:
|
| 55 |
+
input_image_path (str): Path to the input .heic file.
|
| 56 |
+
Returns:
|
| 57 |
+
str: Path to the output JPEG file, or None if conversion fails.
|
| 58 |
+
"""
|
| 59 |
+
try:
|
| 60 |
+
# Normalize input path
|
| 61 |
+
input_image_path = os.path.abspath(input_image_path)
|
| 62 |
+
print(f"Processing file: {input_image_path}")
|
| 63 |
+
|
| 64 |
+
# Check if file exists
|
| 65 |
+
if not os.path.isfile(input_image_path):
|
| 66 |
+
print(f"Error: File '{input_image_path}' does not exist")
|
| 67 |
+
return None
|
| 68 |
+
|
| 69 |
+
# Check if file has .heic extension
|
| 70 |
+
if not input_image_path.lower().endswith('.heic'):
|
| 71 |
+
print(f"Error: File '{input_image_path}' is not a .heic file")
|
| 72 |
+
return None
|
| 73 |
+
|
| 74 |
+
# Register HEIF opener to enable Pillow to read .heic files
|
| 75 |
+
register_heif_opener()
|
| 76 |
+
|
| 77 |
+
# Open the image
|
| 78 |
+
img = Image.open(input_image_path)
|
| 79 |
+
print(f"Image opened: {img.size}, {img.mode}")
|
| 80 |
+
|
| 81 |
+
# Generate output path (replace .heic with .jpg)
|
| 82 |
+
output_image_path = os.path.splitext(input_image_path)[0] + '.jpg'
|
| 83 |
+
|
| 84 |
+
# Convert to RGB and save as JPEG
|
| 85 |
+
img_rgb = img.convert('RGB')
|
| 86 |
+
img_rgb.save(output_image_path, format='JPEG', quality=95)
|
| 87 |
+
print(f"Converted {input_image_path} to {output_image_path}")
|
| 88 |
+
|
| 89 |
+
# Delete the original .heic file
|
| 90 |
+
try:
|
| 91 |
+
os.remove(input_image_path)
|
| 92 |
+
print(f"Deleted original file: {input_image_path}")
|
| 93 |
+
except Exception as e:
|
| 94 |
+
print(f"Error deleting {input_image_path}: {str(e)}")
|
| 95 |
+
# Still return the output path since conversion was successful
|
| 96 |
+
return output_image_path
|
| 97 |
+
|
| 98 |
+
return output_image_path
|
| 99 |
+
|
| 100 |
+
except Exception as e:
|
| 101 |
+
print(f"Error converting {input_image_path}: {str(e)}")
|
| 102 |
+
return None
|