Re: [Tutor] adding a watermark to a sequence of images

2008-07-21 Thread Terry Carroll
On Sun, 20 Jul 2008, Christopher Spears wrote:

 Has anyone used Python to watermark of sequence of images?

There's a recipe for watermarking using PIL here:

http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/362879

I have a half-baked program that goes through a directory of images and 
time-stamps them with information from the EXIF data, which I'd be happy 
to share with you.  I started writing it because my wife likes to have our 
photos timestamped, and I don't, so this was our compromise; but she later 
came around to my point of view so I never really cleaned it up.  It 
works, but needs some polish.  It would be easy to adapt to a fixed 
watermark.

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] adding a watermark to a sequence of images

2008-07-21 Thread Christopher Spears
By all means, share your script!  Even if I don't use it, I can learn something 
from it!

Thanks!

--- On Mon, 7/21/08, Terry Carroll [EMAIL PROTECTED] wrote:

 From: Terry Carroll [EMAIL PROTECTED]
 Subject: Re: [Tutor] adding a watermark to a sequence of images
 To: [EMAIL PROTECTED], tutor@python.org
 Date: Monday, July 21, 2008, 1:30 PM
 On Sun, 20 Jul 2008, Christopher Spears wrote:
 
  Has anyone used Python to watermark of sequence of
 images?
 
 There's a recipe for watermarking using PIL here:
 
 http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/362879
 
 I have a half-baked program that goes through a directory
 of images and 
 time-stamps them with information from the EXIF data, which
 I'd be happy 
 to share with you.  I started writing it because my wife
 likes to have our 
 photos timestamped, and I don't, so this was our
 compromise; but she later 
 came around to my point of view so I never really cleaned
 it up.  It 
 works, but needs some polish.  It would be easy to adapt to
 a fixed 
 watermark.


  
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] adding a watermark to a sequence of images

2008-07-21 Thread Terry Carroll
On Mon, 21 Jul 2008, Christopher Spears wrote:

 By all means, share your script!  Even if I don't use it, I can learn
 something from it!

Well, maybe.  If nothing else, perhaps you'll learn some new bad habits.

The timestamp on this file shows that I dropped this project in June 2005, 
and you can see it's unfinished.  It still has a few raw edges (including 
a prompt for a preserve EXIF feature that never got written; and lots of 
debugging print statements), but it's not bad as a proof-of-concept.

The code imports and depends on Gene Cash's EXIF.py module; it now 
apparently lives at http://sourceforge.net/projects/exif-py/ rather than 
the URL in the code.  But still, if the EXIF module doesn't exist, it just 
uses the file's timestamp as the date -- not exactly the safest way of 
figuring the date the photo was taken.

At the time I wrote this, I was unaware that PIL had some 
then-undocumented EXIF support; I probably would have used that to avoid 
importing a non-standard module.

Use this code however you wish, if you find it helpful.  Ignore the 
copyright statement; BaishuSoft was just a little joke between my wife and 
me.


--snip---
import Tkinter as Tk
import tkFileDialog
import os, sys, time
import Image, ImageDraw, ImageFont, ImageTk


class MyApp:
Begin a Tkinter-based application

def __init__(self, root):
initializer for Tkinter-based application
self.root=root
self.root.title(Picture TimeStamper)
NoticeFrame = Tk.Frame(self.root)
NoticeFrame.pack()
headertext = u
Baishusoft Picture TimeStamper
\U00A9 2005 Baishusoft LLP

Tk.Label(NoticeFrame,text=headertext).pack()
ButtonFrame = Tk.Frame(self.root)
ButtonFrame.pack()   
SelButton = Tk.Button(ButtonFrame, 
text=Select Directory, command=self.select)
SelButton.pack(side=left)
QuitButton = Tk.Button(ButtonFrame, text=Quit, 
command=self.quit)
QuitButton.pack(side=left)

OptionsFrame = Tk.Frame(self.root)
OptionsFrame.pack()
self.EXIFvar = Tk.IntVar()
self.EXIFCheckbox = Tk.Checkbutton(
OptionsFrame,
text=Preserve EXIF (requires JHead),
variable = self.EXIFvar)
self.EXIFCheckbox.pack(side=top)
self.Progressvar = Tk.IntVar()
self.ProgressCheckbox = Tk.Checkbutton(
OptionsFrame,
text=Show progress,
variable = self.Progressvar)
self.ProgressCheckbox.pack(side=left)
   
