-
Notifications
You must be signed in to change notification settings - Fork 0
/
Scale2x.py
240 lines (191 loc) · 8.21 KB
/
Scale2x.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#!/usr/bin/env python3
"""
Scale2x aka AdvMAME2x bitmap image scaling using Python only, merged command line and GUI versions.
Created by: Ilya Razmanov (mailto:ilyarazmanov@gmail.com)
aka Ilyich the Toad (mailto:amphisoft@gmail.com)
Usage:
python Scale2x.py source.png result.png - rescales source.png and writes result.png
python Scale2x.py source.png - rescales source.png and overwrites source.png
python Scale2x.py - starts GUI for selecting source and result
History:
2024.05.11 Initial release of merged GUI and CLI versions.
2024.05.14 Linked with IncSrc and IncScaleNx version 2024.05.14, data exchange format changed to incompatible with previous versions.
24.08.01 Complete I/O change, excluding IncSrc in favour of pnglpng.
24.10.01 Internal restructure, imports change.
24.12.03 PPM and PGM support added
"""
__author__ = 'Ilya Razmanov'
__copyright__ = '(c) 2024 Ilya Razmanov'
__credits__ = 'Ilya Razmanov'
__license__ = 'unlicense'
__version__ = '24.12.03'
__maintainer__ = 'Ilya Razmanov'
__email__ = 'ilyarazmanov@gmail.com'
__status__ = 'Production'
from pathlib import Path
from sys import argv
import pnglpng # PNG-list-PNG joint, uses PyPNG
import pnmlpnm # PNM-list-PNM
from scalenx import scale2x # Scale2x and Scale3x from: https://github.com/Dnyarri/PixelArtScaling
""" ╔═════════════════════╗
║ commandline variant ║
╚═════════════════════╝ """
def cli(Rez, Dvo):
"""
Command line variant of Scale2x. Input - source and result PNG filenames.
"""
# --------------------------------------------------------------
# Open source file
if Path(Rez).suffix == '.png':
# Reading image as list
X, Y, Z, maxcolors, ImageAsListListList, info = pnglpng.png2list(Rez)
elif (Path(Rez).suffix == '.ppm') or (Path(Rez).suffix == '.pgm'):
# Reading image as list
X, Y, Z, maxcolors, ImageAsListListList = pnmlpnm.pnm2list(Rez)
# Creating dummy info
info = {}
# Fixing color mode. So far pnglpng does not make any assumptions on that; guess it must be fixed later.
if Z < 3:
info['greyscale'] = True
else:
info['greyscale'] = False
if maxcolors > 255:
info['bitdepth'] = 16
# Scaling list to 2x image list
EPXImage = scale2x(ImageAsListListList)
# --------------------------------------------------------------
# Fixing resolution to match original print size.
# If no pHYs found in original, 96 ppi is assumed as original value.
if 'physical' in info:
res = info['physical'] # Reading resolution as tuple
x_pixels_per_unit = res[0]
y_pixels_per_unit = res[1]
unit_is_meter = res[2]
else:
x_pixels_per_unit = 3780 # 3780 px/meter = 96 px/inch, 2834 px/meter = 72 px/inch
y_pixels_per_unit = 3780 # 3780 px/meter = 96 px/inch, 2834 px/meter = 72 px/inch
unit_is_meter = True
x_pixels_per_unit = 2 * x_pixels_per_unit # Double resolution to keep print size
y_pixels_per_unit = 2 * y_pixels_per_unit # Double resolution to keep print size
info['physical'] = [x_pixels_per_unit, y_pixels_per_unit, unit_is_meter]
# Resolution changed
# --------------------------------------------------------------
# Explicitly setting compression
info['compression'] = 9
if Path(Dvo).suffix == '.png':
pnglpng.list2png(Dvo, EPXImage, info)
elif (Path(Dvo).suffix == '.ppm') or (Path(Dvo).suffix == '.pgm'):
pnmlpnm.list2pnm(Dvo, EPXImage, maxcolors)
return None # end of CLI variant
""" ╔═════════════╗
║ GUI variant ║
╚═════════════╝ """
def gui():
"""
GUI variant of Scale2x, based on tkinter.
"""
from tkinter import Label, Tk, filedialog
# Creating dialog
iconpath = Path(__file__).resolve().parent / '2x.ico'
iconname = str(iconpath)
useicon = iconpath.exists() # Check if icon file really exist. If False, it will not be used later.
sortir = Tk()
sortir.title('Scale2x')
if useicon:
sortir.iconbitmap(iconname) # Replacement for simple sortir.iconbitmap('2xGUI.ico') - ugly but stable
sortir.geometry('+200+100')
zanyato = Label(sortir, text='Starting...', font=('arial', 14), padx=14, pady=10, justify='center')
zanyato.pack()
sortir.withdraw()
# Main dialog created and hidden
# Open source file
sourcefilename = filedialog.askopenfilename(title='Open image file to reScale2x', filetypes=[('Supported formats', '.png .ppm .pgm'), ('PNG', '.png'), ('PNM', '.ppm .pgm')])
if sourcefilename == '':
return None
# Updating dialog
sortir.deiconify()
zanyato.config(text=f'Reading {sourcefilename}...')
sortir.update()
sortir.update_idletasks()
# Dialog shown and updated
if Path(sourcefilename).suffix == '.png':
# Reading image as list
X, Y, Z, maxcolors, ImageAsListListList, info = pnglpng.png2list(sourcefilename)
elif (Path(sourcefilename).suffix == '.ppm') or (Path(sourcefilename).suffix == '.pgm'):
# Reading image as list
X, Y, Z, maxcolors, ImageAsListListList = pnmlpnm.pnm2list(sourcefilename)
# Creating dummy info
info = {}
# Fixing color mode. So far pnglpng does not make any assumptions on that; guess it must be fixed later.
if Z < 3:
info['greyscale'] = True
else:
info['greyscale'] = False
if maxcolors > 255:
info['bitdepth'] = 16
# Updating dialog
zanyato.config(text=f'Scaling {sourcefilename}...')
sortir.update()
sortir.update_idletasks()
# Scaling list to 2x image list
EPXImage = scale2x(ImageAsListListList)
# --------------------------------------------------------------
# Fixing resolution to match original print size.
# If no pHYs found in original, 96 ppi is assumed as original value.
if 'physical' in info:
res = info['physical'] # Reading resolution as tuple
x_pixels_per_unit = res[0]
y_pixels_per_unit = res[1]
unit_is_meter = res[2]
else:
x_pixels_per_unit = 3780 # 3780 px/meter = 96 px/inch, 2834 px/meter = 72 px/inch
y_pixels_per_unit = 3780 # 3780 px/meter = 96 px/inch, 2834 px/meter = 72 px/inch
unit_is_meter = True
x_pixels_per_unit = 2 * x_pixels_per_unit # Double resolution to keep print size
y_pixels_per_unit = 2 * y_pixels_per_unit # Double resolution to keep print size
info['physical'] = [x_pixels_per_unit, y_pixels_per_unit, unit_is_meter]
# Resolution changed
# --------------------------------------------------------------
# Explicitly setting compression
info['compression'] = 9
# Hiding dialog
sortir.withdraw()
# Adjusting "Save to" formats to be displayed according to bitdepth
if Z < 3:
format = [('PNG', '.png'), ('Portable grey map', '.pgm')]
else:
format = [('PNG', '.png'), ('Portable pixel map', '.ppm')]
# Open export file
resultfilename = filedialog.asksaveasfilename(
title='Save Scale2x image file',
filetypes=format,
defaultextension=('PNG file', '.png'),
)
if resultfilename == '':
return None
# Updating dialog
sortir.deiconify()
zanyato.config(text=f'Saving {resultfilename}...')
sortir.update()
sortir.update_idletasks()
if Path(resultfilename).suffix == '.png':
pnglpng.list2png(resultfilename, EPXImage, info)
elif (Path(resultfilename).suffix == '.ppm') or (Path(resultfilename).suffix == '.pgm'):
pnmlpnm.list2pnm(resultfilename, EPXImage, maxcolors)
# Destroying dialog
sortir.destroy()
sortir.mainloop()
return None # end of GUI variant
# --------------------------------------------------------------
if __name__ == '__main__':
# Taking user input
if len(argv) == 2:
Rez = argv[1]
Dvo = argv[1] # will overwrite source file
cli(Rez, Dvo)
elif len(argv) == 3:
Rez = argv[1]
Dvo = argv[2] # will write new file
cli(Rez, Dvo)
else:
gui() # will open GUI