I have finally got my program finished and going. Im now looking to make it look a bit better. While it is processing it shows and empty tk window. How does one go about putting something in here? I'd just like my users to be reassured it is actually working and not crashed. Ideally a progress bar or similar but anything from text listing the current file, a count of files done or just a spinny loading icon would do. Here is the code if anyone is interested.

import Image, os, tkFileDialog, tkMessageBox

def watermark(im, mark, mark2):
    
    if im.mode != 'RGBA':
        im = im.convert('RGBA')

    wd = float(im.size[0])
    ht = float(im.size[1])
    if wd > ht:
        basewidth = 720
    	wpercent = (basewidth/float(im.size[0]))
    	hsize = int((float(im.size[1])*float(wpercent)))
    	im = im.resize((basewidth,hsize))	
    else:    
        baseht = 720
    	wpercent = (baseht/float(im.size[1]))
    	wsize = int((float(im.size[0])*float(wpercent)))
    	im = im.resize((wsize,baseht))
    layer = Image.new('RGBA', im.size, (0,0,0,0))
    layer.paste(mark, ((im.size[0]-mark.size[0]) , (im.size[1] - mark.size[1]) ))
    layer.paste(mark2, (0,0))
    return Image.composite(layer, im, layer)

def run():

    tkMessageBox.showinfo("Select Folder", "Specify Image Folder")
    p = tkFileDialog.askdirectory()
    tkMessageBox.showinfo("First Logo", "Select first .png logo")
    b = tkFileDialog.askopenfile()
    mark = Image.open(b)
    tkMessageBox.showinfo("Second Logo", "Select second .png logo")
    c = tkFileDialog.askopenfile()
    mark2 = Image.open(c)
 
    os.chdir(p)
    x = 0   
    count = 0   
    while (count < 1):
      for i in os.listdir(os.getcwd()):
        count = count + 1
        im = Image.open(i)
        watermark(im, mark, mark2).save('pro_'+ i , "JPEG", quality=95)
       

if __name__ == '__main__':
    run()

Many thanks,
Rich

I do not know but maybe you could show each picture before you start to process it, I do not know if the slow down is acceptable.

I could not test your functions as it gave me error

>>> 

Traceback (most recent call last):
  File "D:\Tony\Tests\watermark.py", line 42, in <module>
    im = Image.open(i)
  File "D:\Python26\lib\site-packages\PIL\Image.py", line 1980, in open
    raise IOError("cannot identify image file")
IOError: cannot identify image file
>>>

After I had converted one of my gif images for png format.
Could you give few pictures of required format as attachment to your message?
I would clean up your loop Python style

if __name__ == '__main__':
    toplevel = Tkinter.Tk()
    ## take out the disturbing main window
    toplevel.withdraw()

    tkMessageBox.showinfo("Select Folder", "Specify Image Folder")
    p = tkFileDialog.askdirectory()
    os.chdir(p)

    tkMessageBox.showinfo("First Logo", "Select first .png logo")
    b = tkFileDialog.askopenfile()
    mark = Image.open(b)
    tkMessageBox.showinfo("Second Logo", "Select second .png logo")
    c = tkFileDialog.askopenfile()
    mark2 = Image.open(c)
 
## unused variable   x = 0   
    for count,i in enumerate(os.listdir(os.getcwd())):
        im = Image.open(i)
        print count,i
        watermark(im, mark, mark2).save('pro_'+ i , "JPEG", quality=95)

Thanks for doing the housework on my code!! Looks a bit nicer now!
I have no idea about the error you get there, never had anything come up like that. I am on a linux box though.
This is the tidied version, works absolutely fine here. Im not sure how to attach a picture on the forum but its just a 100x100 logo png file, just to sit in the corners. The first part gets you to link to a folder of JPEGs, mine just taken straight off a camera then put some little logos into it.

import Image, os, tkFileDialog, tkMessageBox, Tkinter

def watermark(im, mark, mark2):
    
    if im.mode != 'RGBA':
        im = im.convert('RGBA')

    wd = float(im.size[0])
    ht = float(im.size[1])
    if wd > ht:
        basewidth = 720
    	wpercent = (basewidth/float(im.size[0]))
    	hsize = int((float(im.size[1])*float(wpercent)))
    	im = im.resize((basewidth,hsize))	
    else:    
        baseht = 720
    	wpercent = (baseht/float(im.size[1]))
    	wsize = int((float(im.size[0])*float(wpercent)))
    	im = im.resize((wsize,baseht))
    layer = Image.new('RGBA', im.size, (0,0,0,0))
    layer.paste(mark, ((im.size[0]-mark.size[0]) , (im.size[1] - mark.size[1]) ))
    layer.paste(mark2, (0,0))
    return Image.composite(layer, im, layer)