self.StatusFrame = Tk.Frame(self.root)
self.StatusFrame.pack()
self.ImageLabel = Tk.Label(self.StatusFrame)
self.ImageLabel.pack()
self.FilenameLabel = Tk.Label(self.StatusFrame)
self.FilenameLabel.pack()

def select(self):
dirname = tkFileDialog.askdirectory()
if dirname != '':
os.path.walk(dirname, self.process_files, None)
print PROCESSING COMPLETED.  SELECT MORE FILES OR QUIT.
else:
print NO DIRECTORY SELECTED.
return

def quit(self):
print EXITING.
sys.exit()

def process_files(self, junk, dirpath, namelist):
for filename in namelist:
stamped_filename = self.getstampedfilename(filename)
if stamped_filename is not None:
if os.path.isfile(os.path.join(dirpath,stamped_filename)):
print FILE EXISTS, SKIPPING:, stamped_filename
else:
self.updatestatus(dirpath, filename)
datetext = self.timestamp(dirpath, filename, 
stamped_filename)
print FILE IMPRINTED:, stamped_filename, datetext

def updatestatus(self,dirpath,filename):
im=Image.open(os.path.join(dirpath, filename))
print time.asctime(), thumbnailing..., filename, im.mode, im.size
im.thumbnail((100,75))
print time.asctime(), showing..., filename, im.mode, im.size
#im.show()
self.Tkimage = ImageTk.PhotoImage(im)
print created
self.ImageLabel.config(image=self.Tkimage)
self.ImageLabel.pack()
self.FilenameLabel.config(text=filename)
self.FilenameLabel.pack()
self.StatusFrame.pack()
self.root.update()


def getstampedfilename(self, filename):
fn, ft = os.path.splitext(filename)
if ft.upper() in [.JPG, .JPEG] and \
   not (fn.upper().endswith(-DATED)):
return fn + -dated + ft
else:
return None

def timestamp(self, dirpath, original_filename, new_filename):
full_original_filename = os.path.join(dirpath, original_filename)
full_new_filename = os.path.join(dirpath, new_filename)
font=ImageFont.truetype(Arial.ttf, 40)
datetext = GetFileDate(full_original_filename)

[Tutor] adding a watermark to a sequence of images

2008-07-20 Thread Christopher Spears
Has anyone used Python to watermark of sequence of images?

Thanks.


  
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] adding a watermark to a sequence of images

2008-07-20 Thread Michiel Overtoom
Christopher wrote...

 Has anyone used Python to watermark of sequence of images?

No, but I have used PIL (Python Image Library) to do other things to batches
of images, like resizing or printing statistics on it.
Maybe you can get some ideas from these; maybe you can combine them with a
watermarking library.

--- ShowImageDimensions.py

import os,sys,Image

# See http://www.pythonware.com/library/pil/handbook/introduction.htm

def showdimension(filename):
im=Image.open(filename)
print filename,im.size

for infile in os.listdir('.'):
f, e = os.path.splitext(infile)
if (e=='.tga' or e=='.jpg' or e=='.gif' or e=='.bmp'):
showdimension(infile)



--- WoWScreenshotConverter.py

# this converts all TGA's in the current ditectory to JPG's, full-, half-
and small size versions.
# I wrote this in the time when World of Warcraft could not save screenshots
in JPG format.


import os,sys,Image

# See http://www.pythonware.com/library/pil/handbook/introduction.htm

def convert(basename):
print 'Converting',basename
im=Image.open(basename+'.tga')
im.save(basename+'-full.jpg')
# halfsize=tuple(map(lambda x: x/2,im.size))
halfsize=tuple([x/2 for x in im.size])
im.thumbnail(halfsize,Image.ANTIALIAS)
im.save(basename+'-half.jpg')
thumbsize=128,128
im.thumbnail(thumbsize,Image.ANTIALIAS)
im.save(basename+'-small.jpg')


# convert('WoWScrnShot_123005_091926.tga')
for infile in os.listdir('.'):
f, e = os.path.splitext(infile)
if (e=='.tga'):
convert(f)

-- 
The ability of the OSS process to collect and harness
the collective IQ of thousands of individuals across
the Internet is simply amazing. - Vinod Vallopillil
http://www.catb.org/~esr/halloween/halloween4.html

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor