[SOLVED] Imagemagick transparent background and white image outlines

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
coloring
Posts: 79
Joined: 2015-08-27T10:17:36-07:00
Authentication code: 1151

[SOLVED] Imagemagick transparent background and white image outlines

Post by coloring »

I'm trying to convert some png's to gif, while applying some image editing to them. After searching, I couldn't find any easy methods of doing what I want to do, so I need some help.

The best I can do so far, isn't that elegant. Change white to another color (which matches the default background of an app, but it would be better if it were transparent)

Code: Select all

convert -transparent "#ffffff" -trim -fuzz 3% -background "#ebebeb" -extent 0x0 +matte
I have 3 specific goals that I want to do:
  1. Replace all white pixels in the image with transparency (easy enough to do).

    Code: Select all

    convert -transparent "#ffffff" -fuzz 3%
  2. find the outlines in the image, and add a thicker white outline (to make it look good)
  3. due to the replacement method in 1, there will be inevitable small "holes" inside the image. I would like a fairly decent way to fill these in with white again, in addition to having the outline. Perhaps the step in 2 will be sufficient enough to fill them in? (creating a completely white mask image that has everything inside filled in and overlaying it might do the trick?)
Version: ImageMagick 7.0.7-22
Last edited by coloring on 2018-03-01T15:56:58-07:00, edited 1 time in total.

snibgo
Posts: 13034
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Imagemagick transparent background and white image outlines

Post by snibgo »

For v7, you should use "magick", not "convert".

Your commands have no input or output images.

I'm not sure what you are trying to do. Perhaps you can supply sample inputs.
snibgo's IM pages: im.snibgo.com

coloring
Posts: 79
Joined: 2015-08-27T10:17:36-07:00
Authentication code: 1151

Re: Imagemagick transparent background and white image outlines

Post by coloring »

Forgive my bad example commands. The commands are supposed to be examples. I'll try to find some images to illustrate what I'm trying to do

Edit:
Take this image for example
Image

