[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: 82
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: 12159
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: 82
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: 82
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: 25562
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
coloring
Posts: 82
Joined: 2015-08-27T10:17:36-07:00
Authentication code: 1151

Re: Imagemagick transparent background and white image outlines

Post by coloring »

Thank you very much. You've given me some ideas I'm going to play around with, see how I can further optimize the process. I'll post back with my further questions.

By the way, when I'm using imagemagick, I keep seeing graphics corruptions like this (horizontal lines that are in the wrong places, and horizontal pixel lines cut out in other places). Do you know what's happening? This doesn't happen to me with graphicsmagick, but since gm is older, I don't want to use it
Image
snibgo
Posts: 12159
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: 82
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: 25562
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: 82
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: 25562
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
coloring
Posts: 82
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-28T16:33:36-07:00 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
Same issue. I filed a bug report about the package. I'll have to wait until they fix it. Thanks for your help :) I'll update this topic with my progress about the rest of what I wanted when/if the package gets fixed.

I don't think that any of this is achievable using graphicsmagick (which works for me), so I guess I'm out of luck for now
User avatar
fmw42
Posts: 25562
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: 82
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?
Post Reply