How to divide photo to X and Y pieces

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
chainas

How to divide photo to X and Y pieces

Post 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

Image after:
Image
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: How to divide photo to X and Y pieces

Post 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
Last edited by Bonzo on 2009-11-19T12:13:54-07:00, edited 2 times in total.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to divide photo to X and Y pieces

Post 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
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: How to divide photo to X and Y pieces

Post 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 ?

Image

Code: Select all

convert input.jpg -background NavajoWhite -gravity south -splice 0x20+0+0 output.jpg 
chainas

Re: How to divide photo to X and Y pieces

Post 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 ?
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: How to divide photo to X and Y pieces

Post by Bonzo »

How are you running your code ?
What version of ImageMagick are you using ?
chainas

Re: How to divide photo to X and Y pieces

Post 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.
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: How to divide photo to X and Y pieces

Post 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.
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: How to divide photo to X and Y pieces

Post 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">
Image
chainas

Re: How to divide photo to X and Y pieces

Post 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 ?
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: How to divide photo to X and Y pieces

Post 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.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to divide photo to X and Y pieces

Post 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
Last edited by fmw42 on 2009-11-19T18:25:48-07:00, edited 1 time in total.
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: How to divide photo to X and Y pieces

Post 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.
chainas

Re: How to divide photo to X and Y pieces

Post by chainas »

Thanks all for help. I made everything with "-draw line" command. Code looks not so beauty, but enough for now :D
rebeltaz

Re: How to divide photo to X and Y pieces

Post 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
Post Reply