1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2008
    Rep Power

    Divide image into 6 parts

    Hi, I have an image which has a certain height n pixels. I would like to divide this to 6 parts so that each part has a height of n/6 pixels. The length will be the same for each. How would I be able to achieve this? Currently I have the Python Imaging Library imported, then have the ff:

    im = Image.open('somepic.bmp')
    for example, if somepic.bmp has a height of 270 pixels, then it will be divided into 6 images each with a height of 45 pixels.
  2. #2
  3. Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Aug 2011
    Rep Power
    I've never used PIL. I suppose your project is possible as stated. Anyway, install imagemagick, bash, and gawk if you haven't already got them. Write the following shell script into the file windowBlinds.sh , change the mode to executable, then run with the command

    path/windowBlinds.sh NameOfPicture.picture_type

    For example, I tested using the Bourne again shell command:

    ./windowBlinds.sh BrideAndGroom.jpg | bash

    it made the pictures, correctly, as

    identify $1 | gawk --assign NAME="$1" '{split($3,size,/x/);for(i=0;i<6;++i){printf"convert -crop %dx%d+0+%d %s %d%s\n",size[1],size[2]/6,i*size[2]/6,NAME,i,NAME}}'
    Imagemagick handles a great many picture formats. I've written the code to give output in the same picture format as input---because it's easier and you didn't give other instructions. convert uses an internal form so can input one picture format and output another without trouble.

    [Edit: this solution reloads the picture 6 times. If you need to do this in real time or if performance is an issue for whatever reason, a gimp script or the PIL might be preferable. Actually, imagemagick features a language as well so you could probably do it using the magickwand.]
    Last edited by b49P23TIvg; July 3rd, 2012 at 11:23 AM. Reason: clarification
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo