How to remove alpha channeled margins from an image using ImageMagick - imagemagick

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

Related

Imagemagick replace all colors in image with white

I have a bunch of PNG images (logos) that I want to convert to an all-white color pallet; basically I want to replace all non-transparent pixels with white pixels. Is there an easy way to do this in imagemagick?
You can do that in ImageMagick using -colorize.
convert input.png -fill white -colorize 100 output.png
It will preserve the transparent pixels and make everything else white.

How to remove specific white space from image using ImageMagick

Currently, I have thousands of pictures where I want to get rid of white space around the object, but not inside the object. However, the white space is not constant. See pictures below.
Picture 1 :
Picture 2 :
Picture 3 :
Options like this don't work as they delete all white space in the images
convert image.png -fuzz 10% -transparent white output.png
Any suggestions?
What do you mean by getting rid of the white space? If you mean make it transparent, then in ImageMagick 6, you can do a whole folder of images using the mogrify command as follows:
The process involves padding the image with 1 pixel border of white to ensure white is all around. Then doing a fuzzy flood fill. Then remove the extra one pixel all around. I have assumed that the background color is nearly white. Change the fuzz value as desired.
Create a new directory to hold the output (or backup your input directory)
Change directory to the one holding your input images
mogrify -path path_to/outdirectory -format png -bordercolor white -border 1 -fuzz 20% -fill none -draw "matte 0,0 floodfill" -shave 1x1 *.png
Here are the resulting images.
Of course you will have to download them to see that they are transparent.
Note that the images that I downloaded were JPG. So if your images are jpg and not png as in your command, then change *.png to *.jpg. Or just use * to change all formats.
See
https://imagemagick.org/Usage/basics/#mogrify
https://imagemagick.org/Usage/draw/#matte
If using ImageMagick 7, change matte to alpha and change mogrify to magick mogrify.

Extract watermark from image

I've got 2 versions of 1 image
Is it possible to get a transparent png file representing this watermark? Can ImageMagick do this?
SilverMonkey has the basic solution using Imagemagick. But the request was for a transparent PNG. So I will add a little bit more to his code to make it transparent by adding -alpha copy.
convert kitty2.jpg kitty1.jpg -compose minus -composite -auto-level -alpha copy watermark1.png
Here is another approach that makes a binary mask for the watermark by thresholding. But it leaves a lot of noise. So I use some morphology open to remove the noise and then some morpholgy close to try to fill in where the text is broken up. Then I add -alpha copy to make the image transparent. But the text is white and the original watermark was light gray. So I turn alpha off, multiply by 0.75 to reduce the brightness of the white letters to gray without affecting the alpha channel. Then turn the alpha channel back on.
convert kitty2.jpg kitty1.jpg -compose minus -composite -threshold 0.6% -morphology open diamond:1 -morphology close octagon:1 -alpha copy -alpha off -evaluate multiply 0.75 -alpha on watermark2.png
For more on morphology, see https://imagemagick.org/Usage/morphology/
You can achieve your goal by calculating the difference between both images (subtract the pixels of both images and calculate the absolute value). This will result in:
ImageMagick seems to be capable of image subtraction, look here:
The code:
convert image2 image1 -compose minus -composite result

Imagemagick inline border without resizing the original image

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

Blur image with ImageMagick and extent it

I want to blur an image with transparent background using ImageMagick. I've trimmed this image to its content, so it doesn't have any surrounding whitespace/transparency.
The above part works fine, but:
When I blur the trimmed image, it will (obviously) overflow the boundaries. So the question is: How can I blur a trimmed image, while extending its boundaries to the right size before?
Extend the background with transparent pixels, blur and then re-trim:
convert original.png -background none -gravity center -extent 200x200% -blur ... -trim result.png

Resources