crop image with imagemagick offset given in percentage
imagemagick's crop command supports cropping to a percentage of an image but the offset values must be specified in pixel values, e.g.:
convert image.png -crop 50%x+10+20
I want to crop with offset values
y given in percentage of the image width, and height respectively. The pixel values can be calculated, for instance if the image size is
100x200 an offset of 10% would result in 10 and 20 respectively. Is it possible to do this calculation as part of the call to
convert? Width and height are available as
%h at some places, but this does not work:
convert image.png -crop 50%x+(0.1*%w)+(0.1*%h)
If you're running IM v6 you can use FX expressions with "-set" to set image attributes. By setting the page geometry you can specify the offsets to a calculated percentage and do the crop like this...
convert image.png -set page -%[fx:w*0.1]-%[fx:h*0.1] -crop 50%x+0+0 result.png
That reads the image, sets the geometry for the upper left corner to a location outside the original canvas, and crops to the new top left corner specified by the geometry.
Note the offsets are negative numbers.
Also, if you're doing additional processing in the same command you'll probably want to "+repage" after the crop in order to reset the page geometry to the new WxH+0+0.
Edited to add: You can even include the width and height dimensions for the crop when using "-set page". This command would crop an output of 50% the input width and height, and starting at 10% in from the left and top...
convert image.png \ -set page %[fx:w*0.5]x%[fx:h*0.5]-%[fx:w*0.1]-%[fx:h*0.1] -crop +0+0 result.png
Notice how the crop operation is simply "-crop +0+0" since the dimensions and offsets are in the page geometry.
This method lets you use more complex calculations than just using a percent or number of pixels for the cropped output dimensions.
Crop, By setting the page geometry you can specify the offsets to a calculated percentage and do the crop like this That reads the image, sets the geometry for the upper left corner to a location outside the original canvas, and crops to the new top left corner specified by the geometry. Crop image using target width,height and offset in pixel. Crop the image to target size 240×160 with crop starting point at x=100,y=50 $ convert -crop 240x160+100+50 convert-crop-img1.jpg convert-crop-img2.jpg Here is the outcome image (convert-crop-img2.jpg): Crop image using target width,height in percentage. Crop the image to target size 50%x50% with crop starting point at x=100,y=50
You cannot do that in ImageMagick 6. But you can do that in ImageMagick 7.
magick image.png -crop "50%x+%[fx:0.1*w]+%[fx:0.1*h]" +repage result.png
In ImageMagick 6, you need to do the computations ahead of the command, store them in a variable and use the variable in the crop command.
However, in ImageMagick 6, you can do the equivalent using -distort with viewport processing as follows:
convert image.png -set option:distort:viewport "%[fx:0.5*w]x%[fx:0.5*h]+%[fx:0.1*w]+%[fx:0.1*h]" -filter point -distort SRT 0 result.png
crop image with imagemagick offset given in percentage, How to crop image using ImageMagick What method (or rather what tool) do you use to determine the values for offset, width and height? ImageMagick has way too many features to describe here (check the partial list on its site), but the one I was interested in was tile cropping, which subdivides one large image into many smaller images. There are many ways to do auto-cropping, but I just wanted the most basic: For a given input image, create multiple output files using a
With v7 ImageMagick, make start image:
magick -size 200x100 gradient: a.jpg
Now crop using lots of calculated widths, heights, offsets:
magick a.jpg -crop "%[fx:w*0.9]x%[fx:h*0.8]+%[fx:w*0.1]+%[fx:h*0.05]" b.png
identify b.png b.png PNG 180x80 200x100+20+5 8-bit Gray 256c 408B 0.000u 0:00.000
If you only have v6, use
bash and integer arithmetic:
read w h < <(identify -format "%w %h" a.jpg) convert a.jpg -crop $((w*80/100))x$((h*90/100))+$((w*10/100))+$((h*5/100)) result.png
identify result.png result.png PNG 160x90 200x100+20+5 8-bit Gray 256c 412B 0.000u 0:00.000
How to crop image using ImageMagick – Code Yarns , Imagemagick convert is pretty handy tool to manipulate images and can Crop image using both target width,height and offset in percentage. The output image, or 'missed' image, is a minimal image, one pixel in size at a 0 offset, but with original images page or canvas size, as well as any other meta-data the image may have associated. Here it represents the 'empty' or 'zero sized' image that should have been returned by " -crop ", but as no image format can output a image of 'zero
How to crop an image using imagemagick convert, Offsets, if present in the geometry string, are ignored, and the -gravity option has no effect. the image pixels so that those that are moved out of the image area are cut off. blend an image into another by the given absolute value or percent. For example if an image sampled so that you have 5 pixel sub regions, then (For example, sampling an image that has 100 pixel across, down to 20 pixel samples), then you can use an sampling offset percentage in the range 0 to 19.9 to select the first pixel in each region, 20.1 to 39.9 for second, and so on.
ImageMagick: Command-line Options, Use this option to assign a specific comment to the image. Optionally you For example to crop the image by ten percent on all sides of the image, use -crop 10%. Omit the x and y offset to generate one or more subimages of a uniform size. If both percentages are given, each image are dissolved to the percentages given. Note that dissolve percentages do not add, two opaque images dissolved '50,50', produce a 75% transparency. For a 50% + 50% blending of the two images, you would need to use dissolve values of '50,100'.
ImageMagick image transforming utility, [-adaptive-blur • -adaptive-resize • -adaptive-sharpen • -adjoin • -affine Offsets, if present in the geometry string, are ignored, and the -gravity option has no effect. The opacity of the composite image is multiplied by the given percent, then it Now to do what you want first -crop the image to remove excess, then use the same dimensions with -extent to 'pad out' the image with the current background color setting. Code: Select all convert mypic.jpg -gravity Center -crop 500x100+0+0 -background white -extent 500x100+0+0 newmypic.jpg