You are not logged in.

#1 2008-12-27 02:52:23

crouse
Arch Linux f@h Team Member
From: Iowa - USA
Registered: 2006-08-19
Posts: 907
Website

Python Batch Image Resizer - GUI

There appear to be a couple ways of doing gui's for python using Tkinter.  I choose the grid system, was wondering if this is the best way, or if the pack method is better ? 
The grid system seems to make more sense to me, not that that means much big_smile It's probably not real  "Pythonic" either, as I'm just learning.

Wrote this today......... still learning.......  wink
So don't be too critical....... it does work smile
Several more things to do before it's final........ like error checking and application checking etc etc....... a work in progress.....
Resizes all jpg files in a directory. Currently it just creates new images with PBIR. in front of the filenames ....... it doesn't overwrite the existing images.

pythonimage.png

# Python Batch Image Resizer
# by Crouse 12-26-2008
import os
import subprocess
import glob
from tkFileDialog import askdirectory
from Tkinter import *

mainbg = '#ffffff';
root = Tk()
root.title('Python Batch Image Resizer')

# Functions
def mkgrid(r, c, w):
    w.grid(row=r, column=c, sticky='news')
    return w

def processimages():
    subprocess.call(['sleep', '2'])        
    if not mydirectory:
        exit
    else:
        os.chdir(mydirectory)
        jpegFiles = glob.glob("*.[jJ][pP][gG]") + glob.glob("*.[jJ][pP][eE][gG]")
    Width = label01.get()
    Height = label11.get()    
    for ifile in jpegFiles:    
        subprocess.call(['convert', ifile, '-resize', Width+"x"+Height , 'PBIR.'+ifile])
        label61.configure(text="Resizing "+ifile)
        root.update()
    label61.configure(text="Finished - All Images Processed")

def choosedir():
    global mydirectory
    mydirectory = askdirectory(
        title="Directory to Resize ?",
        mustexist=1)
    label21.configure(text=str(mydirectory ))
    
# Labels
label00 = mkgrid(0, 0, Label(root, text="Enter Max Width",
                          anchor='e', bg=mainbg))
label01 = mkgrid(0, 1, Entry(root, bg=mainbg))
label02 = mkgrid(0, 2, Label(root, text='  ', bg=mainbg))
label10 = mkgrid(1, 0, Label(root, text="Enter Max Height",
                          anchor='e', bg=mainbg))
label11 = mkgrid(1, 1, Entry(root, bg=mainbg))
label12 = mkgrid(1, 2, Label(root, text='', bg=mainbg))
label20 = mkgrid(2, 0, Button(root, text="Choose Directory",
                           bg='#949494', activebackground='#949494',
                           command=choosedir))
label21 = mkgrid(2, 1, Label(root, text='', relief=SUNKEN, bg=mainbg))
label22 = mkgrid(2, 2, Label(root, text='', bg=mainbg))
label30 = mkgrid(3, 0, Label(root, text='', bg=mainbg))
label31 = mkgrid(3, 1, Label(root, text='', bg=mainbg))
label32 = mkgrid(3, 2, Label(root, text='', bg=mainbg))
label40 = mkgrid(4, 0, Label(root, text='', bg=mainbg))
label41 = mkgrid(4, 1, Button(root, text="Process Images",
                           bg='#949494', activebackground='#949494',
                           command=processimages))
label42 = mkgrid(4, 2, Label(root, text='', bg=mainbg))
label50 = mkgrid(5, 0, Label(root, text='', bg=mainbg))
label51 = mkgrid(5, 1, Label(root, text='', bg=mainbg))
label52 = mkgrid(5, 2, Label(root, text='', bg=mainbg))
label60 = mkgrid(6, 0, Label(root, text='Processing Status', bg=mainbg))
label61 = mkgrid(6, 1, Label(root, text='' ,
                          anchor='w', bg=mainbg ))
label62 = mkgrid(6, 2, Label(root, text='', bg=mainbg))
root.mainloop()

Offline

#2 2008-12-27 04:44:34

tam1138
Member
Registered: 2007-09-10
Posts: 238

Re: Python Batch Image Resizer - GUI

