forked from boyi01/SlideSlowGenerator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess.py
More file actions
165 lines (133 loc) · 6.59 KB
/
process.py
File metadata and controls
165 lines (133 loc) · 6.59 KB
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
from PIL import Image, ImageFilter, ImageOps
import os
import json
def crop_to_aspect_ratio(image, target_ratio):
"""
Crop an image to a specified aspect ratio, centered on the middle of the image.
Parameters:
image (PIL.Image): The input image to be cropped.
target_ratio (float): The target aspect ratio (width / height).
Returns:
PIL.Image: The cropped image with the specified aspect ratio.
"""
# Get original image dimensions
original_width, original_height = image.size
original_ratio = original_width / original_height
# Determine new dimensions for cropping
if original_ratio > target_ratio:
# Image is wider than target ratio: crop width (centered horizontally)
new_width = int(original_height * target_ratio)
new_height = original_height
left = (original_width - new_width) // 2
upper = 0
else:
# Image is taller than target ratio: crop height (centered vertically)
new_width = original_width
new_height = int(original_width / target_ratio)
left = 0
upper = (original_height - new_height) // 2
# Calculate right and lower boundaries based on left and upper to maintain centered crop
right = left + new_width
lower = upper + new_height
# Crop and return the image, centered on the middle
return image.crop((left, upper, right, lower))
def process_image(input_path, output_path,blurred_background, target_width, target_height):
# Open the image
img = Image.open(input_path)
img = ImageOps.exif_transpose(img)
# Ensure image is in RGB mode (no transparency issues)
if img.mode != "RGB":
img = img.convert("RGB")
original_width, original_height = img.size
aspect_ratio = original_width / original_height
# Set target dimensions
target_width = target_width
target_height = target_height
target_aspect_ratio = 5 / 3
# Determine new dimensions with padding to achieve 5:3 aspect ratio
if aspect_ratio > target_aspect_ratio:
new_width = original_width
new_height = int(new_width / target_aspect_ratio)
else:
new_height = original_height
new_width = int(new_height * target_aspect_ratio)
paste_position = ((new_width - original_width) // 2, (new_height - original_height) // 2)
if blurred_background:
# Create a blurred version of the original image
new_background = img.filter(ImageFilter.GaussianBlur(25))
new_background = crop_to_aspect_ratio(new_background,target_aspect_ratio)
# Resize the blurred image to match the new dimensions
new_background = ImageOps.contain(new_background, (new_width, new_height))
else:
# Create a new image with black background and the desired aspect ratio
new_background = Image.new("RGB", (new_width, new_height), (0, 0, 0))
# Use the blurred background to fill the black areas
new_background.paste(img,paste_position)
new_background=new_background.resize((target_width, target_height))
# Save the final image
new_background.save(output_path)
print(f"Image processed and saved as {output_path}")
def load_processed_images(json_file):
if os.path.exists(json_file):
try:
with open(json_file, 'r') as f:
data = json.load(f)
return data if isinstance(data, list) else [] # Ensure it returns a list
except (json.JSONDecodeError, ValueError):
print(f"Warning: {json_file} is empty or contains invalid JSON. Starting fresh.")
return []
return []
def save_processed_images(json_file, processed_images):
with open(json_file, 'w') as f:
json.dump(processed_images, f, indent=4)
def resize_images_in_folder(input_folder, output_folder, json_file, blurred_background,target_width, target_height):
# Load processed images from JSON log
processed_images = load_processed_images(json_file)
processed_paths = {img['input_path'] for img in processed_images}
image_index = len(processed_images) # Start numbering from the last count
if image_index != 0:
image_index += 1
# Walk through all files in input directory and subdirectories
current_input_paths = set()
for root, _, files in os.walk(input_folder):
for file in files:
if file.lower().endswith(('.jpg', '.jpeg', '.png')):
input_path = os.path.join(root, file)
current_input_paths.add(input_path)
# Skip already processed images
if input_path in processed_paths:
print(f"Skipping {input_path} (already processed)")
continue
# Define output path with numbered filename
output_path = os.path.join(output_folder, f"{image_index}.jpg")
process_image(input_path, output_path, blurred_background,target_width,target_height)
# Add to the processed images list
processed_images.append({
"input_path": input_path,
"output_path": output_path
})
processed_paths.add(input_path)
image_index += 1
# Remove output images for deleted input images
updated_processed_images = []
for img in processed_images:
if img['input_path'] not in current_input_paths:
# If the input image no longer exists, delete the output image
if os.path.exists(img['output_path']):
os.remove(img['output_path'])
print(f"Deleted output image {img['output_path']} because the input was removed.")
else:
updated_processed_images.append(img)
# Save the updated processed images list
save_processed_images(json_file, updated_processed_images)
if __name__ == "__main__":
input_folder = "***" # Change this to your input folder path
output_folder = "***" # Change this to your output folder path
blurred_background = True # The a blurry variant of the picture
target_height = 1200 # Target Height
target_width = 2000 # Target Width
json_file = os.path.join(output_folder, "processed_images.json")
# Create the output folder if it doesn't exist
os.makedirs(output_folder, exist_ok=True)
# Start resizing images in the input folder
resize_images_in_folder(input_folder, output_folder, json_file,blurred_background,target_height,target_width)