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

    Join Date
    Jul 2008
    Posts
    29
    Rep Power
    0

    Taking sections from a copied image not working - SOLVED


    SOLVED - see last post
    I wonder if someone could explain what I am doing wrong as I have just started to learn python and obviously it is something I do not understand.

    The following program, takes an image A, copies it to B and then tries to take sections of B and show them individually as C & D.

    It defines a box and then crops from B and pastes into new images C & D.

    The first one(C) works fine but the second (D) appears a red image. Hope the code is not too long.

    Code:
    #!/usr/bin/python
    # 
    # test_things.py
    
    #
    # imports here
    #
    import sys, os
    import Image
    
    def main():
    
        infile = "test_images/AS16_M_0491.jpg"
        sects_wide = 4
        sects_down = 3
    
    #
    # open the image if we can
    #   
        try:
            im = Image.open(infile)        
        except IOError:
            print "Cannot open the input file " + infile
            sys.exit(77)
            pass
    #
    # get image details
    #
        width = im.size[0]
        height = im.size[1]
        mode = im.mode
    
    # new image canvas
        new_w = 2400
        new_d = 2430
        newsize = (int(new_w), int(new_d))
        imnew = Image.new('RGB', newsize, (255,0,0)) # new image same size but black background    
    
    # copy old image into new image canvas
        uleft = 0
        udown = 0
        bleft = new_w
        bdown = new_d
        box = (uleft, udown, bleft, bdown)
        region = im.crop(box) # apply from input image    
        imnew.paste(region, box) # write out the box to output image
        # imnew now contains new copy of original
    
        s_width = 600
        s_depth = 810
    
        # create a new smaller image canvas (C)
        sect_size = ( s_width, s_depth )
        imout = Image.new('RGB', sect_size, (255,0,0) ) # new image section size
        
        # copy section of full sized image to smaller section canvas
        uleft = 0
        udown = 0        
        bleft = 600
        bdown = 810
        box = (uleft, udown, bleft, bdown)        
        region = imnew.crop(box) # apply from input image    
        imout.paste(region, box) # write out the box to output image
        
        # show a thumbnail of new smaller seection of copied original
        size = (s_width/sects_wide),(s_depth/sects_down)  # gets proportioned to original imageout size (256x256) => (50x50)
        imout.thumbnail(size)
        imout.show()
    
        # -------create another new smaller image canvas (D)---------------
        # create a new smaller image canvas
        sect_size = ( s_width, s_depth )
        imout = Image.new('RGB', sect_size, (255,0,0) ) # new image section size
        
        # copy section of full sized image to smaller section canvas
        uleft = 600
        udown = 810        
        bleft = 1200
        bdown = 810
        box = (uleft, udown, bleft, bdown)        
        region = imnew.crop(box) # apply from input image    
        imout.paste(region, box) # write out the box to output image
        
        # show a thumbnail of new smaller seection of copied original
        size = (s_width/sects_wide),(s_depth/sects_down)  # gets proportioned to original imageout size (256x256) => (50x50)
        imout.thumbnail(size)
        imout.show()
    
        sys.exit()
    
    #
    if __name__ == '__main__':
      main()
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,995
    Rep Power
    481


    I stitched images using j in this thread. I doubt it helps you.


    Anyway, I'm otherwise occupied for a while but recommend you investigate a command line with imagemagick to split the image. I think you're a bash guru.
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,995
    Rep Power
    481
    This bash script uses imagemagick programs and gawk to split an image into top and bottom pieces. change the mode to executable. ( chmod +x the_script.sh ) Don't use weird file names (no spaces!).

    Code:
    #!/bin/bash
    
    # Purpose: split an image into top and bottom pieces
    # Output: 2 files in same format as input having names top and bot prepended
    # Use: the argument is the image name to split.
    
    if [[ ! -f $1 ]]
    then
      echo Use: the argument is the image name to split.
      exit 1
    fi
    
    identify $1 | gawk '{
      name = $1
      split($3,g,/x/)
      h = int(g[2]/2)
      printf"convert %s -crop %dx%d+0+0  top%s\n",name,g[1],h,  name
      printf"convert %s -crop %dx%d+0+%d bot%s\n",name,g[1],h,h,name
    }' | bash
    Last edited by b49P23TIvg; September 29th, 2012 at 04:57 PM.
    [code]Code tags[/code] are essential for python code and Makefiles!
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2008
    Posts
    29
    Rep Power
    0
    Thanks for these responses which help go towards my goal.

    The end result I need to get to with this python program is to split the image into a user-specified number pieces, so that a large image is more managable.

    It is also an exercise for me to learn more about python which I have just started learning (having been programming in other languages for years)

    SOLVED
    I was trying to copy too large an area into too small a canvas I think, anyway, I found this snippit of code which solved the problem.

    #
    # copy an area of an image to another image
    # http://www.php2python.com/wiki/function.imagecopy/
    #
    Code:
    def imagecopy (dst_im, src_im, dst_x, dst_y, src_x, src_y, src_w, src_h): 
        src_im_crop = src_im.crop((src_x, src_y, src_x + src_w, src_y + src_h)) 
        dst_im.paste(src_im_crop, (dst_x, dst_y)) 
        return True

IMN logo majestic logo threadwatch logo seochat tools logo