I'm trying to crop image by height with this command line:
convert 1053257.png -gravity South -crop 2910x3312+0+0 -background black +repage image-cropped-top.png
The generated image is not cropped correctly, as the dimensions after running the command are 2791 x 3312.
The width is cropped as well!
Can anyone help with this?
The general form is:
convert input.jpg -crop WIDTHxHEIGHT+0+0 result.jpg
If you want to crop to a specific width, say 1024, leaving the height unaffected:
convert image.jpg -crop 1024x+0+0 result.jpg
If you want to crop to a specific height, say 768, leaving the width unaffected - note the height is after the x:
convert image.jpg -crop x768+0+0 result.jpg
If you want to crop to a maximum width and height, say 1024 wide by 768 tall without distorting the aspect ratio:
convert image.jpg -crop 1024x768+0+0 result.jpg
If you want to crop to a specific width and height, say 1024 wide by 768 tall and are happy to allow gross distortions:
convert image.jpg -crop 1024x768+0+0\! result.jpg
Think of the exclamation mark as meaning "just do it!". Note that the backslash is only needed on Linux/Unix/macOS to escape the exclamation mark, you omit the backslash on Windows.
Note, if you are saving the cropped image in PNG format, you probably want to reset the page afterwards so the image "forgets" it used to be part of a larger image:
convert input.jpg -crop 1024x768+0+0 +repage result.png
With ImageMagick a problem like that can occur if you've done a "-trim" to the image before the crop. When you "-trim" an image it can still remember the original page dimensions from before the trim, then when you crop it, it uses those page dimensions as the starting reference for the crop. You probably need to "+repage" before the crop to start with fresh paging information. Try something like this...
convert 1053257.png -gravity South +repage -crop 2910x3312+0+0 +repage image-cropped-top.png
Related
Friends,
I need to -trim some images but keep the original canvas size. Works like this:
convert in.png -fuzz 10% -trim -background white -set page "%[fx:w]x%[fx:h]" +repage out.png
But how can I position the trimmed image part at it's original position? -gravitiy center is not an option as the to-be-trimmed part usually not at the canvas center.
Any ideas?
You should be able to -trim an image, then use -flatten to lay it back onto its original canvas. Try this command...
convert logo: -background none -trim -flatten trimmed.png
#GeeMack's answer is certainly simpler and more succinct, but if you need more flexibility for dinking around, another way is to get the image height and width and the trimbox in one invocation and use them in the next - maybe with adaptation.
So, starting with this image:
# Get image width and height and the trim-box
read geom trim < <(magick start.png -format "%G %#" info:)
# Make a new white canvas same size as original and trim new image onto it
magick -size $geom xc:white \( start.png -crop $trim \) -flatten result.png
I put an artificial yellow border around it so you can see the extent of it on SO's white background.
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 x and 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 %w and %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.
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
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
Check:
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
Check:
identify result.png
result.png PNG 160x90 200x100+20+5 8-bit Gray 256c 412B 0.000u 0:00.000
I have a web page where I list images that should be 600px by 600px (width & height). So I resize all big images to 600x600 using imagemagick, which works fine.
However, some images, when I resize them (in order to keep their aspect ratio), get resized to a size smaller than 600px in height (for example 600x450). So I tried to fill the needed height by using imagemagick options (gravity, background, and extent) like so :
image.combine_options do |img|
img.crop('1200x1200+0+0')
img.resize '600x600'
img.background 'white'
img.extent '600x600'
img.gravity 'center'
end
That gave me what I want for these kind of images (short images). However, this effect of centering is applied to the other images (bigger ones) as well ! Is there any way to prevent that ?
Update:
Just to show you what I mean, I made up this example... if I don't add extent and gravity the image will start from the top left corner
but if I add extent and gravity (center) the image will start from the center, something like this :
I want only images that result on resized images smaller than 600x600 to be centered, but in the example as you see even an image that get resized to exact 600x600 get centered !! that's not what I want
the solution :
I end up using shell command using system function in ruby
system "convert #{image.path} -crop 1024x1024+0+0 -resize 600x600 -background white -gravity center -extent 600x600 output.png"
and that worked fine! I don't know why minimagick wasn't working correctly, but I just get rid of that gem from my gemfile which is fine too.
Here is your command in command line Imagemagick with proper resetting of the virtual canvas after the crop.
convert image -crop 1200x1200+0+0 +repage -resize 600x600 -background white -gravity center -extent 600x600 result
Here are two results with slightly different resize arguments. It looks like your commands are using the equivalent of the ^ flag set on the resize argument.
Input:
Proper result without the ^ flag: (Note padded with white)
convert wiki.png -crop 1200x1200+0+0 +repage -resize 600x600 -background white -gravity center -extent 600x600 result1.png
Result with the ^ flag: (Note cropped)
convert wiki.png -crop 1200x1200+0+0 +repage -resize 600x600^ -background white -gravity center -extent 600x600 result2.png
The above is Unix syntax. For Windows double the ^ to ^^, since ^ is an escape character in Windows.
Perhaps your issue is with minimagick and not Imagemagick. You can check by testing my command line.
I have an image of 4500x5400, I want to resize it for the height 4200 keeping the width ratio, however the width of the image needs to be 4050, leaving the sides transparent.
I have this ImageMagick command:
convert file.png -resize 4500x5400 -gravity center -background transparent -extent 4050x4200 out.png
However it's cutting the top and the bottom, while it needs to appear.
Do you guys have any idea of how I can make it work?
Appreciate your time!
Try this...
convert input.png -resize 4050x4200 -background none -gravity center -extent 4050x4200 output.png
The "-resize" fits your input image within a container of that size. The "-extent" makes sure the total canvas is that size. The "-background" and "-gravity" make sure extra space is filled with transparent and that the input image is located in the center of the output canvas.
I have images of unknown size and ratios. I wish to take a centred 16:9 crop.
If all the images were known 4:3, then it's easy: 9/16 รท 3/4 = 0.75 so I simply set the height to 75% of the original like this:
convert photo.jpg -gravity center -crop '100%x75%' +repage photo16x9.jpg
However, if the photo is already 16:9 (or even wider), I don't want to crop it, and if it is 'taller' than 16:9 I want to crop it only by the amount necessary to achieve a 16:9 crop.
This would be also easier if I wanted to scale to a known width or height (example question for that). But I'm looking to leave as much of the original image data in place as poss.
Therefore I'm looking for a way to crop the height to a factor of the image's width, with a cut-off.
I hoped this might work
convert photo.jpg -gravity center \
-crop '100%x[fx:9/16*w]' +repage photo16x9.jpg
but alas it seems the [fx:...] is not allowed in a -crop argument.
Hacking a bit I found somewhere that I could not for the life of me understand(!) also failed, but I'll list it here to show research effort!
convert photo.jpg -set option:distort:viewport \
'[fx: w]x[fx: w*9/16 ]+0+0' -filter point \
-distort SRT 0 +repage photo16x9.jpg
(I realise that neither attempts above cover the case when the image is already wider than 16:9, but if the [fx:...] thing worked I could achieve that by using the ternary operator, so I kept the examples simple.)
Maybe you can just calculate the aspect ratio and use that to make a decision. Create two test images, one 15x9 and one 17x9:
convert -size 15x9 xc:black 15x9.png
convert -size 17x9 xc:black 17x9.png
Now ask ImageMagick to tell you if the aspect ratio is wider than 16:9 or less than or equal to 16:9:
convert 15x9.png -format "%[fx:(w/h)>16/9]" info:
0
convert 17x9.png -format "%[fx:(w/h)>16/9]" info:
1