Recently I've been looking at Povray, pyprocessing, and cfdg (version 3.0) as tools for creating digital images. I have branched two separate blogs where I mainly explore jruby + processing and processing.py

Sunday, 2 August 2009

Adding copyright to your images

From some distant memories of following the python challenge (a lot of fun but very distracting) I remembered that python has some excellent tools for manipulating images.
Now my artist friend has some posters he wants to sell; which he may do via a web page, so I suggested he should add copyright or some such text to the images (since he does a handy extra line in greetings cards, and unscrupulous people might steal the images to do the same). Now this scenario is probably unlikely as the people who buy his cards, are only likely to buy them from the kosher outlets. Anyway I did a bit of exploring and came up with the following script (a variation on some ideas at Stack Overflow a rich source of coding ideas), this script is in need of some refinements, to offer choice of light or dark text etc..... However it seems a lot simpler than adding an extra watermark layer from a second image, which is what I mostly found!!!
   1 #!/usr/bin/env python2.5
2 # -*- coding: utf-8 -*-
3 '''
4 A script for adding a 'white' copyright to a folder of images
5 Optimal image size ca ''1000 x 1000'' or '500 x 500' font 48
6 or ca 200 x 200 font 24 for light background use fill=#000
7 instaed of #fff
8 '''
9 import ImageFont, ImageDraw, Image, os
10 from os.path import join
11
12 def test():
13 ''''Is test method'''
14 batch("/home/tux/pics", "/home/tux/outputfolder/")
15
16 def batch(infolder, outfolder):
17 '''batch process infolder to outfolder'''
18 font_path = "/usr/share/fonts/ttf-bitstream-vera/Vera.ttf"
19 font_m = ImageFont.truetype(font_path, 48)
20 font_s = ImageFont.truetype(font_path, 24)
21 copyright_m = "Copyright"
22 fill_w = "#fff"
23 for root, dirs, files in os.walk(infolder):
24 for name in files:
25 try:
26 image = Image.open(join(root, name))
27 width, height = image.size
28 draw = ImageDraw.Draw(image)
29 if width > 400:
30 draw.text((width - 400, height * 0.8), copyright_m, font=font_m, fill=fill_w)
31 else:
32 draw.text((width/10, height * 0.8), copyright_m, font=font_s, fill=fill_w)
33 image.save(join(outfolder, name), image.format)
34 except Exception, (msg):
35 print msg
36
37
38 if __name__ == '__main__':
39 test()


Of course it depends on your ability to access suitable font files (these are gnome fonts).

No comments:

Post a Comment

Followers

Blog Archive

About Me

My photo
Pembrokeshire, United Kingdom
I have developed JRubyArt and propane new versions of ruby-processing for JRuby-9.1.5.0 and processing-3.2.2