-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInternal.py
93 lines (67 loc) · 2.95 KB
/
Internal.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
import cv2
from PIL import Image, ImageDraw
from math import ceil
def create_sheet(PathToSpritesheet, anchor, path, order, name):
if not(PathToSpritesheet):
return
# Load the image
image = cv2.imread(PathToSpritesheet)
# Convert to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Apply threshold to create binary mask
_, threshold = cv2.threshold(gray, 1, 255, cv2.THRESH_BINARY)
# Find contours
contours, _ = cv2.findContours(threshold, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# Loop through contours and get bounding box coordinates
bounding_boxes = []
for contour in contours:
x, y, w, h = cv2.boundingRect(contour)
bounding_boxes.append((x, y, x + w, y + h)) # Format: (x1, y1, x2, y2)
# Draw bounding boxes on the original image (optional)
for x1, y1, x2, y2 in bounding_boxes:
cv2.rectangle(image, (x1, y1), (x2, y2), (0, 255, 0), 2) # Green rectangle
#print(x1, y1, x2, y2)
# Save or display the image with bounding boxes
#cv2.imwrite(f'{path}Debug.png', image)
# Caculate new image dimensions
max_x = 0
max_y = 0
for x1, y1, x2, y2 in bounding_boxes:
if (x2 - x1 ) > max_x:
max_x = x2 - x1
if (y2 - y1 ) > max_y:
max_y = y2 - y1
max_x += 3
max_y += 3
#print(max_x, max_y)
filtered_bounding_boxes = [item for item in bounding_boxes if (item[2] - item[0]) * (item[3] - item[1]) >= 200]
# Specify image dimensions
width = max_x * len(filtered_bounding_boxes)
height = max_y
# Create a new image with white background
source_image = Image.open(PathToSpritesheet)
new_image = Image.new("RGBA", (width + 2, height + 2), (0, 0, 0, 0))
if order:
sorted_bounding_boxes = sorted(filtered_bounding_boxes, key=lambda item: ((item[1] + (item[3] - item[1]) // 2), (item[0] + (item[2] - item[0]) // 2)))
else:
sorted_bounding_boxes = sorted(filtered_bounding_boxes, key=lambda item: ((item[0] + (item[2] - item[0]) // 2), (item[1] + (item[3] - item[1]) // 2)))
c = -1
for x1, y1, x2, y2 in sorted_bounding_boxes:
c += 1
adjusted_x = adjusted_y = 0
if (x2 - x1) < max_x:
match anchor:
case 2 | 5 | 8:
adjusted_x = (max_x - (x2 - x1)) // 2
case 3 | 6 | 9:
adjusted_x = max_x - (x2 - x1)
if (y2 - y1) < max_y:
match anchor:
case 4 | 5 | 6:
adjusted_y = (max_y - (y2 - y1)) // 2
case 7 | 8 | 9:
adjusted_y = max_y - (y2 - y1)
region = source_image.crop((x1, y1, x2, y2))
new_image.paste(region, (max_x * c + adjusted_x + 1, adjusted_y + 1))
# Save the new image
new_image.save(f'{path}{name}Result.png')