First, I want to replace all the white with transparency. The reason why is because , although there are some holes that will be formed as a result (the eyes and between the arm , some holes you want to keep transparent, such as closed areas between an arm and object that has white inside, and other areas you want to stay white, such as eyeballs.

Two, I want to create a white outline around outside edges (the person and the letters)

Three, I want to make sure that the inside of the image, the parts that should be white (like the eyeballs), stay white. I figured that the steps to using an outline to create a border around the outside might have the side effect of being able to fill in smaller holes (such as eyeballs).
Last edited by coloring on 2018-02-28T08:56:23-07:00, edited 5 times in total.

Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Imagemagick transparent background and white image outlines

Post by Bonzo »

You should be able to use -background none or transparent

coloring
Posts: 79
Joined: 2015-08-27T10:17:36-07:00
Authentication code: 1151

Re: Imagemagick transparent background and white image outlines

Post by coloring »

Bonzo wrote:
2018-02-28T08:28:36-07:00
You should be able to use -background none or transparent
Check my updated second post, I think it should be clearer about what I'm trying to achieve :)

User avatar
fmw42
Posts: 26383
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Imagemagick transparent background and white image outlines

Post by fmw42 »

I am not sure what you mean by white border. But to make the image transparent and preserve the eyes, you can use connected components to remove small areas from the alpha channel created by -transparent. Here is an outline of the commands which could be combined.

original:
Image


make the image transparent:

Code: Select all

convert image.png -fuzz 10% -transparent white tmp.png
Image

extract the alpha channel:

Code: Select all

convert tmp.png -alpha extract tmp2.png
Image

remove 30 pixel or smaller areas:

Code: Select all

convert tmp2.png \
-define connected-components:mean-color=true \
-define connected-components:area-threshold=30 \
-connected-components 4 \
tmp3.png
Image

Put the new alpha channel back into the original:

Code: Select all

convert image.png tmp3.png -alpha off -compose copy_opacity -composite result.png
Image

snibgo
Posts: 13034
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Imagemagick transparent background and white image outlines

Post by snibgo »

coloring wrote:Do you know what's happening?
Please post a reproducible example: the input, the output with unwanted lines or whatever, and the command that made it.
snibgo's IM pages: im.snibgo.com

coloring
Posts: 79
Joined: 2015-08-27T10:17:36-07:00
Authentication code: 1151

Re: Imagemagick transparent background and white image outlines

Post by coloring »

snibgo wrote:
2018-02-28T11:46:22-07:00
coloring wrote:Do you know what's happening?
Please post a reproducible example: the input, the output with unwanted lines or whatever, and the command that made it.
Source image:
Image

Commands:

Code: Select all

$ magick convert image.png -transparent white tmp.png
$ magick convert tmp.png -alpha extract tmp2.png
$ magick convert tmp2.png \
> -define connected-components:mean-color=true \
> -define connected-components:area-threshold=30 \
> -connected-components 4 \
> tmp3.png
tmp3.png
Image

I'm using my Android device for this. The images sometimes come out clean, other times they don't. No matter what commands or images I use, it happens. It seems to be a deeper underlying problem. I think probably it will not be reproducible for you.. :/


----
Regarding the outline I was talking about earlier, I was reading the docs, and it seems I can achieve this expanded outline by using the dilate morphology method, but seems I'm running into these same issues again..

Code: Select all

magick convert tmp3.png -morphology Dilate Octagon outp.png
Image

User avatar
fmw42
Posts: 26383
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Imagemagick transparent background and white image outlines

Post by fmw42 »

I do not get that corruption for your new image using IM 7.0.7.23 Q16 Mac OSX. What is your platform/OS? What version of libpng are you using?

Code: Select all

convert -list format
PNG* PNG rw- Portable Network Graphics (libpng 1.6.34)

coloring
Posts: 79
Joined: 2015-08-27T10:17:36-07:00
Authentication code: 1151

Re: Imagemagick transparent background and white image outlines

Post by coloring »

fmw42 wrote:
2018-02-28T15:50:37-07:00
I do not get that corruption for your new image using IM 7.0.7.23 Q16 Mac OSX. What is your platform/OS? What version of libpng are you using?

Code: Select all

convert -list format
PNG* PNG rw- Portable Network Graphics (libpng 1.6.34)
PNG* rw- Portable Network Graphics (libpng 1.6.34)
I'm using an Android ARM aarch64 build. Whatever it is, it definitely must be specific to my device or build :/ I think I should file a bug about the package. Thanks for your help, I'll get back to this if/when I fix this issue

User avatar
fmw42
Posts: 26383
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Imagemagick transparent background and white image outlines

Post by fmw42 »

Note in IM 7 the correct command is magick, not magick convert. Try magick alone and see if that helps.

Code: Select all

magics image.png -transparent white tmp.png
magics tmp.png -alpha extract tmp2.png
magick tmp2.png \
-define connected-components:mean-color=true \
-define connected-components:area-threshold=30 \
-connected-components 4 \
tmp3.png

User avatar
fmw42
Posts: 26383
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Imagemagick transparent background and white image outlines

Post by fmw42 »

I do not see any Bug report on this forum regarding this issue.

coloring
Posts: 79
Joined: 2015-08-27T10:17:36-07:00
Authentication code: 1151

Re: Imagemagick transparent background and white image outlines

Post by coloring »

fmw42 wrote:
2018-02-28T17:42:40-07:00
I do not see any Bug report on this forum regarding this issue.
I believe the problem is with the Linux package in the repository, not your program, so I filed the bug with the package maintainer. Others have filed similar bug reports to this one there as well.
https://github.com/termux/termux-packages/issues/2214

This is a imagemagick build for Android, using ndk and termux. My build is specifically for aarch64.
Do you feel that this warrants I file a bug report here as well?

User avatar
fmw42
Posts: 26383
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Imagemagick transparent background and white image outlines

Post by fmw42 »

Since it did not fail for me, that would be one indication that the problem is not likely with ImageMagick or libpng as I had questioned. However, one can never tell. But my recommendation is to see what the package manager say first. If they cannot verify the issue, then you might report the issue here. But the IM developers would have to reproduce the issue and I do not know if they have access to your same package and hardware.

coloring
Posts: 79
Joined: 2015-08-27T10:17:36-07:00
Authentication code: 1151

Re: Imagemagick transparent background and white image outlines

Post by coloring »

So far this is what I have.

1st, I remove all white on the outside border. (I decided not to mess with the white inside the image)
2, get the alpha mask
3, enlarge the border (to create room for extra working space), dilate the edges (so I can create a white outline in the final result), and trim off the extra
4, composite the final

Do my commands look good? And I also want to antialias the edges of tmp3, I haven't figured out how to smooth them yet

Code: Select all

# remove all outside borders
magick image.png -bordercolor white -border 1x1 \
-fill transparent -floodfill +0+0 white \
-shave 1x1 tmp.png

# get mask image
magick tmp.png -alpha extract tmp2.png

# expand border, create white edges along outside, re-trim image
magick tmp2.png -bordercolor black -border 20x20 \
-morphology Dilate Octagon:4 \
-trim tmp3.png

magick image.png tmp3.png -alpha off -compose copy_opacity -composite result.png

Locked