I have some images that have a transparent border of varying width; I'd like to crop these to the visible parts of image. (I imagine a solution won't be specific to the transparent part, but would work with a frame of any color.)
E.g. the 200x200 image below has a transparent border of 3 pixels. I'd like to get the equivalent of
$ gm convert a.png -crop 194x194+3+3 b.png
without having to determine the numbers by hand. Is this possible with graphicsmagick (or imagemagick)?
Related
I have an image that all four margins are transparent alpha channels and I want to crop all margins and only keep the non-alpha channel parts. Is there any way to do it with ImageMagick? Take this image as an example but consider its transparent margins!
You can do that in Imagemagick using -trim to automatically remove the constant transparent regions.
convert img.png -trim +repage result.png
I have a few images I want to convert in batch using ImageMagick.
All images should be converted to a specific width, lets say 500px.
If an image's width is larger than 500px, scale it down and preserve the aspect ratio.
If an image's width is less than 500px, add borders to the sides to make it 500px wide.
The first one is possible using magick mogrify -resize 500x *, but this also scales images with a smaller width up. How can I prevent that from happening and add borders instead?
I figured I could just add -background black -gravity center but I don't know how to prevent upscaling.
Solved it using two separate commands for now:
Resize images larger than 500px wide:
magick mogrify -resize '500x>' *
Add borders to images smaller than 500px wide:
magick mogrify -gravity center -extent '735x<' *
I tried to add a border to an image of dimension 200x200 px using the below code.
convert -border 2x2 -bordercolor "#cccccc" old.png new.png
The above code ads a 2 px border around the old.png. Therefore, the image expands to 204x204 px.
However, I want to add an inline border. I have no issues with border overlaying edge portions of the old image. Thus, the new image should be able to retain the dimensions 200x200 px. Please advise how exactly to do that.
You need to shave two pixels all around and then add a two pixel border. You should try this with Imagemagick 6. If using Imagemagick 7, replace convert with magick.
convert old.png -shave 2x2 -bordercolor "#cccccc" -border 2 new.png
I want to put some white space around an image in equal length. I want to maintain the image pixel ratio to 12:7 too. Any help would do a great help. Thank you.
Another method using Imagemagick V7:
magick ShF4m.jpg -background white -gravity center -extent "%[fx:w+20]"x"%[fx:h+20]" result.jpg
Open image, set the background to white, set the gravity to the centre so the -extent extends the canvas in all directions, increase the canvas size by 20px on the width and 20px on the height.
It would be simple in ImageMagick if you wrote over the inside of the border with white. That way you do not change the image dimensions nor aspect ratio. Here I shave 10 pixels all around and then put a 10 pixel white border all around. Here I will add a red border just to make it visible. But you can change red to white later.
convert barn.jpg -shave 10x10 -bordercolor red -border 10x10 barn_border10.jpg
It would be very hard to add white outside the image of equal amount and keep the aspect ratio. I do not think you can have equal white border amounts and keep the aspect ratio.
I'm working on a photo application and I need some advice how should I solve the following with Graphics/ImageMagick.
Given a photo with resolution: 2048x1536
Given a specified resolution: 1864x1228
Resize the image and fill the specified resolution with the image (now it's 1864x1398)
Highlight the area of the original image will be cropped (to 1864x1228)
I have a working solution which resizes and crops the image properly:
IMOperation resizeOp = new IMOperation();
resizeOp.addImage();
resizeOp.resize(MAX_WIDTH, MAX_HEIGHT, "^");
resizeOp.gravity("center"); //centered image with crop the top/bottom parts
resizeOp.crop(MAX_WIDTH, MAX_HEIGHT, 0, 0);
resizeOp.addImage();
ConvertCmd cmd = new ConvertCmd(true);
cmd.run(resizeOp, fileName, outputFileName); //cropped, center filled image (1864x1228)
The question is how should I do the following: show the full image instead of the cropped version and highlight the area of the image will be cropped. I'd prefer with red border around the cropped image and show with the cropped parts with alpha layer.
I have an idea which I don't like very much: generate an image from the original with alpha layer and put the cropped image on it with red border. It doesn't seem to be the optimal solution :) My other idea is to do this with javafx imageviews, but it seems suboptimal as well.
Notes:
I'm using im4java with GM. I accept a command line solution too (and I'll figure out and post it in im4java ;)
We can restrict the conversation about horizontal images only, I can figure out the vertical operations
Any comments would be highly appreciated.
The oneliner imagemagick convert (remove the line breaks):
convert ( in.jpg -resize 1864x1228^ -fill white -colorize 50% )
( in.jpg -resize 1864x1228^ -gravity center -crop 1864x1228+0+0 )
-gravity center -composite out.jpg
In bash, you have to escape the () characters with \!
I solved it in im4java with sub operations:
IMOperation op = new IMOperation();
op.openOperation();
op.addImage(); //input image
op.resize(MAX_WIDTH, MAX_HEIGHT, "^");
op.fill("white");
op.colorize(50);
op.closeOperation();
op.openOperation();
op.addImage(); //input image
op.resize(MAX_WIDTH, MAX_HEIGHT, "^");
op.gravity(GRAVITY_OPT_CENTER); //centered image with crop the top/bottom parts
op.crop(MAX_WIDTH, MAX_HEIGHT, 0, 0);
op.closeOperation();
op.gravity(GRAVITY_OPT_CENTER);
op.composite();
op.addImage(); // output image
Notes:
It still doesn't contain red border (I can't add border only to the second image).
I decided to use "fade to white" effect, instead of playing with alpha channel.
Example:
input: http://i.stack.imgur.com/lhMgT.jpg
output: http://i.stack.imgur.com/EYO7c.jpg