Nun habe ich mir ein kleines GUI zusammengebastelt.
Mit dieser Fehlermeldung kann ich nicht viel anfangen.
Meine einziger Ansatz gienge in die Richtung der Gültigkeit von Namensräumen.
Ich glaube, ich werde den gesamten Programmablauf noch einmal überdenken, das ist mir alles etwas zu kompliziert.
Code: Alles auswählen
#!/usr/bin/python
# -*- encoding: latin-1 -*-
import sys, os, Image, string, optparse
from Tkinter import *
import tkFileDialog
__version__ = "0.0.6"
#----------------------------------------------------------
def parse_options():
"""
parses and returns commandline options
"""
#define usage-text
usage = "\n %prog [thumb_height] \n\n" + \
" Description: \n" + \
" Generates a thumbnail for every pic in a folder \n" + \
" by Sebastian Koch 2004 "
version = "%prog " + __version__
# create parser object
parser = optparse.OptionParser(usage = usage, version = version)
parser.add_option("-y", "--thumb_height",
action="store", type="int", dest="thumb_height", default = 100,
help = "(in pixel) used to calculate thumb_width by aspectratio of inputpic --> default = 100")
parser.add_option("-p", "--pic_list",
action = "store", type = "string", dest = "pic_list", default = "jpg,jpeg,jp2,png,gif,bmp,tiff",
help = "Image format that will be processed --> default = jpg, jpeg, jp2, png, gif, bmp, tiff")
parser.add_option("-d", "--directory",
action="store", type="string", dest="pic_dir", default = os.getcwd(),
help = "Path where the pictures lay to be 'thumbnaild' --> default = current working directory")
#parse options
(options, args) = parser.parse_args()
#string of pic types "jpg, png, ..." -> list of pic types ['jpg', 'png', ...]
options.pic_list = options.pic_list.split(',')
if len(sys.argv) <=1:
print "Running without Arguments by using following defaults:"
print "-->Thumb_heigt = 100, pic_list = jpg, jpeg, jp2, png, gif, bmp, tiff"
print "--> pic_dir = ", os.getcwd(), "\n"
print "For help run: ", sys.argv[0], "-h or --help \n\n"
else:
#print "height used for thumbgenerating: ", options.thumb_height
print "For help run: ", sys.argv[0], "-h or --help \n\n"
return (options, args)
#----------------------------------------------------------
def every_image_in_folder(input_folder, thumb_height, pic_list):
""" walks through a given folder and calls create_thumb() for every image
Paramters:
inputfolder = folder with original pics
thumb_height = used to call create_thumb()
"""
for this_file in os.listdir(input_folder):
if os.path.isfile(os.path.join(input_folder, this_file)) == 1:
if string.lower(os.path.splitext(this_file)[1][1:]) in pic_list:
create_thumb(os.path.join(input_folder, this_file), thumb_height)
else:
print "! " + this_file + " is not a pic! Or not defined in the pic_list"
else:
print "\n! " + os.path.join(input_folder, this_file) + " is a folder not a file"
#----------------------------------------------------------
def create_thumb(file_full_path, thumb_height):
""" creates a thumb of a pic by a given height with original aspectratio
Paramters:
file_full_path = path/to/file.ext
thumb_height = (in pixel) used to calculate thumb_width by aspectratio of inputpic
"""
no_extension = os.path.splitext(file_full_path)[0]
print "file_full_path:" , file_full_path
im = Image.open(file_full_path)
original_width, original_height = im.size
thumb_width = thumb_height * original_width / original_height
im.thumbnail((thumb_width, thumb_height), Image.ANTIALIAS)
im.save(no_extension + "-thumb.jpg", "JPEG")
print "\n" + "-"*10 + " Thumb created:"
print "height x width: " +"[Original:", original_height, "x", original_width, "] -" + " [Thumb:", thumb_height, "x", thumb_width, "]"
print "--> " + no_extension+ "-thumb.jpg "
#----------------------------------------------------------
def tk_gui(pic_dir, thumb_height, pic_list):
"""
creates a Tkinter GUI
"""
def open_it():
pic_dir = tkFileDialog.askdirectory()
btn_pic_dir["text"] = pic_dir
def create_them():
#print "pic_dir: ", pic_dir, "\n" + "thumb_height: ", thumb_height, "\n" + "pic_list: ", pic_list
every_image_in_folder(pic_dir, thumb_height, pic_list)
master = Tk()
# ---- widget creation
lbl_pic_dir = Label(master, text = "Picture Directory:")
lbl_thumb_height = Label(master, text="Thumb height:")
btn_ende = Button(master, text = "Quit")
btn_pic_dir = Button(master, text = "Picture Directory")
entry_thumb_height = Entry(master)
btn_los = Button(master, text = "Los")
# ---- basic placement
lbl_pic_dir.grid(row = 0, column = 0, sticky = "w")
lbl_thumb_height.grid(row=1, column = 0, sticky = "w")
btn_ende.grid(row = 2, column = 0, sticky = "w")
btn_pic_dir.grid(row = 0, column = 1, sticky = "ew")
entry_thumb_height.grid(row = 1, column = 1, sticky = "ew")
btn_los.grid(row = 2, column = 1, sticky = "e")
# ---- enhanced widget settings (style + Funktions)
master.title("tk-images2thumbs")
master.columnconfigure(1, weight=1)
lbl_pic_dir.config(underline = 0)
lbl_thumb_height.config(underline = 0)
btn_ende.config(relief = GROOVE, fg = "#B00000", command = master.destroy, underline = 0) #B00000 = red
btn_pic_dir.config(relief = SUNKEN, text = pic_dir, command = open_it, bg = "white")
entry_thumb_height.config(bg = "white")
entry_thumb_height.insert(0, thumb_height)
btn_los.config(relief = GROOVE, fg = "#007500", underline = 0)#007500 = green
btn_los.config(command = create_them)
master.mainloop()
#----------------------------------------------------------
def main():
#parse options and arguments
(options, args) = parse_options()
tk_gui(options.pic_dir, options.thumb_height, options.pic_list)
if __name__ == '__main__':
main()