-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathswappy.py
149 lines (121 loc) · 5.28 KB
/
swappy.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
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
# swappy - my csv based color palette swapper
# swappy takes arguments for a source folder, a csv file and optionally a target folder
# if a target folder is not specified the csv file will be generated from the source folder's color palette
# if a target folder is specified the csv file will be used to swap the color palette of the images in the source folder and save them in the target folder
# the csv file will consist of the columns SourceR, SourceG, SourceB, SourceA, TargetR, TargetG, TargetB, TargetA, Notes
import csv
import os
import sys
import pygame
help_message = """
Swappy - Jared's color palette swapper created one lazy Saturday afternoon
Usage: swappy.py <source_folder> <csv_file> [target_folder]
Required arguments:
source_folder: the folder containing the images to swap the color palette of
csv_file: the csv file for color palette swapping
Optional arguments:
target_folder: the folder to save the images with the swapped color palette
When the target_folder is not specified, the csv file will be generated from
the source_folder's color palette.
When the target_folder is specified, the csv file will be used to swap the
color palette of the images in the source_folder and save them in the target_folder.
"""
def export_palette(path_source_folder, path_csv):
# iterate through every png image in the source folder and extract the entire unique color palette
# once extracted write the sorted palette to a csv file
files = os.listdir(path_source_folder)
palette = set()
for file in files:
if str(file).lower().endswith(".png"):
image = pygame.image.load(os.path.join(path_source_folder, file))
for x in range(image.get_width()):
for y in range(image.get_height()):
color = image.get_at((x, y))
# create the rgba hex code as an 8 digit string
color_hex = "{:02x}{:02x}{:02x}{:02x}".format(
color.r, color.g, color.b, color.a
)
palette.add(color_hex)
with open(path_csv, "w", newline="") as csvfile:
writer = csv.writer(csvfile)
writer.writerow(
[
"SourceR",
"SourceG",
"SourceB",
"SourceA",
"TargetR",
"TargetG",
"TargetB",
"TargetA",
"Notes",
]
)
for color in sorted(palette):
row = []
for _ in range(2):
for i in range(0, len(color), 2):
row.append("{:02x}".format(int(color[i : i + 2], 16)))
row.append("")
writer.writerow(row)
def swap_palette(path_source_folder, path_csv, path_target_folder):
# Read the CSV file and create a mapping of source colors to target colors
color_map = {}
with open(path_csv, "r") as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
source_color = (
int(row["SourceR"], 16),
int(row["SourceG"], 16),
int(row["SourceB"], 16),
int(row["SourceA"], 16),
)
target_color = (
int(row["TargetR"], 16),
int(row["TargetG"], 16),
int(row["TargetB"], 16),
int(row["TargetA"], 16),
)
color_map[source_color] = target_color
# Iterate through each image in the source folder and swap the colors
files = os.listdir(path_source_folder)
for file in files:
if str(file).lower().endswith(".png"):
image = pygame.image.load(os.path.join(path_source_folder, file))
for x in range(image.get_width()):
for y in range(image.get_height()):
color = image.get_at((x, y))
color_tuple = (color.r, color.g, color.b, color.a)
if color_tuple in color_map:
new_color = color_map[color_tuple]
image.set_at((x, y), new_color)
pygame.image.save(image, os.path.join(path_target_folder, file))
def help():
print(help_message)
sys.exit(1)
def main():
# parse arguments and verify the folder(s) and file(s) exist
# if the target folder is not specified generate the csv file from the source folder
# if the target folder is specified swap the color palette of the images in the source folder and save them in the target folder
if not (len(sys.argv) == 3 or len(sys.argv) == 4):
help()
source_folder = sys.argv[1]
csv_file = sys.argv[2]
target_folder = sys.argv[3] if len(sys.argv) == 4 else None
if not os.path.exists(source_folder):
print("Source folder does not exist")
sys.exit(1)
if target_folder and not os.path.exists(target_folder):
print("Target folder does not exist")
sys.exit(1)
# check that the csv file exists as we need to read it
if not os.path.exists(csv_file):
print("CSV file does not exist")
sys.exit(1)
if len(sys.argv) == 3:
export_palette(source_folder, csv_file)
else:
target_folder = sys.argv[3]
swap_palette(source_folder, csv_file, target_folder)
if __name__ == "__main__":
main()