Replace color with transparency.. or "remove a color"

Post questions on how to use or achieve an effect in Inkscape.
jgehrcke
Posts: 2
Joined: Sun Sep 12, 2010 8:15 pm

Replace color with transparency.. or "remove a color"

Postby jgehrcke » Sun Sep 12, 2010 8:26 pm

Hey guys!

Great forums. My first question here is -- hopefully -- a very simple one.

I want to remove the white out of my bitmap image, resulting in 100 % transparency where white was before. How to do this?

I found the "Replace color" extension. There one can define "replace color RRGGBB" "by color RRGGBB". Hence, I cannot define any transparency there. Are there other ways to do what I want? Why don't you allow for "by color RRGGBBAA" in the replace dialogue?


Thanks a lot,

Jan-Philip

User avatar
prkos
Posts: 1625
Joined: Tue Nov 06, 2007 8:45 am
Location: Croatia

Re: Replace color with transparency.. or "remove a color"

Postby prkos » Sun Sep 12, 2010 8:30 pm

Replace color doesn't work on bitmaps, you'll have to use another method.

Try clipping - create an object that covers the bitmap only over the areas that aren't white, lower its opacity so you can see through it and clip the image with the object in the end.
just hand over the chocolate and nobody gets hurt

Inkscape Manual on Floss
Inkscape FAQ
very comprehensive Inkscape guide
Inkscape 0.48 Illustrator's Cookbook - 109 recipes to learn and explore Inkscape - with SVG examples to download

jgehrcke
Posts: 2
Joined: Sun Sep 12, 2010 8:15 pm

Re: Replace color with transparency.. or "remove a color"

Postby jgehrcke » Sun Sep 12, 2010 8:55 pm

Okay, thank you :-)

Two related questions on this:

1) Replacing colors in bitmaps seems to be a quite simple problem.. iterate over all pixels and change some of them. I don't know much about the technical structure of Inkscape, but could this be easily implemented as an additional effect/extension? Perhaps this would be a nice task for me or somebody else to contribute to Inkscape.

2) I interprete "Replace color doesn't work on bitmaps" as replace color only works with inkscape/vector/svg objects. Why isn't it allowed to define "replace by color RRGGBBAA" at this point?

User avatar
brynn
Posts: 10309
Joined: Wed Sep 26, 2007 4:34 pm
Location: western USA
Contact:

Re: Replace color with transparency.. or "remove a color"

Postby brynn » Sun Sep 12, 2010 10:56 pm

The reason Inkscape can't select colors, is because of the nature of vector graphics, which does not recognize pixels. Raster graphics programs achieve this by selecting every pixel with white in it. But with vectors, no pixels, so no way to select the white areas.

That said, I believe there is some sort of trick by which this can be done. Unfortunately, I can't remember what it is :( But maybe someone else can tell you? Manipulating the XML code, I think? However, if you want alpha transparency, where there is some solid white, which would be fully transparent, and "shades" of white which would be partiallly transparent, while "not white" would be opaque, you'll do better with a Mask, rather than a Clip. If your bitmap is a photo or something like it, this will be very difficult to do with Inkscape (if not impossible). But if it is a rather simple line drawing, like a cartoon, it should be "doable" ( :P ) especially if it's all solid white and you want only full transparency.

We get this type of question so much, that I'm in the process of writing kind of an overview, to put in an FAQ for this forum. It's not ready to post yet though.

ivan louette
Posts: 215
Joined: Thu Aug 21, 2008 4:08 am
Location: Belgium

Re: Replace color with transparency.. or "remove a color"

Postby ivan louette » Sun Sep 12, 2010 11:29 pm

I think there is room for one or two new filters there because filters also work very well on bitmaps. If you are interested you could try to create them yourself and if they work well of course they could be integrated with the others. Don't hesitate to pm me about that.

And if you want to test the latest filters experiments they are here : http://www.openclipart.org/detail/84433 Only backup the already existing filters.svg file from Inkscape filters folder and replace it with this one.

