Simple GUI to allow converting images from one format to another. Available formats are: .gif .jpg .png .tif .bmp Uses PIL.
This is short and direct enhancement from recipe http://code.activestate.com/recipes/180801-convert-image-format. Two new features added:
- Optional deletion of the original image files.
- Optional recursive file selection. Implemented using http://code.activestate.com/recipes/577230-file-path-generator-from-path-patterns/
note This assumes recipe/577230 is located in filePattern.py (see first import below)
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 | ## {{{ http://code.activestate.com/recipes/180801/ (r4)
#!/usr/bin/env python
"""Program for converting image files' format. Converts one file at a time or all
files (of a selected format) in a directory at once.
Converted files have same basename as original files.
-- Added: recursive mode will manage files in subfolders too
-- Added: possible to delete original files
-- REQUIRES http://code.activestate.com/recipes/577230/
Uses workaround: askdirectory() does not allow choosing
a new dir, so asksaveasfilename() is used instead, and
the filename is discarded, keeping just the directory.
-- hence you need create an empty image in recursive mode in order to select a root folder not containing images !
"""
import filePattern # this file should contain http://code.activestate.com/recipes/577230/
import os, os.path, string, sys
from Tkinter import *
from tkFileDialog import *
#import Image
from PIL import Image
openfile = '' # full pathname: dir(abs) + root + ext
indir = ''
outdir = ''
def getinfilename():
global openfile, indir
ftypes=(('Bitmap Images', '*.bmp'),
('Jpeg Images', '*.jpg'),
('Png Images', '*.png'),
('Tiff Images', '*.tif'),
('Gif Images', '*.gif'),
("All files", "*"))
if indir:
openfile = askopenfilename(initialdir=indir,
filetypes=ftypes)
else:
openfile = askopenfilename(filetypes=ftypes)
if openfile:
indir = os.path.dirname(openfile)
def getoutdirname():
global indir, outdir
if openfile:
indir = os.path.dirname(openfile)
outfile = asksaveasfilename(initialdir=indir,
initialfile='foo')
else:
outfile = asksaveasfilename(initialfile='foo')
outdir = os.path.dirname(outfile)
def save(infile, outfile):
# should maybe treat transparency apart for some image format ?
if infile != outfile:
try:
Image.open(infile).save(outfile)
return True
except IOError:
print "Cannot convert", infile
return False
def selector():
path, file = os.path.split(openfile)
base, ext = os.path.splitext(file)
if varRec.get():
G= filePattern._paths_from_path_patterns([indir], includes=["*%s"%ext])
l=[g for g in G]
return l
else:
if var.get():
ls = os.listdir(indir)
filelist = []
for f in ls:
if os.path.splitext(f)[1] == ext:
filelist.append(f)
else:
filelist = [file]
return filelist
def convert():
newext = frmt.get()
filelist= selector()
should_delete= varDel.get()
done= "Done. Deleted" if should_delete else "Done. Created"
for f in filelist:
infile = os.path.join(indir, f)
ofile = os.path.join(outdir, f)
outfile = os.path.splitext(ofile)[0] + newext
succeed= save(infile, outfile)
if should_delete and succeed:
os.remove(infile)
done= "%s=%s" % (done,len(filelist))
win = Toplevel(root)
Button(win, text=done, command=win.destroy).pack()
def ________________gui_________________():pass
# Divide GUI into 3 frames: top, mid, bot
root = Tk()
topframe = Frame(root,
borderwidth=2,
relief=GROOVE)
topframe.pack(padx=2, pady=2)
midframe = Frame(root,
borderwidth=2,
relief=GROOVE)
midframe.pack(padx=2, pady=2)
botframe = Frame(root)
botframe.pack()
Button(topframe,
text='Select image to convert',
command=getinfilename).pack(side=TOP, pady=4)
multitext = """Convert all image files
(of this format) in this folder?"""
var = IntVar()
chk = Checkbutton(topframe,
text=multitext,
variable=var).pack(pady=2)
mt= """recursive"""
varRec = IntVar()
chkRec = Checkbutton(topframe,
text=mt,
variable=varRec).pack(pady=6)
mt2= """Delete original"""
varDel = IntVar()
chkDel = Checkbutton(topframe,
text=mt2,
variable=varDel).pack(pady=8)
Button(topframe,
text='Select save location',
command=getoutdirname).pack(side=BOTTOM, pady=4)
Label(midframe, text="New Format:").pack(side=LEFT)
frmt = StringVar()
formats = ['.bmp', '.gif', '.jpg', '.png', '.tif']
for item in formats:
Radiobutton(midframe,
text=item,
variable=frmt,
value=item).pack(anchor=NW)
Button(botframe, text='Convert', command=convert).pack(side=LEFT,
padx=5,
pady=5)
Button(botframe, text='Quit', command=root.quit).pack(side=RIGHT,
padx=5,
pady=5)
root.title('Image Converter')
root.mainloop()
|