Converting Image Lost Background Color

Questions and postings pertaining to the development of ImageMagick, feature enhancements, and ImageMagick internals. ImageMagick source code and algorithms are discussed here. Usage questions which are too arcane for the normal user list should also be posted here.
Post Reply
mporreca
Posts: 4
Joined: 2016-06-13T06:34:37-07:00
Authentication code: 1151

Converting Image Lost Background Color

Post by mporreca »

Hi Everybody,

Is there any command that give me the permission to maintain the background color when I convert an image
I've found a solution for convert only image with background or image without background but I need an univocal command that allow me to convert correctly image without knowing the background color

thank you all!!

p.s: I've tried to convert tiff to png (that usual keep transparency) but doesn't work
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Converting Image Lost Background Color

Post by fmw42 »

What is your IM version and platform? Please post your exact command and the input tiff. What kinds of images are not maintaining the background color -- are they raster or vector formats? The latter is PDF, EPS, etc. The former are jpg, png, tiff, gif, etc. Is it only transparency that does not get maintained or some other colors?
mporreca
Posts: 4
Joined: 2016-06-13T06:34:37-07:00
Authentication code: 1151

Re: Converting Image Lost Background Color

Post by mporreca »

I'm actually using ImageMagick-6.9.1-0-Q16-x64 for Win 10

from command line: convert img.tif img.png

I want to convert a Tiff into a PNG, if the Tiff have an opaque background no problem, but when i convert a Tiff with a transparent background authomatically the PNG appear with a white/black background
the images are bitmap, and yes it only transparency that doesn't get maintained

ps: I want only to know if it possible to have a univocal command that allow me to convert all the image that I have keeping the same background
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Converting Image Lost Background Color

Post by snibgo »

Please show an example of a TIF file that loses transparency when converted to a PNG. You can upload to somewhere like dropbox.com, and paste the URL here.
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Converting Image Lost Background Color

Post by fmw42 »

Tiff can have either background transparency or alpha channel transparency. That may be important to your problem. We need to see an example tiff image that fails, so we can see if there is a universal solution.
mporreca
Posts: 4
Joined: 2016-06-13T06:34:37-07:00
Authentication code: 1151

Re: Converting Image Lost Background Color

Post by mporreca »

snibgo wrote:Please show an example of a TIF file that loses transparency when converted to a PNG. You can upload to somewhere like dropbox.com, and paste the URL here.
Actually I'm testing on an image that I've found in a post here, cause I need to convert a lot of images
here is the link:

https://www.dropbox.com/s/lnravwtfvm6pl68/Hat.tif?dl=0
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Converting Image Lost Background Color

Post by snibgo »

hat.tif contains two images. They both have clipping paths. The second has ordinary transparency, and a canvas offset.

Code: Select all

convert hat.tiff[1] +repage out.png
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Converting Image Lost Background Color

Post by fmw42 »

I believe this image is not properly formed. If I open it in PS, I see one layer with a clipping path. But when I get the verbose information from Imagemagick, it shows a clip path and 2 layers, but the first layer has two sets of color data.

If you convert the image in IM as

Code: Select all

convert -quiet Hat.tif Hat.png
you will get two output png files. The first is the correct size, but has a white background. The second is transparent but bounded by the clip path, so it is smaller.

If you just want the transparent result, then

Code: Select all

convert -quiet Hat.tif[1] Hat.png
I thought to always take the last layer by using

Code: Select all

convert -quiet Hat.tif[-1] Hat.png
but oddly IM objects with "no images defined"

But that does work with a valid image as

Code: Select all

convert rose:[-1] tmp.png
So that is another reason that I think your file is malformed. If not, then IM is misreading the clip path as a second layer and a developer would have to review your image to see what is going on.

Do you have other images that are like this one?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Converting Image Lost Background Color

Post by fmw42 »

This will process the image correctly.

Code: Select all

convert -quiet Hat.tif \
\( -clone 0 -alpha on -channel rgba -evaluate set 0 \) \
\( -clone 1 \) \
-delete 0,1 -background none -flatten hat.png
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Converting Image Lost Background Color

Post by fmw42 »

I am not sure this is a malformed image. I have seen this problem before and what I wrote above was the solution. Tif files with background transparency seem to have a hidden layer for the actual image before placing it in the virtual canvas with the transparent background. I believe this problem was discussed before, but not sure if on or off the forum. You can try searching the forum if you want. I seem to recall discussing this with dlemstra some time ago. He may recall and can comment if he wants.
mporreca
Posts: 4
Joined: 2016-06-13T06:34:37-07:00
Authentication code: 1151

Re: Converting Image Lost Background Color

Post by mporreca »

I've found

mogrify -format png -alpha deactivate -flatten -transparent white *.tif[0]

that works on images that have background color different from #ffffff

thanks all for the support
Post Reply