How to prevent alpha channel association during conversion?

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
alexcgt
Posts: 11
Joined: 2018-06-14T05:47:35-07:00
Authentication code: 1152

How to prevent alpha channel association during conversion?

Post by alexcgt » 2018-06-14T06:12:37-07:00

Hello, everyone.

It seems that ImageMagick turns a regular alpha channel into an associated alpha channel if a converted image has only grayscale color values despite being an RGB image.
How to force the conversion process not to change the output color space and most importantly keep an alpha channel unassociated?

I'm looking for a command line and Magick++ solutions but it seems like Magick++ cannot read Photoshop TIFFs so just the command line would be fine.

Thank you!

User avatar
fmw42
Posts: 23248
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to prevent alpha channel association during conversion?

Post by fmw42 » 2018-06-14T09:20:39-07:00

Use -define tiff:alpha=associated|unassociated|unspecified

Specifies the alpha extra samples as associated, unassociated or unspecified when writing to a tiff file

Sorry I do not know how to do that in Magick++
______

Please, always provide your IM version and platform when asking questions, since syntax may differ. Also provide your exact command line and if possible your images.

See the top-most post in this forum "IMPORTANT: Please Read This FIRST Before Posting" at http://www.imagemagick.org/discourse-se ... f=1&t=9620

For novices, see

http://www.imagemagick.org/discourse-se ... f=1&t=9620
http://www.imagemagick.org/script/comma ... essing.php
http://www.imagemagick.org/Usage/reference.html
http://www.imagemagick.org/Usage/
https://github.com/ImageMagick/usage-markdown

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

Re: How to prevent alpha channel association during conversion?

Post by snibgo » 2018-06-14T10:20:10-07:00

I guess with ...

Code: Select all

void Magick::Image::attribute(const std::string name_,const char *value_)
... where name_ is "tiff:alpha" and value_ is "unassociated" or whatever.
snibgo's IM pages: im.snibgo.com

alexcgt
Posts: 11
Joined: 2018-06-14T05:47:35-07:00
Authentication code: 1152

Re: How to prevent alpha channel association during conversion?

Post by alexcgt » 2018-06-15T02:24:49-07:00

Thanks, fmw42, and snigbo.

I actually did find the same solutions before posting but they didn't quite work or so I thought.
I gave it another try and here's what I found:

When converting an RGBA image containing non-grayscale color values everything works as expected (i.e. alpha un/association works).

But when converting an RGBA image containing only grayscale values things start to get weird.

Using associated and unassociated options produce only associated alpha channel.
The channel differs slightly between both options which is probably the result of alpha multiplication or something like that (sorry, not an expert at that).
Using unspecified option inexplicably does produce the unassociated alpha channel which is actually what I'm looking for.

A part of the confusion can be attributed to using XnView as an image viewer.
It's a decent image viewer and it never gave me any trouble but the thing is that after the conversion it shows that the converted images do not have alpha channels which just might be a bug because the Photoshop does recognize the alpha channels.

The problem now as I've mentioned is that I can't seem to retain RGB color space of the original image if the image has only grayscale color values.
Every conversion operation ends up with it being converted to grayscale.
Specifying -colorspace RGB doesn't work. Which is unusual since specifying -colorspace CMYK does work.

Is there any workaround for that?

I'm using ImageMagick-7.0.8-Q8 / Windows 10 Pro

User avatar
fmw42
Posts: 23248
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to prevent alpha channel association during conversion?

Post by fmw42 » 2018-06-15T09:52:54-07:00

ImageMagick will change colorspace to gray for those formats that support it if the image is completely gray. But you can often force RGB for certain output types. Try adding -type truecolor to your command if your output is tif.

alexcgt
Posts: 11
Joined: 2018-06-14T05:47:35-07:00
Authentication code: 1152

Re: How to prevent alpha channel association during conversion?

Post by alexcgt » 2018-06-18T05:30:11-07:00

Thank you very much!

It looks like -type truecolor does work.
True, it discards alpha channel completely and using -type truecolormatte does just the opposite by forcing an alpha channel in images which don't have one initially but it's good solution for now.

I'm still not happy about IM converting images to grayscale without asking.

User avatar
fmw42
Posts: 23248
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to prevent alpha channel association during conversion?

Post by fmw42 » 2018-06-18T10:16:24-07:00

try -type truecoloralpha when using IM 7

alexcgt
Posts: 11
Joined: 2018-06-14T05:47:35-07:00
Authentication code: 1152

Re: How to prevent alpha channel association during conversion?

Post by alexcgt » 2018-06-21T08:03:48-07:00

truecoloralpha does work in a way but it still generates an empty alpha channel for images with no alpha channels and I prefer it wouldn't do that. It's Ok for now though -- I have a non-IM related solution.
(btw., I don't believe that truecoloralpha is mentioned in command line options reference)

User avatar
fmw42
Posts: 23248
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to prevent alpha channel association during conversion?

Post by fmw42 » 2018-06-21T09:14:02-07:00

(btw., I don't believe that truecoloralpha is mentioned in command line options reference)
convert -list type

or

magick -list type

Code: Select all

Bilevel
ColorSeparation
ColorSeparationAlpha
ColorSeparationMatte
Grayscale
GrayscaleAlpha
GrayscaleMatte
Optimize
Palette
PaletteBilevelAlpha
PaletteBilevelMatte
PaletteAlpha
PaletteMatte
TrueColorAlpha
TrueColorMatte
TrueColor
If you do not want the alpha channel created, then just use -type truecolor. If you want an existing alpha channel to be kept then use -type truecoloralpha. You could create a conditional testing on whether the input has an alpha channel so as to decide which form of -type you should use.

The docs at http://www.imagemagick.org/script/comma ... s.php#type do need updating.

alexcgt
Posts: 11
Joined: 2018-06-14T05:47:35-07:00
Authentication code: 1152

Re: How to prevent alpha channel association during conversion?

Post by alexcgt » 2018-06-22T00:19:38-07:00

Thanks, fmw42 -- I'll keep that in mind!

Post Reply