Source

BlogExample / Python / photoframe.py

Full commit
Michele Gatti fdb5d0a 




















































# -*- coding: utf-8 -*-

import os.path
import sys
from PIL import Image, ImageFont, ImageDraw, ImageEnhance, ImageOps

WHITEBORDER = 2
BLACKBORDER = 40

def add_frame(im):
    """
    Aggiunge la cornice alla foto
    """

    img = ImageOps.expand(im, border = BLACKBORDER, fill = 'black')
    img = ImageOps.expand(img, border = WHITEBORDER, fill = 'white')
    img = ImageOps.expand(img, border = BLACKBORDER, fill = 'black')

    return img

def file_name_saved(fileroot):
    """
    Modifico il nome del file da salvare
    """

    root, filenamefull = os.path.split(fileroot)
    filename, extension = os.path.splitext(filenamefull)

    return os.path.join(root, filename + '_framed' + extension)

def main():
    """
        sys.argv[1] Path del file da modificare
        sys.argv[2] Font da utilizzare 
    """

    if sys.argv > 1:
        try:
            img = Image.open(sys.argv[1])
            font = ImageFont.truetype(sys.argv[2], 80)
        except IOError:
            print "Il File non trovato"
            return
            
        newimgwatermark = add_frame(newimgframe)
        newimgwatermark.save(file_name_saved(sys.argv[1]), "JPEG")

if __name__ == "__main__":
    main()