Introduction to Image Operations in Python

Discussion in 'Python' started by pradeep, Jun 11, 2012.

  1. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in
    Image manipulation is an interesting subject, as well almost inevitable in today's websites, you can use it to render CAPTCHA, crop & resize images, convert between image formats and many more advanced functions. Python with it's growing popularity has encouraged the community & enterprises to come up with modules for image processing in Python, there are many available modules in Python which will help you manipulate images, but in this article we'll be looking into the Python Imaging Library developed by pythonware.com. PIL is able to read a large number of image formats, but it has been restricted to writing out to only the popular formats.

    Installing Python Imaging Library



    Get the latest stable version of PIL from http://www.pythonware.com/products/pil/. Make sure you have Python development files on your system, in case you installed a Python binary. Untar & run python setup.py install, in case you run into any problem or you would like to customize your installation please refer to the README.

    Basic Usage



    By basic usage I mean opening an image, identifying the format, creating thumbnail, writing out in a different format than the source, etc. I can explain the functions but that is already done in the PIL documentation, so like always let me help you understand using a demo program with comments at appropriate places.

    Code:
    import Image
    
    ## the image being used in the demo is the G4E banner image http://i1.g4estatic.com/bannertm.gif
    path_to_image = '/tmp/pradeep/bannertm.gif'
    
    ## open the image
    img = Image.open(path_to_image)
    
    ## print the dimension and image format
    print "Format: ",img.format ,"| Dimension: %d x %d" % img.size
    
    ## change mode to RGB if not so
    if img.mode != "RGB":
        img = img.convert("RGB")
    
    ## now let's create a thumbnail (96x96) of the image and save it as JPG
    
    thumb_dimensions = (96,96)
    thumb_image_path = '/tmp/pradeep/g4e_thumb.jpg'
    
    ## create thumbnail
    img.thumbnail(thumb_dimensions)
    
    ## save the file in the path specified, if you do not specify the output format
    ## PIL will try to guess it from the output filename
    img.save(thumb_image_path, "JPEG")
    

    Advanced Functions



    There are a wide variety of advanced functions available in PIL, but it is out of the scope of this article to cover all the functions, so here we'll be looking into some popular operations like crop, image transform etc. Please follow the code sample below:

    Code:
    import Image
    
    ## here let's try using a PNG image
    path_to_image = '/tmp/pradeep/roundcube_logo.png'
    thumb_image_path = '/tmp/pradeep/roundcube_logo_crop.png'
    thumb_dimensions = (96,96)
    
    ## open the image
    img = Image.open(path_to_image)
    
    ## let's specify the crop area, here's the format of specifying crop area co-ordinates
    ## (left, upper, right, lower)
    box = (20, 0, 50, 20)
    cropped_img = img.crop(box)
    
    cropped_img.save(thumb_image_path)
    
    ## let's rotate the image counter-clockwise 
    rotated_img = img.rotate(45)
    
    ## the above can be done this way too
    rotated_img = img.transpose(Image.ROTATE_90)
    
    ## we can also flip an image, thereby creating a mirror image
    mirror_img = im.transpose(Image.FLIP_LEFT_RIGHT)
    
    ## using in-built filters, for better resized images & thumbnails
    ## this will create the best thumbnail
    img.thumbnail(thumb_dimensions,'ANTIALIAS')
    
    The PIL has many sub-modules which offer more extended functionality ImageFont, ImageEnhance, ImageDraw and so on, for a full list and documentation please refer to the PIL documentation.
     
  2. Scripting

    Scripting John Hoder

    Joined:
    Jun 29, 2010
    Messages:
    421
    Likes Received:
    57
    Trophy Points:
    0
    Occupation:
    School for life
    Location:
    /root
    This is really good tut! I haven't found better even on whole internet than this one. Appreciate it.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice