Hello!
I have just created a script (see it at the end of the message) to watermark images. If you want to, could you check it and suggest ways to improve it? Think of me as a very beginner ...

Cheers!

#Import the required modules
import os, shutil, fnmatch, Image, ImageEnhance

#Ask for pictures folder and move to it
picsFolder = raw_input("Where are your pictures? ")
os.chdir(picsFolder)

#Create a Backup directory
os.mkdir ( 'Backup' )

#Copy pictures to a Backup directory
for filename in fnmatch.filter(os.listdir(picsFolder),'*.jpg'):
    shutil.copy2(filename, "Backup/%s" % filename)
    
#Ask for desired largest side size in pixels
px = raw_input("Which is the desired largest side size in pixels? ")
px = int(px)
#----------------------------------------------------------------------------
#Image processing    
for file in fnmatch.filter(os.listdir(picsFolder),'*.jpg'):
    print "Resizing image " + file
# Open the image
    img = Image.open(file)
#Get actual image size and convert it to float
    width, height = img.size
    width = float(width)
    height = float(height)
#Resize landscape images
    if width > height:
        resizeFactor = width/int(px*2)
        img = img.resize((int(width/resizeFactor),int(height/resizeFactor)))
        width, height = img.size
        img = img.resize((int(width/2),int(height/2)), Image.ANTIALIAS)
#Resize portrait images
    if width<height:
        resizeFactor = height/int(px*2)
        img = img.resize((int(width/resizeFactor),int(height/resizeFactor)))
        width, height = img.size
        img = img.resize((int(width/2),int(height/2)), Image.ANTIALIAS)
#Resize square images
    if width==height:
        img = img.resize((int(px*2),int(px*2)))
        width, height = img.size
        img = img.resize((int(px),int(px)), Image.ANTIALIAS)
#Save resized image
    img.save(file)
    
#Create a Resized directory
os.mkdir ( 'Resized' )

#Copy pictures to a Resized directory
for filename in fnmatch.filter(os.listdir(picsFolder),'*.jpg'):
    shutil.copy2(filename, "Resized/%s" % filename)

#----------------------------------------------------------------------------    
#Add mark
#Create a marked directory
os.mkdir ( 'Watermarked' )

#Ask for mark file
markFile = raw_input("Where is your mark file? ")

#Getting the sizes of the base image and the mark
imgmark = Image.open(markFile)
markWidth, markHeight = imgmark.size

for filename in fnmatch.filter(os.listdir(picsFolder),'*.jpg'):
# Open the image
    img = Image.open(file)
#Get actual image size and convert it to float
    width, height = img.size
    
    baseim = Image.open(file)
    logoim = Image.open(markFile) #transparent image
    baseim.paste(logoim,(int(width)-int(markWidth)-20,int(height)-int(markHeight)-10),logoim)
    baseim.save(filename, "JPEG")

#Recover EXIF data
    os.system("jhead -te Backup/*jpg *.jpg")

for filename in fnmatch.filter(os.listdir(picsFolder),'*.jpg'):
    shutil.move(filename, "Watermarked/%s" % filename)

On second run of program I get error:

Traceback (most recent call last):
  File "D:\test\stamper.py", line 9, in <module>
    os.mkdir ( 'Backup' )
WindowsError: [Error 183] Tiedostoa ei voi luoda. Tiedosto on jo olemassa: 'Backup'

Second error I got after choosing one of my jpg files for stamp:

raceback (most recent call last):
  File "D:\test\stamper.py", line 72, in <module>
    baseim.paste(logoim,(int(width)-int(markWidth)-20,int(height)-int(markHeight)-10),logoim)
  File "D:\Python26\lib\site-packages\PIL\Image.py", line 1101, in paste
    self.im.paste(im, box, mask.im)
ValueError: bad transparency mask
>>>

Fix those.

Resizing conditions are exclusive, so I would write:

#Resize landscape images
    if width > height:
        resizeFactor = width/int(px*2)
        img = img.resize((int(width/resizeFactor),int(height/resizeFactor)))
        width, height = img.size
        img = img.resize((int(width/2),int(height/2)), Image.ANTIALIAS)
#Resize portrait images
    elif width<height:
        resizeFactor = height/int(px*2)
        img = img.resize((int(width/resizeFactor),int(height/resizeFactor)))
        width, height = img.size
        img = img.resize((int(width/2),int(height/2)), Image.ANTIALIAS)
#Resize square images
    else:
        img = img.resize((int(px*2),int(px*2)))
        width, height = img.size
        img = img.resize((int(px),int(px)), Image.ANTIALIAS)

Thank you so much! I will work on it. Now, regarding the errors:
1) On the first run the program creates a Backup folder where it stores all the original pictures. How can I make the program to ask for overriding it in case it exists?

2) I have no idea on what this error mean. I didn't get such an error when testing the script.

Thank you so much again!
Cheers!

Dani

This checks if the path exists, and if don't it creates it. More info.

if not os.path.exists('Backup'):
    os.mkdir('Backup')

Hello,
I have found an error on lines 69 and 73. In these lines "file" should be replaced by "filename" for the loop to work properly. Maybe it was the cause of the 2nd error tonijv found.
Cheers!

Dani

Thank you Beat_Slayer for your quick reply! As you can see, I am a very beginner, but enthusiastic!
Cheers!
Dani

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.