ivan

User avatar
prkos
Posts: 1625
Joined: Tue Nov 06, 2007 8:45 am
Location: Croatia

Re: Replace color with transparency.. or "remove a color"

Postby prkos » Sun Sep 12, 2010 11:54 pm

You'll have to use GIMP or some other raster editor to edit bitmaps, you can even right-click on an image and open it in a raster editor. Inkscape has limited raster abilities, find them under Extensions > Raster.

When using replace color opacity is preserved, only color is changed, you can consider them separate properties if you like. I guess there'd had to be an additional box where to enter opacity value inside Replace color dialogue. It's this way because the extension author created it that way lol Report a bug if you want something changed or added https://bugs.launchpad.net/inkscape/

Hey Ivan! Got any tips on what primitives to use to create a filter that makes all white areas transparent?
BTW I tried the new filters they are a great addition, and it's nice they are all marked as new makes it easier to explore.
just hand over the chocolate and nobody gets hurt

Inkscape Manual on Floss
Inkscape FAQ
very comprehensive Inkscape guide
Inkscape 0.48 Illustrator's Cookbook - 109 recipes to learn and explore Inkscape - with SVG examples to download

ivan louette
Posts: 215
Joined: Thu Aug 21, 2008 4:08 am
Location: Belgium

Re: Replace color with transparency.. or "remove a color"

Postby ivan louette » Mon Sep 13, 2010 12:10 am

prkos wrote: Hey Ivan! Got any tips on what primitives to use to create a filter that makes all white areas transparent?
BTW I tried the new filters they are a great addition, and it's nice they are all marked as new makes it easier to explore.


This kind of filter already exists : Transparency tool/Light eraser but it's progressive and based on lightness and not color.

However you can easily modify it : open the Filters Editor, then apply the filter, then add a new Effect Color Matrix, then in the fourth line at the bottom of the Color Matrix, change the two last numbers for example from "1" and "0" to "10" and "-1" You will see that transparency is now much more confined to white or very pale areas.

ivan

User avatar
prkos
Posts: 1625
Joined: Tue Nov 06, 2007 8:45 am
Location: Croatia

Re: Replace color with transparency.. or "remove a color"

Postby prkos » Mon Sep 13, 2010 2:21 am

Thanx Ivan!

I tried your tip and it works on the image I tested but not on the yellow area. Increasing the first of the last two numbers to 20 or more fixed it.

But the more I increase it the light areas turn more opaque (as expected right?)

One issue emerges with matting - matted pixels along the dark object edges are also visible when the image is over a dark background. Do you know of any voodoo that can address this?

Thank you for all of the work you do on filters, I feel like I can never thank you enough :)
just hand over the chocolate and nobody gets hurt

Inkscape Manual on Floss
Inkscape FAQ
very comprehensive Inkscape guide
Inkscape 0.48 Illustrator's Cookbook - 109 recipes to learn and explore Inkscape - with SVG examples to download

ivan louette
Posts: 215
Joined: Thu Aug 21, 2008 4:08 am
Location: Belgium

Re: Replace color with transparency.. or "remove a color"

Postby ivan louette » Mon Sep 13, 2010 5:03 am

Thanks to you too for using them ;-)

I am not sure but I think the edges problem will not be addressed before Inkscape rendering use more than 8 bit per color channel. However you could try to overlay your first filter (please do group your object with itself before applying a second filter to keep the filters separated in the Filters Editor) with a second one in ABC sub-menu called Edge cleaner and which does some kind of feathering. Increasing the gaussian blur amount does one part of the work but not all unfortunately.

ivan

P.S. New version of Filters alternate here : http://www.openclipart.org/detail/84595

I added the control by Color Matrix in Light eraser and other Transparency utilities. And I also added a fluorescent option in the Bicolorize filter : go to the bottom of the tree in the Filters Editor and drag and drop the first entry handle of the last Composite to the Color Matrix just above.

ivan

