usr bin env python coding utf-8 import os sys try import pygtk pygtk r

  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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os, sys
try:
import pygtk
pygtk.requare("2.0")
except:
pass
try:
import gtk
import gtk.glade
except:
sys.exit(1)
class image_completer:
def __init__(self):
self.gladefile = "inface.glade"
self.wTree = gtk.glade.XML(self.gladefile)
dic = { "on_window_main_destroy": gtk.main_quit,
"on_rename_togglebutton_toggled": self.button_rename_toggled,
"on_web_togglebutton_toggled": self.button_web_toggled,
"on_dirchooser_file_set": self.on_dirchooser_file_set,
"on_rename_button_do_clicked": self.on_rename_button_do_clicked,
"on_copyonly_checkbutton_toggled": self.on_copyonly_checkbutton_toggled,
"on_web_button_do_clicked": self.on_web_button_do_clicked }
self.wTree.signal_autoconnect(dic)
self.app_path = os.path.dirname(os.path.realpath(__file__))
self.wTree.get_widget("copytype_combobox").set_active(0)
def button_rename_toggled(self, widget):
frame_rename = self.wTree.get_widget("rename_prop_area")
if not widget.get_active():
frame_rename.set_property('visible',False)
return
self.wTree.get_widget("rename_cnt_start_splin").set_range(1,10001-len(self.files_list))
self.wTree.get_widget("web_togglebutton").set_active(False)
frame_rename.set_property('visible',True)
def button_web_toggled(self, widget):
frame_web = self.wTree.get_widget("web_prop_area")
if not widget.get_active():
frame_web.set_property('visible',False)
return
self.wTree.get_widget("rename_togglebutton").set_active(False)
frame_web.set_property('visible',True)
def on_dirchooser_file_set(self, file):
curr_dir = file.get_current_folder()
info_label = self.wTree.get_widget("dir_info_label")
files_list_all = os.listdir(curr_dir)
# get JPEG files list
files_list_jpeg = [filename for filename in files_list_all if os.path.splitext(filename.lower())[1] == '.jpg']
if not len(files_list_jpeg):
info_label.set_text(u"В каталоге нет ни одного JPEG изображения")
self.wTree.get_widget("dir_info_image").set_from_stock(gtk.STOCK_DIALOG_ERROR, gtk.ICON_SIZE_BUTTON)
self.wTree.get_widget("dir_info_area").set_property('visible', True)
self.wTree.get_widget("toggle_buttons_area").set_property('visible', False)
self.wTree.get_widget("rename_togglebutton").set_active(False)
self.wTree.get_widget("web_togglebutton").set_active(False)
return
else:
message = u"Изображений: %d" % len(files_list_jpeg)
if len(files_list_all) > len(files_list_jpeg):
message += u"\n(и не JPEG файлов: %d)" % (len(files_list_all)-len(files_list_jpeg))
info_label.set_text(message)
self.wTree.get_widget("dir_info_image").set_from_stock(gtk.STOCK_INFO, gtk.ICON_SIZE_BUTTON)
self.img_dir = curr_dir
self.files_list = files_list_jpeg
self.files_list.sort()
self.wTree.get_widget("dir_info_area").set_property('visible', True)
self.wTree.get_widget("toggle_buttons_area").set_property('visible', True)
def on_rename_button_do_clicked(self, widget):
import shutil
os.chdir(self.img_dir)
name_nmb = self.wTree.get_widget("rename_cnt_start_splin").get_value_as_int()
os.mkdir("tmp")
for filename in self.files_list:
shutil.move(filename, "tmp/%s" % filename)
for i, filename in enumerate(self.files_list):
pref = ""
if name_nmb < 10: pref = "000"
elif name_nmb < 100: pref = "00"
elif name_nmb < 1000: pref = "0"
new_filename = "%s%d.jpg" % (pref, name_nmb)
shutil.move("tmp/%s" % filename, new_filename)
self.files_list[i] = new_filename
name_nmb += 1
os.rmdir("tmp")
self.wTree.get_widget("rename_togglebutton").set_active(False)
def on_copyonly_checkbutton_toggled(self, widget):
sens = not widget.get_active()
self.wTree.get_widget("web_w_label1").set_sensitive(sens)
self.wTree.get_widget("web_w_label2").set_sensitive(sens)
self.wTree.get_widget("web_h_label1").set_sensitive(sens)
self.wTree.get_widget("web_h_label2").set_sensitive(sens)
self.wTree.get_widget("width_spinbutton").set_sensitive(sens)
self.wTree.get_widget("height_spinbutton").set_sensitive(sens)
if sens: self.wTree.get_widget("quality_spinbutton").set_value(85)
else: self.wTree.get_widget("quality_spinbutton").set_value(95)
def on_web_button_do_clicked(self, widget):
import shutil
from PIL import Image
# prepare path's
if self.wTree.get_widget("copytype_combobox").get_active() == 0: copy_type = "mini"
else: copy_type = "full"
copy_file = os.path.join(self.app_path, "media/copy_%s.png" % copy_type)
split_img_dir = os.path.split(self.img_dir)
web_dir = os.path.join(split_img_dir[0], split_img_dir[1]+"-web")
os.chdir(self.img_dir)
# remove and create web dir
if os.path.exists(web_dir): shutil.rmtree(web_dir)
os.mkdir(web_dir)
# work
img_copy = Image.open(copy_file)
for filename in self.files_list:
img_orig = Image.open(filename)
if not self.wTree.get_widget("copyonly_checkbutton").get_active():
img_orig.thumbnail(
(self.wTree.get_widget("width_spinbutton").get_value_as_int(),
self.wTree.get_widget("height_spinbutton").get_value_as_int()),
Image.ANTIALIAS)
img_orig.paste(img_copy,
(
0, #left
img_orig.size[1]-img_copy.size[1], #upper
img_copy.size[0], #right
img_orig.size[1] #lower
),
img_copy)
img_orig.save(os.path.join(web_dir, filename))
self.wTree.get_widget("web_togglebutton").set_active(False)
if __name__ == "__main__":
hwg = image_completer()
gtk.main()