Skip to content

Commit af8b3ad

Browse files
committed
Now converting bmps to pngs.
1 parent 5042ccd commit af8b3ad

File tree

4 files changed

+51
-8
lines changed

4 files changed

+51
-8
lines changed

Media/palette.bmp

1.3 KB
Binary file not shown.

Python/convert.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from Python import regex_rules
88
from Python import zips as zips_py
99
from Python import update_progress
10+
from Python import palette
1011

1112

1213
warnings_file_name = "Warnings.json"
@@ -26,7 +27,7 @@ def resource_path(relative_path):
2627

2728

2829
# If an exe executes this program then sys is frozen.
29-
output_folder = ".." if getattr(sys, 'frozen', False) else "Output"
30+
output_folder_path = ".." if getattr(sys, 'frozen', False) else "Output"
3031

3132

3233
def load_conversion_and_warning_rules():
@@ -57,7 +58,7 @@ def check_github_button_clicked_and_exit(clicked_github_button):
5758

5859

5960
def convert():
60-
global output_folder, warnings
61+
global output_folder_path, warnings
6162

6263
print("") # Only prints a newline.
6364

@@ -76,12 +77,12 @@ def convert():
7677

7778
if len(mod_subfolder_parts) > 0 and mod_subfolder_parts[0].endswith(".rte"):
7879
try_print_mod_name(mod_subfolder_parts, mod_subfolder)
79-
output_subfolder = os.path.join(output_folder, mod_subfolder)
80+
output_subfolder = os.path.join(output_folder_path, mod_subfolder)
8081
create_folder(input_subfolder_path, output_subfolder)
81-
process_files(input_subfiles, input_subfolder_path, output_subfolder, input_folder_path)
82+
process_files(input_subfiles, input_subfolder_path, output_subfolder, input_folder_path, output_folder_path)
8283

8384
if cfg.sg.user_settings_get_entry("output_zips"):
84-
zips_py.create_zips(input_folder_path, output_folder)
85+
zips_py.create_zips(input_folder_path, output_folder_path)
8586

8687
if len(warnings) > 0:
8788
warnings_popup()
@@ -115,20 +116,25 @@ def create_folder(input_subfolder_path, output_subfolder):
115116
pass
116117

117118

118-
def process_files(input_subfiles, input_subfolder_path, output_subfolder, input_folder_path):
119+
def process_files(input_subfiles, input_subfolder_path, output_subfolder, input_folder_path, output_folder_path):
119120
for full_filename in input_subfiles:
120121
filename, file_extension = os.path.splitext(full_filename)
121122

122-
input_file_path = os.path.join(input_subfolder_path, full_filename)
123+
input_file_path = os.path.join(input_subfolder_path, full_filename)
124+
123125
output_file_path = os.path.join(output_subfolder, full_filename)
124126

127+
if palette.is_input_image(full_filename):
128+
palette.process_image(full_filename, input_file_path, output_file_path)
129+
125130
if full_filename == "desktop.ini":
126131
continue
127132

128133
if file_extension in (".ini", ".lua"):
129134
create_converted_file(input_file_path, output_file_path, input_folder_path)
130135
else:
131-
shutil.copyfile(input_file_path, output_file_path)
136+
if not palette.is_input_image(full_filename):
137+
shutil.copyfile(input_file_path, output_file_path)
132138

133139
update_progress.increment_progress()
134140

Python/palette.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import os
2+
3+
from PIL import Image
4+
import cv2
5+
6+
7+
palette = Image.open(os.path.join("Media", "palette.bmp")).getpalette()
8+
9+
10+
def is_input_image(full_filename):
11+
return full_filename.lower().endswith((".bmp", ".png", ".jpg", ".jpeg")) and full_filename.lower() != "palette.bmp"
12+
13+
14+
def process_image(full_filename, input_image_path, output_image_path):
15+
global palette
16+
17+
# A bmp threw an "Unsupported BMP compression (1)" with Image.open(), so we open it in cv2 first.
18+
# cv2 solution: https://stackoverflow.com/a/52416250/13279557
19+
# cv2 to PIL: https://stackoverflow.com/a/43234001/13279557
20+
# old_img = Image.fromarray(cv2.imread(input_image_path)) # TODO: This may work just as well as the line below for all cases.
21+
old_img = Image.fromarray(cv2.cvtColor(cv2.imread(input_image_path), cv2.COLOR_BGR2RGB))
22+
23+
scale = 1
24+
old_img = old_img.resize((int(old_img.width * scale), int(old_img.height * scale)))
25+
26+
# putpalette() always expects 256 * 3 ints.
27+
for k in range(256 - int(len(palette) / 3)):
28+
for j in range(3):
29+
palette.append(palette[j])
30+
31+
palette_img = Image.new('P', (1, 1))
32+
palette_img.putpalette(palette)
33+
new_img = old_img.convert(mode="RGB").quantize(palette=palette_img, dither=False)
34+
35+
new_img.save(os.path.splitext(output_image_path)[0] + ".png")

requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,6 @@ PySimpleGUI
22
playsound
33
pyinstaller
44
jsoncomment
5+
Pillow
6+
opencv-python
57
# python3 needs sudo apt-get install python3-tk

0 commit comments

Comments
 (0)