/
main.py
executable file
·339 lines (295 loc) · 15.8 KB
/
main.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
#
# generated by wxGlade 0.6.8 on Mon Apr 27 21:03:03 2015
#
import wx
# begin wxGlade: dependencies
# end wxGlade
# begin wxGlade: extracode
# end wxGlade
class RootFrame(wx.Frame):
def __init__(self, *args, **kwds):
# begin wxGlade: RootFrame.__init__
kwds["style"] = wx.DEFAULT_FRAME_STYLE
wx.Frame.__init__(self, *args, **kwds)
self.checkbox_resize = wx.CheckBox(self, wx.ID_ANY, "Resize")
self.resize_mode_radio_box = wx.RadioBox(self, wx.ID_ANY, "Mode", choices=["Width and Height", "Long side", "Narrow side"], majorDimension=0, style=wx.RA_SPECIFY_ROWS)
self.label_resize_input_size = wx.StaticText(self, wx.ID_ANY, "Width / Height or Select Side", style=wx.ALIGN_CENTRE)
self.input_resize_width = wx.TextCtrl(self, wx.ID_ANY, "")
self.input_resize_height = wx.TextCtrl(self, wx.ID_ANY, "")
self.checkbox_aspect = wx.CheckBox(self, wx.ID_ANY, "Aspect")
self.checkbox_zoom = wx.CheckBox(self, wx.ID_ANY, "Zoom")
self.resample_mode_radio_box = wx.RadioBox(self, wx.ID_ANY, "Resample", choices=["Antialias", "Bicubic", "Bilinear"], majorDimension=0, style=wx.RA_SPECIFY_ROWS)
self.checkbox_colors = wx.CheckBox(self, wx.ID_ANY, "Colors")
self.combo_box_colors = wx.ComboBox(self, wx.ID_ANY, choices=["24bit", "8bit", "4bit", "1bit"], style=wx.CB_DROPDOWN)
self.checkbox_flip_holizontal = wx.CheckBox(self, wx.ID_ANY, "Flip Horizontal")
self.checkbox_flip_vertical = wx.CheckBox(self, wx.ID_ANY, "Flip Vertical")
self.checkbox_flip_exif = wx.CheckBox(self, wx.ID_ANY, "Rotate EXIF")
self.checkbox_grayscale = wx.CheckBox(self, wx.ID_ANY, "Grayscale")
self.checkbox_negative = wx.CheckBox(self, wx.ID_ANY, "Flip Negative")
# XXX
self.checkbox_remove_exif = wx.CheckBox(self, wx.ID_ANY, "Remove EXIF")
self.checkbox_file_overwrite = wx.CheckBox(self, wx.ID_ANY, "File Overwrite")
self.checkbox_output_dir = wx.CheckBox(self, wx.ID_ANY, "Output Dir")
self.input_output_dir = wx.TextCtrl(self, wx.ID_ANY, "convert")
self.checkbox_file_format = wx.CheckBox(self, wx.ID_ANY, "File Format")
self.combo_box_format = wx.ComboBox(self, wx.ID_ANY, choices=["Jpeg", "Png", "Gif"], style=wx.CB_DROPDOWN)
self.label_save_quality = wx.StaticText(self, wx.ID_ANY, "Save Quality")
self.slider_save_quality = wx.Slider(self, wx.ID_ANY, style=wx.SL_LABELS)
self.static_line_2 = wx.StaticLine(self, wx.ID_ANY)
self.tree_filelist = wx.ListCtrl(self, wx.ID_ANY, style=wx.LC_REPORT | wx.SUNKEN_BORDER | wx.FULL_REPAINT_ON_RESIZE)
# XXX
self.tree_filelist.InsertColumn(0, 'filename', wx.LIST_FORMAT_LEFT)
self.button_file_choose = wx.Button(self, wx.ID_ANY, "Choose")
self.button_file_convert = wx.Button(self, wx.ID_ANY, "Convert")
self.__set_properties()
self.__do_layout()
self.Bind(wx.EVT_RADIOBOX, self.ChangeResizeMode, self.resize_mode_radio_box)
self.Bind(wx.EVT_BUTTON, self.OnFileChoose, self.button_file_choose)
self.Bind(wx.EVT_BUTTON, self.OnConvert, self.button_file_convert)
# end wxGlade
def __set_properties(self):
# begin wxGlade: RootFrame.__set_properties
self.SetTitle("Pylfan")
self.SetSize((600, 320))
self.checkbox_resize.SetValue(1)
self.input_resize_width.SetValue('800')
self.input_resize_height.SetValue('600')
self.resize_mode_radio_box.SetSelection(0)
self.checkbox_aspect.SetValue(1)
self.resample_mode_radio_box.SetSelection(0)
self.combo_box_colors.SetSelection(0)
self.checkbox_output_dir.SetValue(1)
self.combo_box_format.SetSelection(0)
self.slider_save_quality.SetValue(85)
self.slider_save_quality.SetMin(1)
# end wxGlade
def __do_layout(self):
# begin wxGlade: RootFrame.__do_layout
main_sizer = wx.BoxSizer(wx.VERTICAL)
sub_sizer = wx.BoxSizer(wx.HORIZONTAL)
file_sizer = wx.BoxSizer(wx.VERTICAL)
filelist_sizer = wx.BoxSizer(wx.HORIZONTAL)
filelist_sub_sizer = wx.BoxSizer(wx.VERTICAL)
file_buttons_sizer = wx.BoxSizer(wx.HORIZONTAL)
convert_sizer = wx.BoxSizer(wx.VERTICAL)
edit_sizer = wx.BoxSizer(wx.VERTICAL)
zoom_sizer = wx.BoxSizer(wx.VERTICAL)
resize_sizer = wx.BoxSizer(wx.VERTICAL)
resize_input_sizer = wx.BoxSizer(wx.HORIZONTAL)
resize_sizer.Add(self.checkbox_resize, 0, wx.EXPAND, 0)
resize_sizer.Add(self.resize_mode_radio_box, 0, wx.EXPAND, 0)
resize_sizer.Add(self.label_resize_input_size, 0, wx.EXPAND | wx.ALIGN_CENTER_HORIZONTAL | wx.ALIGN_CENTER_VERTICAL, 0)
resize_input_sizer.Add(self.input_resize_width, 0, 0, 0)
resize_input_sizer.Add(self.input_resize_height, 0, 0, 0)
resize_sizer.Add(resize_input_sizer, 1, wx.SHAPED, 0)
edit_sizer.Add(resize_sizer, 1, wx.EXPAND, 0)
zoom_sizer.Add(self.checkbox_aspect, 0, wx.EXPAND, 0)
zoom_sizer.Add(self.checkbox_zoom, 0, wx.EXPAND, 0)
zoom_sizer.Add(self.resample_mode_radio_box, 0, wx.EXPAND, 0)
edit_sizer.Add(zoom_sizer, 1, wx.EXPAND, 0)
sub_sizer.Add(edit_sizer, 1, 0, 0)
convert_sizer.Add(self.checkbox_colors, 0, wx.EXPAND, 0)
convert_sizer.Add(self.combo_box_colors, 0, 0, 0)
convert_sizer.Add(self.checkbox_flip_holizontal, 0, wx.EXPAND, 0)
convert_sizer.Add(self.checkbox_flip_vertical, 0, wx.EXPAND, 0)
convert_sizer.Add(self.checkbox_flip_exif, 0, wx.EXPAND, 0)
convert_sizer.Add(self.checkbox_grayscale, 0, wx.EXPAND, 0)
convert_sizer.Add(self.checkbox_negative, 0, wx.EXPAND, 0)
convert_sizer.Add(self.checkbox_remove_exif, 0, wx.EXPAND, 0)
sub_sizer.Add(convert_sizer, 1, 0, 0)
file_sizer.Add(self.checkbox_file_overwrite, 0, wx.EXPAND, 0)
file_sizer.Add(self.checkbox_output_dir, 0, wx.EXPAND, 0)
file_sizer.Add(self.input_output_dir, 0, 0, 0)
file_sizer.Add(self.checkbox_file_format, 0, wx.EXPAND, 0)
file_sizer.Add(self.combo_box_format, 0, 0, 0)
file_sizer.Add(self.label_save_quality, 0, wx.EXPAND, 0)
file_sizer.Add(self.slider_save_quality, 0, wx.EXPAND, 0)
file_sizer.Add(self.static_line_2, 0, wx.EXPAND, 0)
filelist_sub_sizer.Add(self.tree_filelist, 1, wx.EXPAND, 0)
file_buttons_sizer.Add(self.button_file_choose, 0, 0, 0)
file_buttons_sizer.Add(self.button_file_convert, 0, 0, 0)
filelist_sub_sizer.Add(file_buttons_sizer, 1, 0, 0)
filelist_sizer.Add(filelist_sub_sizer, 1, 0, 0)
file_sizer.Add(filelist_sizer, 1, wx.EXPAND, 0)
sub_sizer.Add(file_sizer, 1, 0, 0)
main_sizer.Add(sub_sizer, 1, wx.EXPAND, 0)
self.SetSizer(main_sizer)
self.Layout()
# end wxGlade
def ChangeResizeMode(self, event): # wxGlade: RootFrame.<event_handler>
selection = self.resize_mode_radio_box.GetSelection()
if 1 == selection or 2 == selection:
self.input_resize_height.Disable()
else:
self.input_resize_height.Enable()
def OnFileChoose(self, event): # wxGlade: RootFrame.<event_handler>
dialog = wx.FileDialog(self, 'Choose files', './', '', 'Image files (*.gif;*.png;*.jpg)|*.gif;*.png;*.jpg;*.jpeg;*.GIF;*.PNG;*.JPG;*.JPEG;', wx.FD_MULTIPLE)
if dialog.ShowModal() == wx.ID_OK:
self.convert_files = dialog.GetFilenames()
self.convert_file_dir = dialog.GetDirectory()
else:
dialog.Destroy()
return
dialog.Destroy()
for i, convert_file in enumerate(self.convert_files):
self.tree_filelist.InsertStringItem(i, convert_file)
def OnConvert(self, event): # wxGlade: RootFrame.<event_handler>
import PIL.ImageOps
import Image
import imghdr
import shutil
import glob
import time
import os
import sys
# get select options
is_resize = self.checkbox_resize.IsChecked() # type: bool
if is_resize:
resize_mode = self.resize_mode_radio_box.GetSelection() # type: int
width = int(self.input_resize_width.GetValue()) # type: int
if resize_mode == 0:
height = int(self.input_resize_height.GetValue()) # type: int
else:
height = 1 # type: int
is_aspect = self.checkbox_aspect.IsChecked() # type: bool
is_zoom = self.checkbox_zoom.IsChecked() # type: bool
resample_mode = self.resample_mode_radio_box.GetSelection() # type: int
is_colors = self.checkbox_colors.IsChecked() # type: bool
if is_colors:
color_mode = self.combo_box_colors.GetSelection() # type: int
is_flip_horizontal = self.checkbox_flip_holizontal.IsChecked() # type: bool
is_flip_vertical = self.checkbox_flip_vertical.IsChecked() # type: bool
is_flip_exif = self.checkbox_flip_exif.IsChecked() # type: bool
is_grayscale = self.checkbox_grayscale.IsChecked() # type: bool
is_flip_negative = self.checkbox_negative.IsChecked() # type: bool
is_remove_exif = self.checkbox_remove_exif.IsChecked() # type: bool
is_file_overwrite = self.checkbox_file_overwrite.IsChecked() # type: bool
is_file_output_dir = self.checkbox_output_dir.IsChecked() # type: bool
if is_file_output_dir:
file_output_dir = self.input_output_dir.GetValue() # type: int
is_file_format = self.checkbox_file_format.IsChecked() # type: bool
if is_file_format:
file_format = self.combo_box_format.GetSelection() # type: int
save_quality = self.slider_save_quality.GetValue() # type: int
# end get select options
# CONSTANT
RESAMPLE_MODES = [Image.ANTIALIAS, Image.BICUBIC, Image.BILINEAR] # type: List[int]
FORMATS = {
0: ['JPEG', 'jpg'],
1: ['PNG', 'png'],
2: ['GIF', 'gif'],
} # type: Dict[int, List[str, str]]
TMP_DIR = os.path.join('/tmp', 'pylfan-'+str(time.time())) # type: str
CONVERT_IMAGE = {
1: lambda img: img,
2: lambda img: img.transpose(Image.FLIP_LEFT_RIGHT),
3: lambda img: img.transpose(Image.ROTATE_180),
4: lambda img: img.transpose(Image.FLIP_TOP_BOTTOM),
5: lambda img: img.transpose(Image.FLIP_LEFT_RIGHT).transpose(Image.ROTATE_90),
6: lambda img: img.transpose(Image.ROTATE_270),
7: lambda img: img.transpose(Image.FLIP_LEFT_RIGHT).transpose(Image.ROTATE_270),
8: lambda img: img.transpose(Image.ROTATE_90),
} # type: Dict[int: func]
# end CONSTANT
# INITIALIZE
os.mkdir(TMP_DIR)
# end INITIALIZE
for convert_filename in self.convert_files:
print convert_filename
convert_file_path = os.path.join(self.convert_file_dir, convert_filename) # type: str
shutil.copy(convert_file_path, TMP_DIR)
image = Image.open(os.path.join(TMP_DIR, convert_filename)) # type: Image
image_type = imghdr.what(convert_file_path) # type: str
if image_type == 'jpeg' and 'exif' in image.info:
exif_str = image.info['exif'] # type: str
else:
exif_str = '' # type: str
if image_type == 'jpeg' and is_flip_exif:
exif = image._getexif() # type: Dict[Blob: Any]
if exif is not None:
orientation = exif.get(0x112, 1) # type: int
image = CONVERT_IMAGE[orientation](image) # type: Image
if is_resize:
if (width >= image.size[0] and height >= image.size[1]) and not is_zoom:
pass
else:
if resize_mode == 1 or resize_mode == 2:
height = width
w_percent = (width/float(image.size[0])) # type: float
h_percent = (height/float(image.size[1])) # type: float
w_new_size = int((float(image.size[0]) * float(h_percent))) # type: int
h_new_size = int((float(image.size[1]) * float(w_percent))) # type: int
# width and height or long side
if resize_mode == 0 or resize_mode == 1:
if is_aspect or resize_mode == 1:
if w_percent <= h_percent:
image = image.resize((width, h_new_size), RESAMPLE_MODES[resample_mode]) # type: Image
else:
image = image.resize((w_new_size, height), RESAMPLE_MODES[resample_mode]) # type: Image
else:
image = image.resize((height, width), RESAMPLE_MODES[resample_mode]) # type: Image
# narrow side
elif resize_mode == 2:
if w_percent >= h_percent:
image = image.resize((width, h_new_size), RESAMPLE_MODES[resample_mode]) # type: Image
else:
image = image.resize((w_new_size, width), RESAMPLE_MODES[resample_mode]) # type: Image
if is_colors:
if color_mode == 0:
pass
elif color_mode == 1:
image = image.point(lambda point: int(point/32)*32) # type: Image
elif color_mode == 2:
image = image.point(lambda point: int(point/64)*64) # type: Image
elif color_mode == 3:
image = image.convert('L').point(lambda point: 0 if point < 127 else 255) # type: Image
if is_flip_horizontal:
image = image.transpose(Image.FLIP_LEFT_RIGHT) # type: Image
if is_flip_vertical:
image = image.transpose(Image.FLIP_TOP_BOTTOM) # type: Image
if is_grayscale:
image = image.convert('L') # type: Image
if is_flip_negative:
image = image.point(lambda point: 255 - point) # type: Image
if is_remove_exif:
exif_str = '' # type: str
save_options = {
'quality': save_quality,
} # type: Dict[str, Any]
if image_type == 'jpeg':
save_options['exif'] = exif_str # type: str
if is_file_format:
new_format = FORMATS[file_format][1] # type: str
if new_format != 'jpg':
del save_options['exif']
new_filename = '{0}.{1}'.format('.'.join(convert_filename.split('.')[:-1]), new_format) # type: str
image.save(os.path.join(TMP_DIR, new_filename), FORMATS[file_format][0], **save_options)
if new_filename != convert_filename:
os.remove(os.path.join(TMP_DIR, convert_filename))
else:
image.save(os.path.join(TMP_DIR, convert_filename), **save_options)
if is_file_output_dir:
move_dir = os.path.join(self.convert_file_dir, file_output_dir) # type: str
if not os.path.exists(move_dir):
os.mkdir(move_dir)
elif is_file_overwrite:
move_dir = file_output_dir
else:
print 'make file an {0} directory'.format(TMP_DIR)
sys.exit()
for file_path in glob.glob(os.path.join(TMP_DIR, '*')):
shutil.copy(file_path, move_dir)
# end of class RootFrame
class PylfanApp(wx.App):
def OnInit(self):
wx.InitAllImageHandlers()
root_frame = RootFrame(None, wx.ID_ANY, "")
self.SetTopWindow(root_frame)
root_frame.Show()
return 1
# end of class PylfanApp
if __name__ == "__main__":
Pylfan = PylfanApp(0)
Pylfan.MainLoop()