It always annoys me when people make the kind of post I'm about to make, because most of the software I write is for the fun of writing and design as opposed to the fulfillment of a void in the community.  So if you're doing this because you want to learn or have fun, totally ignore everything that follows.

If, on the other hand, you just want a handy image resizer, allow me to suggest imagemagick, which includes the useful command 'convert' which may be used thusly:

for i in *.jpg; do convert -resize "$i" "${i/%.jpg/-new.jpg}"; done

(Yes, I am a total command-line zealot.)

Last edited by tam1138 (2008-12-27 04:45:19)

Offline

#3 2008-12-27 09:27:48

crouse
Arch Linux f@h Team Member
From: Iowa - USA
Registered: 2006-08-19
Posts: 907
Website

Re: Python Batch Image Resizer - GUI

tam1138 wrote:

It always annoys me when people make the kind of post I'm about to make, because most of the software I write is for the fun of writing and design as opposed to the fulfillment of a void in the community.  So if you're doing this because you want to learn or have fun, totally ignore everything that follows.

If, on the other hand, you just want a handy image resizer, allow me to suggest imagemagick, which includes the useful command 'convert' which may be used thusly:

for i in *.jpg; do convert -resize "$i" "${i/%.jpg/-new.jpg}"; done

(Yes, I am a total command-line zealot.)

Obviously, you didn't read the program tongue  lol .....

But,yes, I am actually doing it just for fun and to learn.  I have in fact created many reuseable small bash programs calling imagemagick (do a search for bbips on sourceforge).

If you look closely at the python program above, you'll notice it has a gui (which I couldn't accomplish with bash and dialog) and it DOES in fact USE imagemagick wink  Why imagemagick ? Because I'm not familiar enough with pil and after writing all the bbips stuff for imagemagick, I'm much more familiar with what it can do.

As for the void.... well, my non-techie wife could use the gui program, but would be lost if I asked her to use the cli.

Last edited by crouse (2008-12-27 09:28:39)

Offline

#4 2008-12-27 13:37:59

kjon
Member
From: Temuco, Chile
Registered: 2008-04-16
Posts: 398

Re: Python Batch Image Resizer - GUI

err... I don't want to kill ye'r fun, however if you prefer the lazy, but effective sysadmin approach, I would recommend you 'phatch'. It's on aur.


They say that if you play a Win cd backward you hear satanic messages. That's nothing! 'cause if you play it forwards, it installs windows.

Offline

#5 2008-12-27 15:09:48

crouse
Arch Linux f@h Team Member
From: Iowa - USA
Registered: 2006-08-19
Posts: 907
Website

Re: Python Batch Image Resizer - GUI

Phatch pretty cool....... but I think I'll just keep learning anyway wink  Like I stated above... " I am actually doing it just for fun and to learn"

Offline

#6 2008-12-27 17:06:32

kjon
Member
From: Temuco, Chile
Registered: 2008-04-16
Posts: 398

Re: Python Batch Image Resizer - GUI

Good luck. I'm learning python too. I'm having serious headaches struggling against a network camera tongue


They say that if you play a Win cd backward you hear satanic messages. That's nothing! 'cause if you play it forwards, it installs windows.

Offline

#7 2008-12-31 12:31:53

raf_kig
Member
Registered: 2008-11-28
Posts: 143

Re: Python Batch Image Resizer - GUI

Just a few hints :-)

1. use time.sleep() from time
2. try to separate logic from presentation
meaning: dont use methods from your gui classes inside your processing function, use arguments instead
3. dont make all stuff global :-)
generally it could look more like:

def processimage(filename, width, height):
      #process one image, 

def getImages(path): 
      #return a list of all images that are to be resized

def startProcessing():
     for file in getImages(path):
           #update your gui here
           processImage(file, width,height)
        
main(): 
    #instanciate gui
    #start mainloop

if __name__ == '__main__':
    main()

Offline

#8 2008-12-31 12:56:50

crouse
Arch Linux f@h Team Member
From: Iowa - USA
Registered: 2006-08-19
Posts: 907
Website

Re: Python Batch Image Resizer - GUI

thanks raf_kig , I'll try to fix it wink

Offline

Board footer

Powered by FluxBB