Emulating convert -crop using MagickWand

The MagickWand interface is a new high-level C API interface to ImageMagick core methods. We discourage the use of the core methods and encourage the use of this API instead. Post MagickWand questions, bug reports, and suggestions to this forum.
Post Reply
ruarl
Posts: 1
Joined: 2013-01-05T15:30:44-07:00
Authentication code: 6789

Emulating convert -crop using MagickWand

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

Re: Emulating convert -crop using MagickWand

Post 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
el_supremo
Posts: 1015
Joined: 2005-03-21T21:16:57-07:00

Re: Emulating convert -crop using MagickWand

Post 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
Sorry, my ISP shutdown all personal webspace so my MagickWand Examples in C is offline.
See my message in this topic for a link to a zip of all the files.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Emulating convert -crop using MagickWand

Post 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.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply