-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImageQuadrantBreak.py
58 lines (47 loc) · 1.68 KB
/
ImageQuadrantBreak.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
from os import rename, listdir
from PIL import Image
dir_path = path.dirname(path.realpath(__file__))
chdir(dir_path)
thesefiles = listdir()
# thesefiles = ("Shrouded Brilliance.01.jpg",)
targets = (".png",".jpg")
newformat = ".jpg"
newdir = "./Originals/"
tlen = 4
def save_quadrants(image_path, output_directory):
# Open the image
image = Image.open(image_path)
if newformat == ".jpg":
# Check if the image has an alpha channel (transparency)
has_alpha = image.mode.endswith('A')
# If the image has an alpha channel, convert it to RGB
if has_alpha:
image = image.convert('RGB')
elif newformat == ".png":
# No alteration needed
pass
# Get the size of the image
width, height = image.size
# Calculate the width and height of each quadrant
quadrant_width = width // 2
quadrant_height = height // 2
# Iterate over the quadrants
for i in range(2):
for j in range(2):
# Calculate the coordinates of the current quadrant
left = j * quadrant_width
top = i * quadrant_height
right = (j + 1) * quadrant_width
bottom = (i + 1) * quadrant_height
# Crop the image to extract the quadrant
quadrant = image.crop((left, top, right, bottom))
# Save the quadrant as a separate file
output_path = f"{output_directory}/{image_path}_{i}{j}{newformat}"
quadrant.save(output_path)
# print(f"Quadrant {i}{j} saved as {output_path}")
for f in thesefiles:
if f[-tlen:] in targets:
save_quadrants(f, "./")
m = newdir + f
rename(f, m)
print("processed", f)