-
Notifications
You must be signed in to change notification settings - Fork 0
/
unsplit.py
72 lines (54 loc) · 3.04 KB
/
unsplit.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
from PIL import Image
import os
import math
def reconstruct_image(part_info_file, output_dir):
# Read the part information from the text file
with open(part_info_file, 'r') as info_file:
part_info = info_file.readlines()
# Get the first part filename and rotation from the part information
first_part_info = part_info[0].strip()
part_filename, position_info, rotation_info = first_part_info.split(',')
position = tuple(map(int, position_info.split('_')))
rotation = int(rotation_info.split(':')[1][:-1]) # Fix to remove ')' from the rotation value
part_filename = part_filename.strip('(') # Fix to remove '(' from the filename
# Open the first part image
part_path = os.path.join(output_dir, part_filename)
part_image = Image.open(part_path)
# Rotate the first part image
rotated_part = part_image.rotate(-rotation, expand=True)
# Calculate the dimensions of the rotated part
part_width, part_height = rotated_part.size
# Calculate the dimensions of the reconstructed image
image_width = part_width * (len(part_info)//int(math.sqrt(len(part_info))))
image_height = part_height * (len(part_info)//int(math.sqrt(len(part_info))))
# Create a new blank image for reconstruction
reconstructed_image = Image.new('RGB', (image_width, image_height))
# Iterate over the part positions and paste the rotated part onto the reconstructed image
for info in part_info:
info = info.strip()
part_filename, position_info, rotation_info = info.split(',')
position = tuple(map(int, position_info.split('_')))
rotation = int(rotation_info.split(':')[1][:-1]) # Fix to remove ')' from the rotation value
part_filename = part_filename.strip('(') # Fix to remove '(' from the filename
# Open the part image
part_path = os.path.join(output_dir, part_filename)
part_image = Image.open(part_path)
# Rotate the part
rotated_part = part_image.rotate(-rotation, expand=True)
# Calculate the coordinates for pasting the part onto the reconstructed image
left = position[1] * part_width
upper = position[0] * part_height
right = left + part_width
lower = upper + part_height
# Paste the rotated part onto the reconstructed image
reconstructed_image.paste(rotated_part, (left, upper, right, lower))
# Save the reconstructed image
reconstructed_image.save(os.path.join(output_dir, 'reconstructed_image.jpg'))
# Calculate the original size of the image after rotating the parts
original_width = image_width
original_height = image_height
return original_width, original_height
part_info_file = 'C:\\Users\\youruser\\Downloads\\New folder\\part_info.txt'
output_dir = 'C:\\Users\\\youruser\\Downloads\\New folder'
original_width, original_height = reconstruct_image(part_info_file, output_dir)
print(f"The original size of the image after rotating the parts is {original_width} x {original_height}.")