Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old September 29th, 2012, 02:42 AM
ocpaul20 ocpaul20 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2008
Posts: 23 ocpaul20 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 50 m 15 sec
Reputation 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()

Reply With Quote
  #2  
Old September 29th, 2012, 10:48 AM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,389 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 14 h 22 m 25 sec
Reputation Power: 383


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!

Reply With Quote
  #3  
Old September 29th, 2012, 03:52 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,389 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 14 h 22 m 25 sec
Reputation Power: 383
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 03:57 PM.

Reply With Quote
  #4  
Old September 29th, 2012, 06:36 PM
ocpaul20 ocpaul20 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2008
Posts: 23 ocpaul20 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 50 m 15 sec
Reputation 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 

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Taking sections from a copied image not working

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap