Page 1 of 2

[SOLVED] Imagemagick transparent background and white image outlines

Posted: 2018-02-28T07:51:15-07:00
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

Re: Imagemagick transparent background and white image outlines

Posted: 2018-02-28T08:09:36-07:00
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.

Re: Imagemagick transparent background and white image outlines

Posted: 2018-02-28T08:27:01-07:00
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).

Re: Imagemagick transparent background and white image outlines

Posted: 2018-02-28T08:28:36-07:00
by Bonzo
You should be able to use -background none or transparent

Re: Imagemagick transparent background and white image outlines

Posted: 2018-02-28T08:56:57-07:00
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 :)

Re: Imagemagick transparent background and white image outlines

Posted: 2018-02-28T10:45:41-07:00
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

Re: Imagemagick transparent background and white image outlines

Posted: 2018-02-28T11:30:19-07:00
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

Re: Imagemagick transparent background and white image outlines

Posted: 2018-02-28T11:46:22-07:00
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.

Re: Imagemagick transparent background and white image outlines

Posted: 2018-02-28T11:54:53-07:00
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

Re: Imagemagick transparent background and white image outlines

Posted: 2018-02-28T15:50:37-07:00
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)

Re: Imagemagick transparent background and white image outlines

Posted: 2018-02-28T15:58:48-07:00
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

Re: Imagemagick transparent background and white image outlines

Posted: 2018-02-28T16:33:36-07:00
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

Re: Imagemagick transparent background and white image outlines

Posted: 2018-02-28T16:51:26-07:00
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

Re: Imagemagick transparent background and white image outlines

Posted: 2018-02-28T17:42:40-07:00
by fmw42
I do not see any Bug report on this forum regarding this issue.

Re: Imagemagick transparent background and white image outlines

Posted: 2018-02-28T18:18:50-07:00
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?