ivan louette
Posts: 215
Joined: Thu Aug 21, 2008 4:08 am
Location: Belgium

Re: Replace color with transparency.. or "remove a color"

Postby ivan louette » Mon Sep 13, 2010 11:00 pm

prkos wrote:Thanx Ivan!

I tried your tip and it works on the image I tested but not on the yellow area. Increasing the first of the last two numbers to 20 or more fixed it.

But the more I increase it the light areas turn more opaque (as expected right?)

One issue emerges with matting - matted pixels along the dark object edges are also visible when the image is over a dark background. Do you know of any voodoo that can address this?

Thank you for all of the work you do on filters, I feel like I can never thank you enough :)


Light eraser and Light eraser negative are improved and the problem with matted pixels along edges is almost solved in this release : http://www.openclipart.org/detail/84655

To isolate more precisely the white areas, increase the fourth value in the fourth line in the last Color matrix.

ivan

User avatar
prkos
Posts: 1625
Joined: Tue Nov 06, 2007 8:45 am
Location: Croatia

Re: Replace color with transparency.. or "remove a color"

Postby prkos » Tue Sep 14, 2010 12:25 am

Do we still need to add the Color Matrix manually?

BTW I now see two Alpha repaint and Light eraser, negative filters in the list.
just hand over the chocolate and nobody gets hurt

Inkscape Manual on Floss
Inkscape FAQ
very comprehensive Inkscape guide
Inkscape 0.48 Illustrator's Cookbook - 109 recipes to learn and explore Inkscape - with SVG examples to download

ivan louette
Posts: 215
Joined: Thu Aug 21, 2008 4:08 am
Location: Belgium

Re: Replace color with transparency.. or "remove a color"

Postby ivan louette » Tue Sep 14, 2010 1:50 am

prkos wrote:Do we still need to add the Color Matrix manually?

BTW I now see two Alpha repaint and Light eraser, negative filters in the list.


No. It's inside now !

~suv
Posts: 2272
Joined: Sun May 10, 2009 2:07 am

Re: Replace color with transparency.. or "remove a color"

Postby ~suv » Sat Sep 18, 2010 1:25 pm

ivan louette wrote:
prkos wrote:Do we still need to add the Color Matrix manually?
No. It's inside now !

Ivan - would you consider adding the default values (identity matrix) for the feColorMatrix component explicitly in 'filters.svg'? Otherwise Inkscape floods the console [1] whenever a 'Light Eraser' filter is active and the current view is updated, with error messages like
** (inkscape-bin:65152): WARNING **: ColorMatrix: values parameter error. Wrong size: 0.

Apparently some functions that process the filter components don't know about default values used for rendering and prefer to complain about missing parameters ;-) The visible result doesn't change though as far as I can tell.

[1] You can check the console messages on Windows with inkscapec.exe (if you use the installer package for 0.48) or just launch inkscape from the command line if you use the 7z package for 0.48 or 0.48+devel (the command line wrapper is now integrated in Inkscape - just that it was forgotten to be added to the installer package 0.48.0-1)

ivan louette
Posts: 215
Joined: Thu Aug 21, 2008 4:08 am
Location: Belgium

Re: Replace color with transparency.. or "remove a color"

Postby ivan louette » Sat Sep 18, 2010 7:10 pm

Thanks. I am afraid this is a wrong rendering message and not a true filters problem. I don't know if it could be changed inside the filters.svg file. Did you already fill a bug on Launchpad ?

ivan

~suv
Posts: 2272
Joined: Sun May 10, 2009 2:07 am

Re: Replace color with transparency.. or "remove a color"

Postby ~suv » Sat Sep 18, 2010 9:28 pm

ivan louette wrote:I am afraid this is a wrong rendering message and not a true filters problem.
I think we mean the same thing - adding the identity matrix would be redundant but make Inkscape "happy", while still conforming to the SVG specification and without changing the rendered result of the preset filter (please test with attached SVG file).

