Page 1 of 1

Emulating convert -crop using MagickWand

Posted: 2013-01-05T15:57:43-07:00
by ruarl
Hi,
I'm trying to work out the best way to chop up a large image into a load of rectangular, equal-sized tiles.
I'm inspired by the ability to use the following command line:

Code: Select all

convert infile -crop widthxheight outfile 
with no x and y offset arguments to generate a set of tiles of size width by height covering the whole image. (As described here: http://www.imagemagick.org/script/comma ... s.php#crop )

For my application these tiles are to be saved to disk as binary data. As far as I can tell, ImageMagick doesn't support the right format, otherwise I'd just use the command line. There may be another way of doing this with the command line, but I'm intrigued as to how ImageMagick works as much as anything. Instead, I've started trying to crop images using the MagickWand C interface.

I have implemented a single crop easily enough with MagickCropImage(). I realise I could just duplicate my image a whole bunch of times, and crop a different tile each time. I'd like to know if the wand still maintains the image data outside the cropped region somehow. If so, is there a way to move the crop 'window', such that I could write all these tiles to disk from a single image in memory?

I hope this is clear.

Re: Emulating convert -crop using MagickWand

Posted: 2013-01-05T20:48:27-07:00
by fmw42
I cannot answer your questions about the API, but if you really want equal sizes and use the command line, then take a look at http://www.imagemagick.org/Usage/crop/#crop_equal

Re: Emulating convert -crop using MagickWand

Posted: 2013-01-10T14:41:01-07:00
by el_supremo
I think this function will do what you want:

Code: Select all

%  MagickGetImageRegion() extracts a region of the image and returns it as a
%  a new wand.
%
%  The format of the MagickGetImageRegion method is:
%
%      MagickWand *MagickGetImageRegion(MagickWand *wand,
%        const size_t width,const size_t height,const ssize_t x,
%        const ssize_t y)
%
%  A description of each parameter follows:
%    o wand: the magick wand.
%    o width: the region width.
%    o height: the region height.
%    o x: the region x offset.
%    o y: the region y offset.
This will return a region in a wand which you can then write to a file, delete the wand and then get the next region.
What sort of binary format do you need for the output file?

Pete

Re: Emulating convert -crop using MagickWand

Posted: 2013-04-16T18:54:53-07:00
by anthony
I doubt it would work... -write would cancle the region before the image is written.

See Regions, and how they work
http://www.imagemagick.org/Usage/masking/#regions

Crop is called TransformImage() and like crop it can generate tiled images. I think the function however has bee split into soem sub-functions though do look at the code.