When an image is rotated by convert -rotate command the image size is enlarged. Is there a way to rotate around the center and to keep the image size, cropping the edges?
convert image.jpg -distort SRT -45 rotate_cropped_image.png
See http://www.imagemagick.org/Usage/warping/#animations
Example:
See also help on -distort: http://www.imagemagick.org/script/command-line-options.php?#distort
This seems now to simply "just work" -- for counter-clockwise 90 degrees:
$ convert image.jpg -rotate -90 rotated_ccw.jpg
If you know the size of the image the following works:
convert -rotate 45 -gravity center -crop NxN input output
tested with square images.
there may be a way to specify NxN is the input image size.
I've found this answer on Imagemagick forum:
A simple solution without knowing what the original size of the image
was, is to use the Alpha Composite Operator 'Src' as a 'crop to this
image size' type of operation. See:
http://www.cit.gu.edu.au/~anthony/graphics/imagick6/compose/#src
For example (ImageMagick version 6 only):
convert image.jpg \( +clone -background black -rotate -45 \) \
-gravity center -compose Src -composite rotate_cropped_image.png
Related
I'm using ImageMagick to trim some PDFs of text that I've converted to jpg for a project. In most cases it works well but when the page has just a small amount of text, say half a sentence, trim works too well so the image is just that, half a sentence.
It's a problem as it causes some display issues where I'm presenting it so it'd be helpful if there was a minimum image size. Is there a way of doing that in ImageMagick? Or adding padding to an image if it is converted below a certain size?
This is the command I'm using:
convert '*.jpeg' -fuzz 1% -trim +repage -set filename:base "%[basename]" "%[filename:base].jpg"
I'm using ImageMagick 7.1.0-19 Q16-HDRI arm 2021-12-22
Currently there is no way that I can think that would limit the trim in that way in Imagemagick. But you can automatically pad the image with some color such as white to some minimum size.
However, you will need to use "magick" in place of "convert" for Imagemagick 7 to make it work properly.
Input (256x256):
Lets pad it with white to 100x100 (minimum)
magick image.png -fuzz 1% -trim +repage -gravity center -bordercolor white -border "%[fx:w<100?round(0.5*(100-w)):0]x%[fx:h<100?round(0.5*(100-h)):0]" result.png
or better
magick input.png -fuzz 1% -trim +repage -gravity center -background white -extent "%[fx:w<100?100:w]x%[fx:h<100?100:h]" result.png
I had the exact same question recently, and the answer was to combine -trim and -extent.
convert -trim -background none -gravity center -extent 1024x1024 input.png output.webp
The image will first be trimmed, then extended to a given size.
You might want to change -gravity and -background depending on your use case.
I'm trying to crop an animated gif usind convert -crop. In some cases it's necessary to add more background to the image to fit. With other image formats it's done with
convert original.gif -rotate 0 -crop 1519x759-237-61\! -background white -flatten edited.gif
For gifs I tried
convert original.gif -coalesce -rotate 0 -crop 1519x759-237-61\! +repage edited.gif
convert is clipping/trimming the background and just the "subject" of the image is shown.
Example is here: https://imgur.com/ls1ED0Z
Result is here: https://imgur.com/59678cD
Expected Result is https://imgur.com/vZGaD7r
I added the red border to show how big the image is. Someone with a great solution? :)
Jan
To do what you want in Imagemagick, you should be using -extent rather than -crop in order to extend the size of the output.
Try this. Adjust the size and offset and background color as desired.
convert original.gif -coalesce -rotate 0 +repage -background white -extent 1519x759-237-61! -bordercolor red -border 3 result.gif
I have added a red border around the image.
With IM you can choose to "-crop", which is most often used to reduce the viewport dimensions, or "-extent", which can also be used to enlarge the viewport. It looks like there are a couple other issues with achieving the result in your sample image, but for starters, try simply substituting your "-crop" with "-extent" like this...
convert original.gif -coalesce -rotate 0 -extent 1519x759-237-61 +repage edited.gif
That will put your input image in a viewport of 1519x759, and place its upper left corner at 237 pixels from the left and 61 pixels from the top. You shouldn't need the exclamation point "!" in any case.
How can I use ImageMagick (probably the montage command) to join png images into a new png, where each of the input pngs becomes a part of the final one? All vertically arranged?
Like so:
2 pngs of roughly equal width (±2px) and different heights joined on top of one another seamlessly.
I don't really mind so much what happens to excess/smaller widths.
convert 1.png 2.png -append result.png
To align left edges, add -gravity West.
To align right edges, add -gravity East.
To align centres, add -gravity center.
To add a transparent spacer between:
convert -background none 1.png -size 10x10 xc:none 2.png -append result.png
Oh, you've gone horizontal! Change -append to +append.
this one:
convert -append img1.png img2.png out.png
e.g. from
will get
And the:
convert +append img1.png img2.png out2.png
will produce
I have two images and I want to crop a part of the first one, and paste it into the other one in a specific place using imagemagick. Also the crop part would be gray
I want this: http://imgur.com/3PomJ9k
But I got this: http://imgur.com/5XmhytN
I have tried:
convert source.jpg ( +clone -crop 240x270+595+140 -resize 112x146 -type Grayscale ) -geometry +10+200 -composite destiny.jpg
but as you see, it does not works as expected.
Although the crop part is cloned as gray, the whole first image is cloned too, but I need to keep in background the second image in color and the crop part in gray in front of it.
Some ideas?
From our discussion in chat, I think you need something along these lines - you may need to fiddle with the actual numbers but hopefully the concept is clear:
convert other.jpg \( source.jpg -crop 240x270+595+140 -colorspace gray -resize 116x150! \) -geometry +4+108 -composite result.png
I would like to zoom into an image test.png. That is, as a silly example, I would like to go from this
to this
The only way I was able to do it was with
convert -resize 110% test.png -gravity Center \
-crop 800x600+0+0 +repage 1.png
This is re-scaling the picture and then cropping it to its original size. The problem is that this only works because I already know that the original size of the image is 800x600.
How can I make this work without knowing the size of the image beforehand?
The way to do this, apparently, is with the -distort option:
convert -distort SRT '1.1 0' +repage test.png 2.png
where 1.1 is a 110% zoom and the 0 is a rotation angle.