If the warning is what I think it is (complaining about the missing 'values' attribute instead of falling back to the default value (identity)) - Inkscape at some point has to be fixed to know about the default values for filter primitive attributes without complaining about missing parameters - somewhere in a subroutine handling the filter stuff.
ivan louette wrote:I don't know if it could be changed inside the filters.svg file.
In my understanding, the warning can be avoided by changes in 'filters.svg' (not "fixed" though, afaiu).
ivan louette wrote:Did you already fill a bug on Launchpad ?
Not yet. I was interested in how you interpret the warning, first. OTOH several of the current preset filters produce a lot of console messages… warnings mainly. I'm not sure yet how to systematically test and then investigate the cause for each, nor do I know if this is really necessary.
(yes, personally I think console warnings are there for a reason, usually, and should be addressed ;) ).
Attachments
light-eraser-negative-1.svg
[updated]
(54.32 KiB) Downloaded 236 times

ivan louette
Posts: 215
Joined: Thu Aug 21, 2008 4:08 am
Location: Belgium

Re: Replace color with transparency.. or "remove a color"

Postby ivan louette » Sat Sep 18, 2010 10:21 pm

That explains because Filters Editor works somewhat minimalistic ; try to do that while XML Editor is open :

- If you simply add a Color Matrix to your filter it doesn't specify the type but Inkscape interprets it by default as a Matrix

- If you add a Color Matrix and then change for example to HueRotate and then come back to Matrix, then it indicates Matrix as the type but it doesn't give any value

- If you add a Color Matrix, then change the values and come back to default values, then it indicates these default values

Thus what is present in filters.svg reflects my process in creating the filters. Of course I could change that, but then perhaps the best should be to change Filters Editor Behavior ! I was wrong this morning :-)

ivan

~suv
Posts: 2272
Joined: Sun May 10, 2009 2:07 am

Re: Replace color with transparency.. or "remove a color"

Postby ~suv » Sat Sep 18, 2010 10:25 pm

I also just noticed that GIMP (2.6.10) and rsvg-view (both use the same library, librsvg, for rendering SVG) do not display the 'Light eraser' filter effect (dropping the filtered object completely) if the default values for the last feColorMatrix component is missing.

Squiggle (Batik 1.7) OTOH renders both versions of the filter as expected (and the same as in Inkscape), as well as web browsers like Firefox (3.6.9) and Opera (10.61).

-> other SVG renderers (also) fail to handle missing default (fallback) values gracefully.

Screenshots with previously attached SVG file, but with both filter effects enabled (as described in the text).
Batik 1.7:
light-eraser-negative-1-batik17-screenshot2.png
light-eraser-negative-1-batik17-screenshot2.png (137.97 KiB) Viewed 36406 times

GIMP 2.6.10:
light-eraser-negative-1-GIMP-2.6.10-screenshot2.png
light-eraser-negative-1-GIMP-2.6.10-screenshot2.png (104.98 KiB) Viewed 36406 times

rsvg-view 2.26.2:
light-eraser-negative-1-rsvg-screenshot2.png
light-eraser-negative-1-rsvg-screenshot2.png (88.77 KiB) Viewed 36406 times

~suv
Posts: 2272
Joined: Sun May 10, 2009 2:07 am

Re: Replace color with transparency.. or "remove a color"

Postby ~suv » Sat Sep 18, 2010 10:39 pm

ivan louette wrote:Thus what is present in filters.svg reflects my process in creating the filters. Of course I could change that, but then perhaps the best should be to change Filters Editor Behavior
Agreed - Inkscape's Filter Editor needs to be fixed to write filter definitions that Inkscape can interpret and render without console warnings about missing parameters (or Inkscape must be fully aware of the default (fallback) values if attributes/values are omitted).

IIRC there are already some reports filed about the filter editor's incorrect handling of parameters - I'll check the bug tracker again to see if this fits one of them, or if it needs to be reported separately (at the moment, specifically the issue with the values for the 'matrix' attribute of feColorMatrix).

