the site of Leo Baker…

Posts Tagged ‘python’

Watermark your images with python

I set about making a tool in python that can place a water-mark identity image over my pictures that I wanted to put in the public domain -(i.e on my website or through email ). Using python means I can make a tool that can batch process image files or folders quickly. I think it’s a good idea to watermark your images. This way people know where they come from, so it is less likely that due credit is lost.

Download the watermarking toolkit here.

The download bundle above has 2 tools (LB_picturePrep.py and LB_watermarker.py) for batch processing your images with options. These scripts have been made for command line usage.

LB_picturePrep should cover all that you would need to do when preparing smaller resolution images, with the option of watermarking. It has the revamped functionality of the image proxy making tool I made a while ago. The script is optimized further, and will now automatically orient your images (shot with a digital camera). This way you can make smaller web ready files that are watermarked, all in one operation. Alternatively, if you aren’t interested in making smaller resolution files, then LB_watermarker does addresses the orientation and watermarking with no image resizing.

The requirements for using this toolkit is of course python, an interpreted scripting language. I also use the Python Image Library (PIL) module, which you will need in order to run either of these 2 scripts.

Here are a couple of examples of image results (click to enlarge):



You can see that the tools can position the watermark in different positions, with varying opacity. The image to the left has a black and white watermark file that was ’screened’ over the image. The image to the right has a watermark with an alpha (transparency) channel to allow for a composite ‘over’ operation. Either mode will allow for varying opacity.

See below for the details of the 2 scripts, and how to use them.


LB_picturePrep is the main, more versatile script, and has these features:

  • - will automatically attempt to orient your images correctly.
  • - will create lower resolution ‘proxies’ with presets for “web” (640 pixels along larger dimension),”1k” (1024 pixels along larger dimension) or otherwise will take a divisional ammount to create the smaller images.
  • -has the option for applying a watermark to your images once they have been reduced in size.

There are 2 modes for completing this. If your image has an alpha channel, this will be used to create the transparency of the composite ‘over’ the source image. If no alpha is found the script will ’screen’ the brighter values onto the image. So you create your watermark image file to cater for your composite mode preference.

**You will need to add the file path to your watermark file at the top of each script.**  This is hardcoded, so you can just set and forget.

As for positioning the watermark, there are 5 options, initiated with the “-p” flag and a number from 1 to 5.
Number 1 being the top left and other position counted around clockwise from here.
So 2 is top right, 3 is lower right, 4 is lower left, and 5 is the centre.

HELP / INSTRUCTIONS:

PROXY-MAKER FLAGS:
-d or –division

    Sets the divisional ammount to create the new smaller res files. The images will be divided by this number.

-t or –type

    Will maintain the file format of the original files -otherwise will default to jpeg.

-w or –web

    Sets the target proxy Res so that the larger dimension is 640 – which is an ideal size for email and web work.
    If images are smaller than this res they will not be altered.

-1 or –1k

    Sets the target proxy Res so that the lerger dimension is 1024 – for a 1k approximation.
    If images are smaller than this res they will not be alterred.

WATERMARK FLAGS:
-m or –waterMark

    Enables the watermarking, once proxy making has taken place.

-o or –opacity

    Sets the opacity level of the watermark. Default is 70 (percent) if this value isn’t specified.

-p or –position

    Sets the position of the watermark file from 1-5. These positions are listed numerically from top left heading clockwise. The 5th position is centered.

-i or –invert

    Inverts the watermark image when in screen mode, handy if you havent had time to prep the watermark properly.

and of course -h or –help will display the HELP / INSTRUCTIONS listed above for either script.

Usage example:

LB_picturePrep.py -d 2 -m -p 4 -o 50 /Path/To/File/Or/Folder

this example will make half resolution proxies with a watermark in the lower left corner at 50% opacity. Much like the first example image above.


LB_watermarker is the almost the same as LB_picturePrep. It just has scaled down functionality to cater only for orientation and watermarking. It will not create lower resolution files. It will still require that you set the path to the watermark file, within the script itself. This script uses the same flags as the LB_picturePrep tool, except for any of the proxy maker flags or the “-m” that enables the watermark for that script. Again running “-h” or “–help” flags will display the usage.

Usage example:

LB_waterMarker.py -p 3 -o 75 -t /Path/To/File/Or/Folder

this example will place the watermark in the lower right corner of the images, with opacity set to 75%, and maintain the original filetype of your source image.

Happy watermarking!


processing images to look like halftone print

I have always liked old prints that look like they are made up of many small dots. This process / look is called halftone.
Here is an example of a black and white halftone print of an old Barbarella poster. (click on the right black and white images below to see what i’m talking about)

barbarella-posterbarbarella-posterbarbarella-poster
I made a python script that sort of replicates this look, using PIL. You can grab my halftone script here.
You can create dotted images in black and white or colour. You can have white dots on a black background, or black dots on a white background. So essentially a positive or negative method of dotting. The script gets some cool results, but it is quite slow, especially when you have a smaller dot size or larger resolution source files. As it builds a new image dot by dot, the more dots the more time it takes.

Here are some of my tests. Click on the images for full effect: please note they are obviously not from photos i have taken myself, and the sources and credits are unknown.

base marilym imagemarilyn-monroe002_dotMatrixzd
base darker image

darker image -s 5






Usage

From commandline:

python pathToLB_halftone_v1.0.py fileOrFolderToSourceFrom args

or of course, you can alias it to ‘dot’ or whatever you like.

FLAGS:
-p      Sets the divisional ammount to create the new smaller res files. The images will be divided by this number.
-t      CURRENTLY NOT IMPLEMENTED: is intended to maintain the file format of the original files -otherwise will default to jpeg.
-w      Sets the target proxy Res so that the larger dimension is 640 – which is an ideal size for email and web work.
If images are smaller than this res they will not be alterred.
-1      Sets the target proxy Res so that the lerger dimension is 1024 – for a 1k approximation.
If images are smaller than this res they will not be alterred.

FLAGS:

-s –squareSize Sets the number of pixels allocated for each dot shape. Eg: 6 = more small dots 16 = fewer larger dots

-c –colours Sets the script to colour mode.

-f –fileType Sets the filetype for new files created. Supported types are: [ "jpeg", "tiff", "gif", "png" ]

-n –negative Sets the script to negative mode (being black dots on a white background) rather than the default, (white dots on a black background).

The python script i have made produces image results very similar to half tone -I guess the only difference is the dots are displayed in line with each other rather than in a zigzag pattern.

I would love to use either this script or my pixelize script to process some of my animation, for a stylized effect. Even animate the dot size over time perhaps? I guess that could be another idea…

Further info on halftone.


backup your stuff! ….for 39kb on mac or 176kb on windows.

Computers suck. They can die on you at the worst time and you don’t want to lose your important stuff, like your mp3 collection…

I’m not really interested in backup systems that restore my computer to certain points in time, like time-machine. Although these are well designed and useful systems, they can be confusing and you can lose track of your files, as such systems are time based. All I want is a simple, reliable tool that archives important stuff, so I have peace of mind that my precious files are BACKED UP! So I wrote my own little backup utility in Python.

I originally wrote this for my personal Mac, and later adapted the script to work on Windows. I use windows for work, and there is a lot of data created daily that needs to be kept safe. The script backs up to external hard drives. I have multiple drives enabled on the Windows version that I use at work so I can rotate the drives daily -keeping one drive offsite (in case or fire, burglary, rodents etc). The other handy thing about the Windows one is that you can log out and it will run and then it can shut your machine down when finished (if you wish to shutdown). So once set up -all you need to do is log out of your machine and it takes care of everything.

The scripts are basically an organiser around the unix ‘rsync’ utility. Rsync doesn’t exist on Windows systems, so I have included an rsync.exe in the Windows version.

download the Mac version

download the Windows version

Both of the bundles have a folder of little bits and pieces. In the folder is the main Python script, you will need to open this in a decent text editor and edit some of the details to make it customized for your system. Note it is best to run the script via an automation system. The Windows version has an ‘instructions’ file for how to do this and setup Windows Scheduler. The Mac version has a cron file for automating via commandline with crontab. If you set this automation you can literally ’set and forget’. The script has inbuilt mail notification, which provides all information about the script output, so you can monitor the process.

