Removing white from image with white background where gradients are present

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?".
Post Reply
davyjcrow
Posts: 3
Joined: 2016-08-27T09:07:45-07:00
Authentication code: 1151

Removing white from image with white background where gradients are present

Post by davyjcrow »

Hi Everyone

I'm working with images that were created as monochrome TIFFs (colour separations) where I need to convert them to PNGs with a transparent background, removing all the white.

The problem is, the images have gradients in them. So for example I need to be able to replace not just solid white with transparent (which I managed to do already) but also take into account percentages; so for example what was 40% black before would become 100% black but 40% opaque, if that makes sense.

So an example of the source image:
https://www.dropbox.com/s/zy5by7duftpj0 ... t.tif?dl=0

Desired result:
https://www.dropbox.com/s/55hf82swzg3mj ... 1.png?dl=0

Looking for recommendations as to how this could be achieved with ImageMagick (I am actually using Magick.NET but guess this is more a question of the correct technique to achieve).

Thanks in advance.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Removing white from image with white background where gradients are present

Post by snibgo »

Your gradient.tif has two images, both CMYK with a profile. I'll take the first image, and convert it to sRGB.

The magickal incantation is "-alpha copy", which copies the grayscale into the alpha channel. Because we want what was white to become transparent, we need to negate.

Code: Select all

convert gradient.tif[0] -profile sRGB.icc +strip -negate -alpha copy -fill black -colorize 100 g.png
The result is entirely black, but with varying transparency. Does that do what you want? Sorry, I don't know Magick.NET.
snibgo's IM pages: im.snibgo.com
davyjcrow
Posts: 3
Joined: 2016-08-27T09:07:45-07:00
Authentication code: 1151

Re: Removing white from image with white background where gradients are present

Post by davyjcrow »

Hi there, thanks for the reply!

When I run the command though I get an error message:

convert: unable to open image `sRGB.icc': No such file or directory @ error/blob.c/OpenBlob/2675.

I figured this was due to the icc profile argument needing a full path to be specified. There did not appear to be any such profile in the ImageMagick files though so I downloaded one from the ICC website. However pointing to that a new error is thrown:

convert: Incompatible type for "RichTIFFIPTC"; tag ignored. `TIFFFetchNormalTag' @ warning/tiff.c/TIFFWarnings/852.

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

Re: Removing white from image with white background where gradients are present

Post by snibgo »

There may be a copy of sRGB.icc in the directory you installed ImageMagick in. Sorry, I should have said.

Code: Select all

convert: Incompatible type for "RichTIFFIPTC"; tag ignored. `TIFFFetchNormalTag' @ warning/tiff.c/TIFFWarnings/852.
That's just a warning. Don't worry about it. Is the result what you want?
snibgo's IM pages: im.snibgo.com
davyjcrow
Posts: 3
Joined: 2016-08-27T09:07:45-07:00
Authentication code: 1151

Re: Removing white from image with white background where gradients are present

Post by davyjcrow »

Ah sorry, I thought it had failed - you are right it had converted the image fine! Yes that was exactly what was needed, thanks a lot - magick indeed!
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Removing white from image with white background where gradients are present

Post by fmw42 »

Those warnings come from TIFF tags that are custom and for which IM knows nothing about them.

In your commands, you can avoid getting them by adding -quiet right after convert (or magick in IM 7).
Post Reply