Rotate and crop an image, while keeping its original size - imagemagick

I have a JPG image with a known size of 3072x2048. Now I want to rotate that image by any degrees (e.g. 45), while keeping its original size. Thus - using ImageMagick on the command line - I first want to rotate, then crop the image, like this:
convert -rotate 45 -gravity center -crop 3072x2048 +repage original.jpg rotated-45.jpg
By using -gravity center I specify to crop the center part of the image, which is what I want. This operation produces four output images:
rotated-45-0.jpg
rotated-45-1.jpg
rotated-45-2.jpg
rotated-45-3.jpg
The first image rotated-45-0.jpg is exactly the final image I want to get. The other three I don't need. I could delete them, but I think it would be nicer to not generate these "extra" images in the first place. So I thought I could do it with this command instead:
convert -rotate 45 -gravity center -crop 3072x2048+0+0 +repage original.jpg rotated-45.jpg
This only produces one output image, however, now the top-left corner of the image is being cropped. So apparently the -gravity center is not used any longer.
Any ideas what I am missing here?

Using ImageMagick you can rotate an image any number of degrees while keeping the original canvas dimensions using "-distort SRT"...
convert original.jpg -virtual-pixel black -distort SRT 45 rotated-45.jpg
Use "-virtual-pixel" to specify how you want to handle the parts that were outside the canvas before the rotation. In this example I used "black". You can use black, white, background, tile, mirror, or none.

Related

Splitting a region of an image into tiles

I have a set of images that are 4500px x 3000px in size.
Out of each of these images I need to extract a set of tiles in a 5 columns by 3 rows combination with each tile being 700px wide by 500px high.
What complicates the above is that the starting point for this needs to be at X:650 and Y:450 position from the top left corner of the image.
I have gotten as far as convert image.jpg -gravity NorthWest -chop 650x450 -crop 700x500 tile-%d.jpg
This gets me to a correct start position for creating the tile set but includes the rest of the blank area in the right and bottom edge of the image.
How do I go about solving this?
Within a single ImageMagick command you can crop the initial rectangle that contains all the tiles first, then crop that into the 15 output images. Try something like this...
convert image.jpg -crop 3500x1500+650+450 -crop 5x3# tile-%02d.jpg

How to move an image on a canvas to a specific location using ImageMagick?

I am using ImageMagick to place an image on particular location on a white background canvas, can someone help me with it?
Here is what i tried so far, this would resize my image to correct resolution and put on a right size canvas.
Convert image.jpg -resize 1025x1537 -background white -extent 1920x1536
Now, I need to move it 40 pixles to the right , 20 to bottom
Reading the documentation for the geometry argument, it seems like one can use:
... -extent 1920x1536+40+20
If you want to define the offset from another origin, then you can use -gravity type.
https://imagemagick.org/script/command-line-processing.php#geometry
https://imagemagick.org/script/command-line-options.php#gravity

Imagemagick crop into tiles and force all to have same size?

I want to crop an image into 2048x2048 slices like this:
convert big.jpg -crop 2048x2048 tile%04d.jpg
However, if the original is for example 5764x3888, the tiles end up like this:
2048x2048
2048x2048
1668x2048
2048x1840
2048x1840
1668x1840
How can I force each tile to be 2048x2048 and sit in the upper left corner?
I would prefer if I could do this in just a command line without a script, since I will do this on both Mac and Windows.
Or do I have to make the original image 6144x4096 first, so it's evenly divisible by 2048 and then crop it?
Thanks!
I would use this:
convert big.jpg -crop 2048x2048 -background blue -gravity northwest -extent 2048x2048 tile%04d.jpg
Obviously choose a different colour background to suit your needs, and choose a different -gravity to determine where the cropped image sits on its extended canvas.

Crop and merge two images using ImageMagick

I am trying to use ImageMagick to place one image (Poseidon_map01.jpg in my code below) in the top-left corner of another (zzOcean Backdrop.png) then crop the resulting image down to 1280x720.
This is the command I currently have:
".\ImageMagick-7.0.8-12-portable-Q16-x64\magick.exe" ".\Raw\zzOcean Backdrop.png" ".\Raw\Poseidon_map01.jpg" -gravity northwest -composite -crop 1280x720>! +repage ".\1280x720\Poseidon_map01.jpg"
The problem is that this command is cutting the composited image into 1280x720 pieces then saving all of those pieces with the names Poseidon_map01-1.jpg to Poseidon_map01-48.jpg. Poseidon_map01-1.jpg is the top-left corner of the composited image and this is the piece that I want to keep. I want to discard the rest of the composite. Does anyone know what I have to change in my command to make this happen? Thanks.
In Imagemagick crop, if you do not include the +X+Y offsets, it will crop as many pieces as it can given the size you specify. That is called tiled cropping. So use -crop WxH+X+Y>!
See https://imagemagick.org/Usage/crop/#crop
".\ImageMagick-7.0.8-12-portable-Q16-x64\magick.exe" ".\Raw\zzOcean Backdrop.png" ".\Raw\Poseidon_map01.jpg" -gravity northwest -composite -crop 1280x720+0+0>! +repage ".\1280x720\Poseidon_map01.jpg"
I am not a windows user and syntax may vary some. You may need to escape the > or ! with ^ on windows or put double quotes around the whole crop set of arguments. Keep that in mind, if this does not work. See https://imagemagick.org/Usage/windows/

convert debian-logo.jpg -crop 16x16-16-16 debian_crop.jpg error

I want to cut an image called debian-logo.jpg with the following command:
convert debian-logo.jpg -crop 16x16-16-16 debian_crop.jpg
But for some reason there is an error. Could someone tell me how to fix it?
How big is your input jpg? Using negative crop offsets in ImageMagick only works with -gravity center (and sizes larger than 32x32). Otherwise, the crop will be off the image bounds for any other gravity setting. Crops are always relative to the gravity setting, which defaults to the -gravity northwest. See http://www.imagemagick.org/Usage/crop/#crop_gravity
If you want to crop inside the image with an offset, then the crop offsets need to be positive and will be cropped inside using positive offsets, even with -gravity south, east etc. The positive offset move inward relative the corner or side indicated by the gravity setting.
Are you trying to extend and pad the image in some direction with the crop. If so, then use
convert debian-logo.jpg -background black -gravity northwest -extent 16x16-16-16 debian_crop.jpg

Resources