ivan louette
Posts: 215
Joined: Thu Aug 21, 2008 4:08 am
Location: Belgium

Re: Replace color with transparency.. or "remove a color"

Postby ivan louette » Sat Sep 18, 2010 11:21 pm

I think the most important thing is to match svg specifications. And I am not sure Filters Editor is wrong in this specific case (while it is wrong sometimes somewhere else).

Of course being more exigent than svg specs can be useful for some programs internally, but to my sense this is also a door open to worst exclusive (if not proprietary) behaviors.

ivan

~suv
Posts: 2272
Joined: Sun May 10, 2009 2:07 am

Re: Replace color with transparency.. or "remove a color"

Postby ~suv » Sat Sep 18, 2010 11:43 pm

ivan louette wrote:I think the most important thing is to match svg specifications. And I am not sure Filters Editor is wrong in this specific case (while it is wrong sometimes somewhere else).
Is it some part of the rendering engine that emits the warnings about missing parameters? Or - since the visual result looks correct in both examples - just some parsing routine updating/constructing the filter definition (from the SVG source) internally for the renderer?

ivan louette
Posts: 215
Joined: Thu Aug 21, 2008 4:08 am
Location: Belgium

Re: Replace color with transparency.. or "remove a color"

Postby ivan louette » Sun Sep 19, 2010 12:24 am

In fact I don't know anything about that area. I am a visual person and thus if a filter looks like I expected in Inkscape I am glad. Afterwards, before uploading the final version of the filters.svg file on Inkscape server at the program release time it is automatically checked with a script buid by Tavmjong Bah which removes almost completely bad code. I didn't have any bad surprises after that, nothing worked differently in filters. But the version you find on OpenClipart is intermediate and not cleaned up as are the intermediate versions I upload on Inkscape server.

ivan

~suv
Posts: 2272
Joined: Sun May 10, 2009 2:07 am

Re: Replace color with transparency.. or "remove a color"

Postby ~suv » Sun Sep 19, 2010 12:32 am

ivan louette wrote:(…) before uploading the final version of the filters.svg file on Inkscape server at the program release time it is automatically checked with a script buid by Tavmjong Bah which removes almost completely bad code. I didn't have any bad surprises after that, nothing worked differently in filters. But the version you find on OpenClipart is intermediate and not cleaned up as are the intermediate versions I upload on Inkscape server.

Sorry for bothering with the implementation details then - I wasn't aware of that process ;)

ivan louette
Posts: 215
Joined: Thu Aug 21, 2008 4:08 am
Location: Belgium

Re: Replace color with transparency.. or "remove a color"

Postby ivan louette » Sun Sep 19, 2010 12:51 am

No problem ! I like to be aware of every problem concerning filters and you provided very useful information there. I am not sure that Tavmjong Bah script takes account of what you remarked. I don't remember if you are a member of the devlist but you could also ask your question there.

ivan

VSChawathe
Posts: 1
Joined: Thu Oct 25, 2012 2:26 pm

Re: Replace color with transparency.. or "remove a color"

Postby VSChawathe » Thu Oct 25, 2012 2:36 pm

ivan louette wrote:
prkos wrote: Hey Ivan! Got any tips on what primitives to use to create a filter that makes all white areas transparent?
BTW I tried the new filters they are a great addition, and it's nice they are all marked as new makes it easier to explore.


This kind of filter already exists : Transparency tool/Light eraser but it's progressive and based on lightness and not color.

However you can easily modify it : open the Filters Editor, then apply the filter, then add a new Effect Color Matrix, then in the fourth line at the bottom of the Color Matrix, change the two last numbers for example from "1" and "0" to "10" and "-1" You will see that transparency is now much more confined to white or very pale areas.

ivan

Please suggest value for black or very dark area and one other color of your choice by transparency. I'm grateful for guidance on removing white background, however owing to inexperience with vector graphics, I 'm lost & wasting too much time on getting basics right so need help.. Thank you.


Return to “Help with using Inkscape”