Imagemagick crop while keeping size - imagemagick

I want to crop the top 10 pixels of an image, but to keep the width
All exmaples I found look like
convert rose: -crop 90x60-10-10 crop_all.gif
I need to supply the 90x60 so for the result size
How can I crop with something like +0+10 and not supply a final size

I think you need this to chop 10px off the top edge:
convert rose: -chop 0x10 result.gif
Just for completeness, if you want to chop 10px off the bottom, do this:
convert rose: -gravity south -chop 0x10 result.gif
And to chop 10px off the left edge:
convert rose: -chop 10x result.gif
and 10px off the right edge:
convert rose: -gravity east -chop 10x result.gif
The reason you don't need to specify -gravity for top and left sides is that the default value for the -gravity setting is NorthWest.

Related

Add top and left borders with magick command

Is there a way to add 5 or any number of white/transparent pixels at the top and left borders of an image with the magick command in Linux?
Use the -splice operator. First make a solid magenta rectangle:
magick -size 100x50 xc:magenta image.png
Now splice on a yellow chunk (so you can see it) 10 wide and 20 tall:
magick image.png -background yellow -gravity northwest -splice 10x20 result.png
Change yellow to none for transparent pixels.
Change magick to convert for v6 ImageMagick.
If you just want to splice to the East side:
magick image.png -background yellow -gravity east -splice 10x east.png
If you just want to splice to the South side:
magick image.png -background yellow -gravity south -splice x10 south.png

imagemagick montage - how to align images to bottom

I want to create a montage, using ImageMagick, where all the images are aligned to the bottom. The images have different heights, and -gravity South doesn't behave as I expect.
Image 1 is 100px high.
Image 2 is 200px high.
If I use :
montage *.png -tile 2x1 -background None -geometry +20+0 -gravity South out/montage.png
I get:
If I add a third image, of 50px high, and run
montage *.png -tile 3x1 -geometry +20+0 -gravity South montage2.jpg
I get:
I understand what's happening here - the canvas is expanding from the bottom. Is there any way to get it to grow from the top, so that the image looks like:
You can just use +smush (documentation) for that:
convert -background white -gravity south [abc].png +smush 10 result.png
If using Imagemagick v7, replace convert with magick.

ImageMagick white out border stripes

I am trying to white out borders of an image. That is, white out 100 px vertical stripe from left, and similarly from right, top, bottom. The following works for left:
mogrify -crop +100+0 -background white -gravity west -splice 100x aaa.tif
But I cannot figure out how to do the same with other sides. I tried many geometries, east, west, this, that, no success. Also please let me know if there is a better alternative than the above command.
Start with a rose:
I'll do the borders with yellow and magenta so you can see what I am doing on StackOverflow's white background.
All Sides
Shave 10px off all sides and then put 10px back on all sides:
convert rose: -shave 10x10 -bordercolor magenta -border 10 result.png
Right Side
convert rose: -gravity east -chop 10x -background yellow -splice 10x result.png
Left Side
convert rose: -gravity west -chop 10x -background yellow -splice 10x result.png
Top
convert rose: -gravity north -chop x10 -background yellow -splice x10 result.png
Bottom
convert rose: -gravity south -chop x10 -background yellow -splice x10 result.png
Left and Right
convert rose: -shave 10x -bordercolor magenta -border 10x result.png
Top and Bottom
convert rose: -shave x10 -bordercolor magenta -border x10 result.png
Tags: ImageMagick, border, bordering, inside, gravity, one side, multiple sides, edges, framing, frame, overpaint, white-out
If you want the equivalent of Photoshop's "Border Outside" just omit the -shave or -chop.
Might be worth exploring -extent option, but I feel it could be quicker just to append padding.
For example...(using blue for visual example)
convert -background blue \
-size 100x xc:blue \
\( rose: -crop +50+0 \) \
-size 100x xc:blue \
+append \
output.png

Cleaning the left side of an image

Using imagemagick, I want to clean the left side of an image, i.e. make white without cropping. For example cleaning the left-most vertical strip of 25 pixels wide. I figured out how to crop to a given geometry, but I couldn't figure out how to clean without cropping.
Here is my start image, made like this:
convert -size 256x256 gradient:cyan-yellow image.png
Method 1
One way to do it would be to use -fx and set all pixels where the x-coordinate is less than 25 to 1.0 (i.e. white) and leave all other pixels as they are:
convert image.png -fx "i<25?1:u" result.png
Method 2
Another, faster way to do it might be to clone the original image, and scale it down to 25 pixels wide, fill it with white and composite that over the original image:
convert image.png \
\( +clone -scale 25x! -fill white -colorize 100 \) \
-composite result.png
The result is the same.
Method 3
A third way to do it might be to crop the image 25 pixels in from the left side, then splice 25 white pixels back on the left side:
convert image.png -crop +25+0 -background white -gravity west -splice 25x result.png
Method 4
Bit of a kludge, but nearer to what you asked. Here, I guess that your image height doesn't exceed 10,000 pixels and draw a rectangle:
convert image.png -fill white -draw "rectangle 0,0 24,9999" result.png
I guess the proper way to do this is to get the height first then use it:
#!/bin/bash
h=$(convert image.png -format "%[fx:h-1]" info:)
convert image.png -fill white -draw "rectangle 0,0 24,$h" result.png

Imagemagick chop 30px off each edge (variable width and height)

I need to chop 30px off each side (top bottom left right). I've tried crop and also -chop 30x30 but that only seems to work on one side;
convert -verbose -density 150 -trim pdfs/test/test.pdf -quality 80 -chop 30x30 images/agents/test/temp/test.jpg
If it's an even amount on both sides of a dimension, then the command you want is -shave
convert -verbose -density 150 -trim pdfs/test/test.pdf -quality 80 -shave 30x30 images/agents/test/temp/test.jpg

Resources