if __name__ == '__main__':
    toplevel = Tkinter.Tk()
    ## take out the disturbing main window
    toplevel.withdraw()

    tkMessageBox.showinfo("Select Folder", "Specify Image Folder")
    p = tkFileDialog.askdirectory()
    os.chdir(p)

    tkMessageBox.showinfo("First Logo", "Select First .png logo")
    b = tkFileDialog.askopenfile()
    mark = Image.open(b)
    tkMessageBox.showinfo("Second Logo", "Select Second .png logo")
    c = tkFileDialog.askopenfile()
    mark2 = Image.open(c)
 
    for count,i in enumerate(os.listdir(os.getcwd())):
        im = Image.open(i)
        print count,i
        watermark(im, mark, mark2).save('Pro_'+ i , "JPEG", quality=95)

I just want it to be doing something while its working so it looks like its working, Running it as a script is ok with the "print count, i" bit but i want to compile it later so that won't work. Maybe put that into a text box on the tkinter window?

Rich

I played around the code and found the error was that you used b = tkFileDialog.askopenfile() instead of b = tkFileDialog.askopenfilename() So the variable, which should have contained string variable, contained open file handle.

I returned the code to manual counting of successes instead of enumerate counting.

Still prints to console for debugging purposes.

I attach a splash screen picture in JPG and two small pics for watermark, even they are gif format.

import os
import Image, ImageTk, tkFileDialog, tkMessageBox

import Tkinter as tk

def watermark(im, mark, mark2):
    
    if im.mode != 'RGBA':
        im = im.convert('RGBA')

    wd = float(im.size[0])
    ht = float(im.size[1])
    if wd > ht:
        basewidth = 720
        wpercent = (basewidth/float(im.size[0]))
        hsize = int((float(im.size[1])*float(wpercent)))
        im = im.resize((basewidth,hsize))   
    else:    
        baseht = 720
        wpercent = (baseht/float(im.size[1]))
        wsize = int((float(im.size[0])*float(wpercent)))
        im = im.resize((wsize,baseht))
    layer = Image.new('RGBA', im.size, (0,0,0,0))
    layer.paste(mark, ((im.size[0] - mark.size[0]),
                       (im.size[1] - mark.size[1]) ))
    layer.paste(mark2, (0,0))
    return Image.composite(layer, im, layer)


if __name__ == '__main__':
    root = tk.Tk()
    root.title('Advanced Watermarks')

    frame = tk.Frame(root,colormap="new",visual='truecolor').pack()
    ## freephoto.com sea picture
    imagedata = ImageTk.PhotoImage(file='3001_07_75_prev.jpg')
    panel = tk.Label(frame,image=imagedata)
    panel.pack()

    tkMessageBox.showinfo("Select Folder", "Specify Image Folder")

    p=''
    while not p: p = tkFileDialog.askdirectory()

    os.chdir(p)
    print os.curdir

    tkMessageBox.showinfo("First Logo", "Select First .png logo")
    a = tkFileDialog.askopenfilename() ## get filename
    mark = Image.open(a)
    
    tkMessageBox.showinfo("Second Logo", "Select Second .png logo")
    b = tkFileDialog.askopenfilename()
    mark2 = Image.open(b)
    
    processed=0
    
    panel.pack_forget() ## Drop splash and start showing info
    
    root.geometry('400x800+0+0')
    out=tk.Text(root)
    out.pack(expand = tk.YES,fill = tk.BOTH)
    out.insert(tk.END,'Welcome to watermark program!\n\n')
    sbar = tk.Scrollbar(out)
    sbar.config(command=out.yview)      # xlink sbar and list

    out.config(yscrollcommand=sbar.set,
               font='{arial 12}') # move one moves other
    sbar.pack(side=tk.RIGHT, fill=tk.Y)


    for i in os.listdir(os.getcwd()):
        if i[:4]=='Pro_':
            print i,'already watermarked'
        elif i==a or i==b:
            print i,'is watermark file'
        else:
            try:
                im = Image.open(i)
                processed+=1
                out.insert(tk.END,str(processed)+':\t'+i+'\n')
                out.update()
                
                ## Maybe better to put results in new directory?
                ## Maybe do not watermark very small files in directory?
                
                ## !! GIF files watermarked were jpeg files with gif extension
                
                ## take out extension and put .jpg
                ## adapt to .jpg and .jpeg etc.
                j=i ## file without extension stays as is
                for ind in range(-6,-2): ## find start point of extension
                    if i[ind]=='.':
                        print ind
                        j=i[:ind]
                        
                watermark(im, mark, mark2).save('Pro_'+j+'.jpg' ,
                                                "JPEG", quality=95)
            except IOError:
                print i,'not image'
    
    out.insert(tk.END,'\nConversion finished!\nProsessed ' +
               str(processed) + ' files.')

    root.mainloop()
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.