The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Python Programming
|
Taking sections from a copied image not working
Discuss Taking sections from a copied image not working in the Python Programming forum on Dev Shed. Taking sections from a copied image not working Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

September 29th, 2012, 02:42 AM
|
|
Registered User
|
|
Join Date: Jul 2008
Posts: 23
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()
|

September 29th, 2012, 10:48 AM
|
 |
Contributing User
|
|
|
|
__________________
[code] Code tags[/code] are essential for python code!
|

September 29th, 2012, 03:52 PM
|
 |
Contributing User
|
|
|
|
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.
|

September 29th, 2012, 06:36 PM
|
|
Registered User
|
|
Join Date: Jul 2008
Posts: 23
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
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|