modImage.py

The modImage script is a command line tool that can resize/crop/convert images. It should work on any platform that has python >= 2.4 and the python image library PIL (usually packaged as python2.4-imaging) installed. It can work on a group of images and has a bunch of options to fit your needs. You can download the script here: modImage.py

Description:

Here is the usage of the script:

$ ./modimage.py -h
usage: modimage.py [options] file1 file2 ...

options:

  -h, --help            show this help message and exit

  -r, --resize          Resize the picture to the dimensions

  -c, --crop            Crop the picture to the dimensions

  -R, --ratio           Apply the ratio of width/height to the picture instead
                        of the actual dimensions

  -W WIDTH, --width=WIDTH
                        Width

  -H HEIGHT, --height=HEIGHT
                        Height

  -d, --decrease        Only apply resizing if at least one of the dimensions
                        needs to be decreased

  -i, --increase        Only apply resizing if at least one of the dimensions
                        needs to be increased

  -f FORMAT, --format=FORMAT
                        Text format string that defines the name of the file
                        to save to. It represents a format as understood by
                        Python "%" feature. If the value is "%s", the name of
                        the output file will be the same as the input file.

  -e EXTENSION, --extension=EXTENSION
                        Extension of the image that will be created. Possible
                        values are 'jpg', 'gif', 'png', 'bmp', 'tif'

  -q QUALITY, --quality=QUALITY
                        Quality

  -D DESTINATION, --destination=DESTINATION
                        Destination directory where the new files should be
                        saved.

When invoking the command, the output will show the action, quality, and then give details for each image worked on.

$ ./modImage.py -c -W 1280 -H 1024 Desktop/Screenshot.png
Action: crop
Quality set to 85
------------------------------------------------------------
Opened image Desktop/Screenshot.png: Width 1680, Height 1050
Cropping to W 1280 H 1024
Saved to file Desktop/Screenshot_crop_1280_1024..png
------------------------------------------------------------

Examples:

To crop your wallpapers to the size of your screen:

$ ./modImage.py -c -W 1280 -H 1024 Desktop/Screenshot.png

With the previous example, only images larger than the dimensions are cropped. One annoying problem with desktop background is that it is usually resized to fit the screen. Since a lot of screens are now widescreens, images are distorted. To avoid this problem, you can use the -R option to crop your wallpapers so that they have the same ratio as your screen:

$ ./modImage.py -c -R -W 1920 -H 1200 Screenshot.png

To resize the image to a certain size:

$ ./modImage.py -r -W 800 -H 600 Screenshot.png

To resize to a certain width, and keep the same ratio for the picture:

$ ./modImage.py -r -W 800 Screenshot.png

In the previous example, all images are resized, meaning that pictures smaller than the constraint are made larger. If you want that only pictures larger than the dimensions are made smaller, and keep small pictures as-is, use the -d option.

$ ./modImage.py -r -d -W 800 Screenshot.png

You can combine the -R option with a resizing. What it does is that it resizes either the width or the height of the picture so that it has the same ratio as the one you give.

$ ./modImage.py -r -R -W 800 -H 600 Screenshot.png

It is possible to just convert the picture to a different format with the -e option:

$ ./modImage.py -e jpg Screenshot.png

When performing an action on an image, the program appends some text to the end of the filename of the modified picture. This way the default behavior won’t overwrite the original picture. With the -f option, you can choose an arbitrary format for the name of the file to save to:

$ ./modImage.py -r -W 800 -f "%s_min" Screenshot.png

You can also choose to overwrite the original images by using the “%s” string:

$ ./modImage.py -r -W 800 -f "%s" Screenshot.png

It is possible to apply the modifications to a group of files. The following example resizes all images in the folder “wallpapers” to a width of 500, only for those with a width larger than 500, keeping the same ratio, and converts them to jpg format.

$ ./modImage.py -r -d -W 500 -e jpg wallpapers/*

To-do

There are a couple of things I would like to add:

  • Options to modify some properties of the picture like brightness, contrast, etc.

Comments are closed.

Trackback this Post |