Page 1 of 2
How to divide photo to X and Y pieces
Posted: 2009-11-19T10:34:04-07:00
by chainas
Hello,
i need to divide photo to X * Y pieces. That are separated N px line each other.
Image (4 x 3) = 12 pieces:
Image before:
Image after:

Re: How to divide photo to X and Y pieces
Posted: 2009-11-19T12:05:52-07:00
by Bonzo
Sorry misread the post - you want to add the red lines to the image ?
Code: Select all
convert -draw -stroke red -strokewidth 10 -draw "line 50,0 50,200" -draw "line 100,0 100,200" -draw "line 0,50 200,50" output.jpg
Draw as many lines as you want and modify the positions to suite.
Original reply was:
-crop is what you want BUT you need to work out the size of the parts.
If you are using php, batch scripts etc. you could automatical do the calculations.
Code: Select all
convert flowers.jpg -crop 200x200 image-%d.jpg
Re: How to divide photo to X and Y pieces
Posted: 2009-11-19T12:09:07-07:00
by fmw42
Is this actually 12 separate images or just one image with a grid overlaying it?
convert image.jpg -crop 25%x33.333% +repage image_%d.jpg
should make an array of 12 separate images number 0-11. An example of 2x2 is at
http://www.fmwconcepts.com/imagemagick/ ... .php#crop2
Then you may have to chop off edges using -shave or -chop, if that is what you are doing. See
http://www.imagemagick.org/Usage/crop/#shave
http://www.imagemagick.org/Usage/crop/#chop
Otherwise, just composite them back into a grid with the appropriate spacing you want onto whatever color background you want to show between the sections.
If you just want to overlay a grid, see my (unix) script, grid, at
http://www.fmwconcepts.com/imagemagick/grid/index.php
Re: How to divide photo to X and Y pieces
Posted: 2009-11-19T12:23:04-07:00
by Bonzo
If you were using the crop method to crop the images and place back onto a background you may be able to do multiple splices ?
Code: Select all
convert input.jpg -background NavajoWhite -gravity south -splice 0x20+0+0 output.jpg
Re: How to divide photo to X and Y pieces
Posted: 2009-11-19T12:34:59-07:00
by chainas
Really, i just need one image with a grid overlaying it, not 12 separated images.
Now trying this example
http://www.fmwconcepts.com/imagemagick/grid/index.php
Hm, looks like "grid" command doesn't work. Can someone give working example witg grid ?
Re: How to divide photo to X and Y pieces
Posted: 2009-11-19T12:56:21-07:00
by Bonzo
How are you running your code ?
What version of ImageMagick are you using ?
Re: How to divide photo to X and Y pieces
Posted: 2009-11-19T13:01:00-07:00
by chainas
I am using IM on symfony framework as external plugin.
Anyway, command:
Code: Select all
convert -draw -stroke red -strokewidth 10 -draw "line 50,0 50,200" -draw "line 100,0 100,200" -draw "line 0,50 200,50" output.jpg
works fine. But, grid, doesn't.
Re: How to divide photo to X and Y pieces
Posted: 2009-11-19T13:09:06-07:00
by Bonzo
Freds script is a shell script and to run you need to:
1/ Upload to the server and find the path to the script.
2/ CHMOD the script to 777
3/ Using in php like:
Code: Select all
<?php
// Setup the error array
$array = array();
exec("/home/user/public_html/folder/grid -s 200,100 -c red -t 10 -o 1 input.jpg grid.png 2>&1", $array);
//Display any errors
echo "<br><pre>".print_r($array)."<br>";
echo "</pre>";
?>
Using php you should be able to use getimagesize() and devide the width and height to get your variables then use that in Freds script. You may get a remainder and so it would be a good idea to crop or resize the image to a size that diveds well first.
Re: How to divide photo to X and Y pieces
Posted: 2009-11-19T13:21:48-07:00
by Bonzo
Code: Select all
<?php
$size = getimagesize("input.jpg");
$width = round($size[0]/5);
$height = round($size[1]/3);
exec("/home/user/public_html/folder/grid -s $width,$height -c red -t 10 -o 1 input.jpg grid.png 2>&1", $array);
//Display any errors
echo "<br><pre>".print_r($array)."<br>";
echo "</pre>";
?>
<img src="grid.png">

Re: How to divide photo to X and Y pieces
Posted: 2009-11-19T13:44:04-07:00
by chainas
I am trying to override this sfImageMagickAdapter class:
http://trac.symfony-project.org/browser ... .class.php
I tried something like that: $command .= ' grid '.$this->image.' new_'.$this->image; but it does no effect. Maybe there's something wrong with IM version?
I tried install IM local on windows. And when i try to execute "grid" command i receive error. How i can run "grid" command on local installed IM ?
Re: How to divide photo to X and Y pieces
Posted: 2009-11-19T13:53:44-07:00
by Bonzo
I do not know anything about symfony framework and I can not run shell scripts on my localhost either using XAMPP - no idea why.
Sorry I can not be of further help.
As the draw code works you could automate that, although the command line etc. could be quite long.
Get the image size - divide it by the amount of lines - draw the vertical lines from the to to the bottom - draw the horizontal lines from the side to side.
Re: How to divide photo to X and Y pieces
Posted: 2009-11-19T14:09:14-07:00
by fmw42
Grid is a unix bash shell script and requires PHP or IM compiled and accessible on a Unix or Max OSX platform. To use on Windows, you need to have Cygwin installed on Windows or something like it as there are numerous Unix function calls, such as grep, sed, awk, tr, bc, etc.
see IM for Cygwin on Windows at
http://www.imagemagick.org/script/binary-releases.php
However, the basic concept behind the grid script is to use -draw for lines. So one just computes the spacing of the lines and then the end points and draws a sequence of horizontal and vertical lines. see tp://
www.imagemagick.org/Usage/draw/#primitives
Re: How to divide photo to X and Y pieces
Posted: 2009-11-19T15:17:02-07:00
by Bonzo
Grid is a unix bash shell script and requires PHP or IM compiled and accessible on a Unix or Max OSX platform. To use on Windows, you need to have Cygwin installed on Windows or something like it as there are numerous Unix function calls, such as grep, sed, awk, tr, bc, etc.
Thanks for the explanation Fred; I did try Cygwin on my old PC but could not get it to work - the main problem I suppose was that I was not that interested in it !
I did have an old PC running Linux but when I started it up the other day and came back into the room there was a smell of burning electrical items and it was asking for a boot disc
I may convert my old windows XP machine over to Linux and have a play on it again.
Re: How to divide photo to X and Y pieces
Posted: 2009-11-19T15:21:29-07:00
by chainas
Thanks all for help. I made everything with "-draw line" command. Code looks not so beauty, but enough for now

Re: How to divide photo to X and Y pieces
Posted: 2009-12-25T22:54:19-07:00
by rebeltaz
fmw42 wrote:convert image.jpg -crop 25%x33.333% +repage image_%d.jpg
should make an array of 12 separate images number 0-11.
I just wanted to say that that code worked great for me to split an image into 9 separate images (after changing 25% to 33.333%, of course), but only after leaving out the -repage option. convert complains that -repage requires an argument.
Derek Tombrello