I am using the imagemagick module with Nodejs
im = require('imagemagick');
The imagemagick module uses the imagemagick command line tools. I use the convert method to crop an image
im.convert([image_path, '-crop', '200x150', '-gravity', 'center', target_path],
function(err, stdout){}
);
This results in two images. The one with the cropped image area - the second with the image garbage i tried to get rid of.
How can i force imagemagick to output one image file only?
Per the imagemagick documentation for cropping, which is admittedly a little obtuse (emphasis added):
The width and height of the geometry argument give the size of the image that remains after cropping, and x and y in the offset (if present) gives the location of the top left corner of the cropped image with respect to the original image.
...
If the x and y offsets are present, a single image is generated, consisting of the pixels from the cropping region.
...
If the x and y offsets are omitted, a set of tiles of the specified geometry, covering the entire input image, is generated.
... so, you just need to specify your x
and y
offsets as part of your geometry argument, like so: 200x150-100-75
Notice that I've specified -100
and -75
for the upper left corner of your crop region, this is because you set your gravity
to center
, but it appears that imagemagick tries to intelligently determine the appropriate distance target based on your gravity, and I don't see exactly how it behaves when you choose center
. So you may have to play around with this one a bit, or you could omit the gravity
and use the actual offset from the top left corner of your original image.