-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrop.py
120 lines (93 loc) · 4.13 KB
/
crop.py
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import os
import sys
import zipfile
import shutil
from PIL import Image, ImageFilter
from app_logging import app_logger
def handle_cbz_file(file_path):
"""
Handle the conversion of a .cbz file: unzip, process images, compress, and clean up.
:param file_path: Path to the .cbz file.
:return: None
"""
app_logger.info(f"********************// Crop Cover Image //********************")
app_logger.info(f"-- Handling CBZ file: {file_path}")
if not file_path.lower().endswith('.cbz'):
app_logger.info("Provided file is not a CBZ file.")
return
base_name = os.path.splitext(file_path)[0] # Removes the .cbz extension
zip_path = base_name + '.zip'
folder_name = base_name + '_folder'
app_logger.info(f"<strong>Processing CBZ:</strong> {file_path} --> {zip_path}")
try:
# Step 1: Rename .cbz to .zip
os.rename(file_path, zip_path)
# Step 2: Create a folder with the file name
os.makedirs(folder_name, exist_ok=True)
# Step 3: Unzip the .zip file contents into the folder
with zipfile.ZipFile(zip_path, 'r') as zf:
zf.extractall(folder_name)
# Step 4: Process the extracted images
#logger.info(f"Processing images in folder: {folder_name}")
process_image(folder_name)
# Step 5: Rename the original .zip file to .bak
bak_file_path = zip_path + '.bak'
os.rename(zip_path, bak_file_path)
# Step 6: Compress the folder contents back into a .cbz file
with zipfile.ZipFile(file_path, 'w', zipfile.ZIP_DEFLATED) as zf:
for root, _, files in os.walk(folder_name):
for file in files:
file_path_in_folder = os.path.join(root, file)
arcname = os.path.relpath(file_path_in_folder, folder_name)
zf.write(file_path_in_folder, arcname)
app_logger.info(f"<strong>Successfully re-compressed:</strong> {file_path}")
# Step 7: Delete the .bak file
os.remove(bak_file_path)
except Exception as e:
app_logger.error(f"<strong>Failed to process {file_path}:</strong> {e}")
finally:
# Clean up the temporary folder
if os.path.exists(folder_name):
shutil.rmtree(folder_name)
def process_image(directory: str) -> None:
# Ensure the directory exists
if not os.path.exists(directory):
app_logger.info(f"Directory {directory} does not exist.")
return
# Recursively search for files in the directory and subdirectories
def find_images(dir_path):
for root, _, files in os.walk(dir_path):
for file in files:
if file != "ComicInfo.xml":
yield os.path.join(root, file)
# Get the first image file found
image_files = list(find_images(directory))
if not image_files:
app_logger.info("No files found in the directory or its subdirectories.")
return
first_image_path = image_files[0]
file_name, file_extension = os.path.splitext(first_image_path)
try:
# Open the image
with Image.open(first_image_path) as img:
width, height = img.size
# Split the image in half
right_half = (width // 2, 0, width, height)
# Save the original image by appending "b" to the file name
backup_path = f"{file_name}b{file_extension}"
img.save(backup_path)
# Save the right half by appending "a" to the file name
right_half_img = img.crop(right_half)
new_image_path = f"{file_name}a{file_extension}"
right_half_img.save(new_image_path)
# Delete the original image
os.remove(first_image_path)
app_logger.info(f"<strong>Processed:</strong> {os.path.basename(first_image_path)}<br /> original saved as {backup_path}, <br />right half saved as {new_image_path}.")
except Exception as e:
app_logger.error(f"Error processing the image: {e}")
if __name__ == "__main__":
if len(sys.argv) < 2:
app_logger.error("No file provided!")
else:
file_path = sys.argv[1]
handle_cbz_file(file_path)