Ahhh, peace of mind.


create lower resolution image proxies with python

For those who are friends of the command line, here is a simple python script that utilises PIL to generate lower resolution proxy images of your larger files.

This is often handy when doing any kind of vfx work where smaller files allow for faster interaction. The script is setup to process individual files or entire folders of images. I also use it a lot for quickly creating emailable size images from the larger res downloads off my digital camera. At the moment the script is setup merely to create jpeg type files, but I wanted to later implement the option of maintaining the original file format, or just specifying an output filetype.

Usage

From commandline:

python pathToLB_proxyMaker.py fileOrFolderToSourceFrom args

or of course, you can alias it to ‘proxy’ or whatever you like.

FLAGS:
-p      Sets the divisional ammount to create the new smaller res files. The images will be divided by this number.
-t      CURRENTLY NOT IMPLEMENTED: is intended to maintain the file format of the original files -otherwise will default to jpeg.
-w      Sets the target proxy Res so that the larger dimension is 640 – which is an ideal size for email and web work.
If images are smaller than this res they will not be alterred.
-1      Sets the target proxy Res so that the lerger dimension is 1024 – for a 1k approximation.
If images are smaller than this res they will not be alterred.

FLAGS:

-d –division Sets the divisional amount to create the new smaller res files. The images will be divided by this number.

-w –web Sets the target proxy Res so that the larger dimension is 640 – which is an ideal size for email and web work. If images are smaller than this res they will not be altered.

-1 –1k Sets the target proxy Res so that the larger dimension is 1024 – for a 1k approximation. If images are smaller than this res they will not be altered.

-t --type CURRENTLY NOT IMPLEMENTED: is intended to maintain the file format of the original files -otherwise will default to jpeg.

You can get it to make proxies based on a division amount of the original res -half res, quarter res etc. This is by using the “-d” flag followed by a dividing number. Eg. “-d 2″ will make half res files or “-d 4″ will make quarter res files. I have also included a couple of handy modes for making web or email size files.The flag “-1″ will make 1k files (where the larger of the 2 dimensions is set to 1024 pixels), or the flag “-w” makes a slightly smaller equivalent, where the larger of the 2 dimension is set to 640 pixels.

Known drawbacks

Unfortunately the version of PIL available at the time I wrote this post has a few annoying limitations. The main problem is that it will not maintain the image’s colour profile, or any other associated meta data. So if you have a particular colour profile assigned to your images, say ’sRGB’, it will not exist on the newly output proxy files. On my computer (Mac) I think it just assigns a ‘colour LCD’ profile upon opening the images -which can often look rather ordinary. Although you can manually reassign a colour profile to your images at a later date. I think there are various scripts out there that can do this, but that again is irritating as it means it becomes a 2 part process.  I believe the next version of PIL (1.1.7) should address maintaining the colour profile across image manipulation.


pixelizing images with python

I’ve been playing around with PIL (python image library) learning how to mash images in various ways. This was my second script after the proxy making script. It converts an image into a pixelized version. You can choose how many pixels across (along the smaller resolution) and how many colours you wish to convert to. So some of the images end up looking like old 8 bit video game stills. Kinda cool. Obviously you get better results form using bold, contrasty pictures.

Here’s a couple of tests. Please note these are not my images, and cannot credit the photographers as I don’t know who they are.


base image

base image

pixelized to 8 colours & 64 pixels across

pixelized to 8 colours & 64 pixels across

pixelized to 4 colours, 64 pixels across

pixelized to 4 colours, 64 pixels across


base image, the lovely gwen stefani

base image, the lovely gwen stefani

pixelized to 16 colours, 128 pixels across

pixelized to 16 colours, 128 pixels across

pixelized to 8 colours, 96 pixels across

pixelized to 8 colours, 96 pixels across


Most graphical programs have ways of doing this as an effect or filter. I know there is a shake node for it. Though I like it how this one you can adjust the number of colours at the same time, and it’s pretty fast. It is also setup to batch process directories of images via command line. click here to download the pixelize python script.