[Python] Batch Image Re-size Tool

Questions about programming languages and debugging
Post Reply
User avatar
maboroshi
Dr. Mab
Dr. Mab
Posts: 1624
Joined: 28 Aug 2005, 16:00
18

[Python] Batch Image Re-size Tool

Post by maboroshi »

Here is a batch image re-size tool using PIL that I wrote a while back. Nothing special really. But can be handy. If people want an exe binary I can provide that to.

Code: Select all

import os
import sys


from Tkinter import *
import PIL.Image as Image
import tkFileDialog

def openit():
    entrypath.delete(0, END)
    opened = tkFileDialog.askdirectory()
    entrypath.insert(END, opened)
    

def resize(folder, fileName):
    filePath = os.path.join(folder, fileName)
    sizeit1 = int(size1.get())
    sizeit2 = int(size2.get())
    size = sizeit1, sizeit2
    im = Image.open(filePath)
    im.thumbnail(size)
    im.save(filePath+"_copy_.png")

def bulkResize():
    imgExts = ["png", "bmp", "jpg", "gif"]
    for path, dirs, files in os.walk(str(entrypath.get())):
        for fileName in files:
            ext = fileName[-3:].lower()
            if ext not in imgExts:
                continue
                
            resize(path, fileName)

root = Tk()
root.title("RImage")
#root.option_readfile("optionDB")
root.wm_resizable(0, 0)
#root.wm_iconbitmap("shinobi.ico")

frame = Frame(root)
entrypath = Entry(frame)
entrypath.pack(side=LEFT, fill=X, expand=True)
entrypath.insert(END, os.getcwd())
button = Button(frame, text="Open Image Directory", command=openit)
button.pack(side=LEFT)
button = Button(frame, text="Resize Image Directory", command=bulkResize)
button.pack(side=LEFT)
frame.pack(fill=X, expand=True)

frame = Frame(root)
size1 = Entry(frame)
size1.pack(side=LEFT, fill=X, expand=True)
label = Label(frame, text="Width")
label.pack(side=LEFT)
size2 = Entry(frame)
size2.pack(side=LEFT, fill=X, expand=True)
label = Label(frame, text="Height")
label.pack(side=LEFT)
frame.pack(fill=X, expand=True)

frame = Frame(root)
label = Label(frame, text="All images will be resized in proportion have fun :-)" )
label.pack(fill=X)
frame.pack(fill=X, expand=True)


root.mainloop()

User avatar
bad_brain
Site Owner
Site Owner
Posts: 11636
Joined: 06 Apr 2005, 16:00
19
Location: In your eye floaters.
Contact:

Post by bad_brain »

and it even works.... :mrgreen: good job... :D
Image

User avatar
leetnigga
Fame ! Where are the chicks?!
Fame ! Where are the chicks?!
Posts: 447
Joined: 28 Jul 2009, 16:00
14

Post by leetnigga »

You mean:

Code: Select all

find $DIRECTORY  -E . -iregex '.*\.(png|bmp|jpg|gif)' -exec 'convert -sample $WIDTHx$HEIGHT {} {}_copy.png;'
(untested) :P

Nice work.

Code: Select all

if ext not in imgExts:
    continue
               
resize(path, fileName) 
Note you could gotten rid of that 'continue' statement by negating the boolean expression:

Code: Select all

if ext in imgExts:
    resize(path, fileName) 
Which seems more logical to me.

Post Reply