how can I add a transparency gradient in a grayscale to an image?

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?".
nars
Posts: 8
Joined: 2015-07-17T04:27:32-07:00
Authentication code: 1151

how can I add a transparency gradient in a grayscale to an image?

Post by nars »

Hi,

how can I add a transparency gradient in a grayscale to an image which already have background transparent?

For example: white color -> 70% transparent
gray -> 40% transparent
etc -> etc

Image

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

Re: how can I add a transparency gradient in a grayscale to an image?

Post by snibgo »

I don't understand what you are trying to do.

If you want to add a transparency gradient to an image, make the gradient in grayscale, then "-compose CopyOpacity -composite".

If the image already has transparency, it will be overwritten.

If you want to modify the transparency of an image, you can "-alpha extract" to copy it to a grayscale image, modify it, then CopyOpacity it back to the image.
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: how can I add a transparency gradient in a grayscale to an image?

Post by fmw42 »

Extract the alpha channel. Then make a gradient with range as desired and -compose multiply the two together. Then replace the old alpha channel with the new one.
246246
Posts: 190
Joined: 2015-07-06T07:38:22-07:00
Authentication code: 1151

Re: how can I add a transparency gradient in a grayscale to an image?

Post by 246246 »

Just a guess. You wanted to apply the given image as a gradient to itself, and want to get 8bit alpha image.

I'm not an expert, but I will try
Given image (name 2mgnsdh.png) is 176x202 and has 1 bit transparent color #F6F6F6. So

Code: Select all

> convert 2mgnsdh.png -negate tmp.png
> convert -size 176x202 'xc:#F6F6F6' png:- | convert - tmp.png -alpha off -compose CopyOpacity -composite -negate PNG32:target.png
Odd edge remains... but, If this is something closer to what you want, someone will show you more elegant solution. (I also wanted to know a way to avoid double negate and to use temporary image. :D )
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: how can I add a transparency gradient in a grayscale to an image?

Post by fmw42 »

For example: white color -> 70% transparent
gray -> 40% transparent
etc -> etc
Please clarify. You are not explaining this very well.

By "gray" do you mean 50% gray?

For a linear gradient you need to specify where white goes (70%) AND where black goes (????) and the direction.

If I assume you want a linear gradient, then
white --> 70%
gray(50%) --> 40%
black --> would then be 10% if linear.

But you still need to supply the direction.

But I think you may mean that you want the transparency to come from the image itself. In that case, try

(unix syntax)

Code: Select all

convert 2mgnsdh.png \
\( -clone 0 -alpha off +level 10%x70% \) \
\( -clone 0 -alpha extract \) \
\( -clone 1 -clone 2 -compose multiply -composite \) \
-delete 1,2 \
-alpha off -compose copy_opacity -composite result.png
246246
Posts: 190
Joined: 2015-07-06T07:38:22-07:00
Authentication code: 1151

Re: how can I add a transparency gradient in a grayscale to an image?

Post by 246246 »

Thank you, Fred. My first trial was to create 100%x0% one of your script step by step. Hope your script is the answer of the original question.

Today I learned a lot from you using -clone. I really appreciate some experts like you are here.
Last edited by 246246 on 2015-07-17T17:29:02-07:00, edited 1 time in total.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: how can I add a transparency gradient in a grayscale to an image?

Post by fmw42 »

246246 wrote:Thank you, Fred. My first trial was to create 100%x0% one of your script step by step. Hope your script is the answer of the original question.

Today I learned a lot from you using -clone. I really appreciate some experts like you are here.
See
http://www.imagemagick.org/Usage/basics/#parenthesis
http://www.imagemagick.org/Usage/basics/#list_ops
246246
Posts: 190
Joined: 2015-07-06T07:38:22-07:00
Authentication code: 1151

Re: how can I add a transparency gradient in a grayscale to an image?

Post by 246246 »

I tried your script with CMYK tiff image simply created with

convert -colorspace CMYK 2mgnsdh.png 2mgnsdh-CMYK.tiff

(To compare I also created 2mgnsdh-RGB.tiff with "convert 2mgnsdh.png 2mgnsdh-RGB.tiff". )

I soon noticed all CMY values of this tiff is 0 so I add -channel K for +level:

Code: Select all

convert 2mgnsdh-CMYK.tiff \
\( -clone 0 -channel K -alpha off +level 100%x0% \) \
\( -clone 0 -alpha extract \) \
\( -clone 1 -clone 2  -compose multiply -composite \) \
-delete 1,2 \
-alpha off -compose copy_opacity -composite result.tiff
Comparing with the case of RGB, 1) the result has something like edge and 2) background transparency lost.

Could you tell me what I am missing?

As for transparency, original image

Alpha: cmyka(0,0,0,9,0) #0000000900
Histogram:
19785: ( 0, 0, 0, 9, 0) #0000000900 cmyka(0,0,0,9,0)
463: ( 0, 0, 0,210,255) #000000D2 cmyka(0,0,0,210,1)
...
Transparent color: cmyka(0,0,0,0,0)

turned out to

Histogram:
12627: ( 0, 0, 0, 0,255) #00000000 cmyka(0,0,0,0,1)
985: ( 0, 0, 0, 7,255) #00000007 cmyka(0,0,0,7,1)
983: ( 0, 0, 0, 5,255) #00000005 cmyka(0,0,0,5,1)
908: ( 0, 0, 0, 9,255) #00000009 cmyka(0,0,0,9,1)
...
Transparent color: cmyka(0,0,0,0,0)

so, I understand what happened, but don't know how to fix it.

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

Re: how can I add a transparency gradient in a grayscale to an image?

Post by fmw42 »

Convert your CMYK TIFF to sRGB using profiles, then process as above, then convert back to CMYK. You may have to turn alpha off before the colorspace change and then turn it back on afterwards? Does your CMYK image have an existing CMYK profile or not. The profile processing is different if it does or does not have a CMYK profile (such as USWebCoatedSWOP). If that does not work, then post your CMYK TIFF so I can test with it. Also always provide your version of IM and platform.

I do not understand why you are turning K off and not Alpha?
convert -colorspace CMYK 2mgnsdh.png 2mgnsdh-CMYK.tiff
This is not a good way to convert to CMYK. You should use profiles. Also the -colorspace CMYK, if you do it that way, needs to be after the input image.

What is the point of trying to do this in CMYK. Most processing assumes sRGB, though I think it should work anyway, but would need to test.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: how can I add a transparency gradient in a grayscale to an image?

Post by fmw42 »

This seems to me to be a bug. IM seems to have -alpha extract in CMYK negated. However, I am not that used to processing in CMYKA, so perhaps there is another reason (like CMYK without profiles typically displays as a negated version of RGB and needs to have a negate somewhere. Perhaps someone who knows CMYK better than I can explain or confirm if it is a bug.

But by negating the extracted alpha channel, the following works fine for me on IM 6.9.1.9 Q16 Mac OSX.

Code: Select all

convert 2mgnsdh.png -colorspace CMYK 2mgnsdh.tif

convert 2mgnsdh.tif \
\( -clone 0 -alpha off +level 10%x70% \) \
\( -clone 0 -alpha extract -negate \) \
\( -clone 1 -clone 2 -compose multiply -composite \) \
-delete 1,2 \
-alpha off -compose copy_opacity -composite result.tif
This also works.

Code: Select all

convert 2mgnsdh.tif \
\( -clone 0 -alpha off -channel K +level 10%x70% +channel \) \
\( -clone 0 -alpha extract \) \
\( -clone 1 -clone 2 -compose multiply -composite -negate \) \
-delete 1,2 \
-alpha off -compose copy_opacity -composite result.tif
246246
Posts: 190
Joined: 2015-07-06T07:38:22-07:00
Authentication code: 1151

Re: how can I add a transparency gradient in a grayscale to an image?

Post by 246246 »

fmw42 wrote:IM seems to have -alpha extract in CMYK negated. However, I am not that used to processing in CMYKA, so perhaps there is another reason (like CMYK without profiles typically displays as a negated version of RGB and needs to have a negate somewhere.
Thank you very much! This is what i missed and confused me.
nars
Posts: 8
Joined: 2015-07-17T04:27:32-07:00
Authentication code: 1151

Re: how can I add a transparency gradient in a grayscale to an image?

Post by nars »

Thank you ;)

After your answers, my solution:

1 - convert in.png to RGBA

2 - I did a loop and i change the alpha values.
For example:
for i, j in zip(range(255+1), list(reversed(range(255+1)))):
(i,i,i,j)
(255,255,255,0)
(254,254,254,1)
(253,253,253,2)
...

Thank you ;)
246246
Posts: 190
Joined: 2015-07-06T07:38:22-07:00
Authentication code: 1151

Re: how can I add a transparency gradient in a grayscale to an image?

Post by 246246 »

fmw42 wrote: But by negating the extracted alpha channel, the following works fine for me on IM 6.9.1.9 Q16 Mac OSX.

Code: Select all

convert 2mgnsdh.png -colorspace CMYK 2mgnsdh.tif

convert 2mgnsdh.tif \
\( -clone 0 -alpha off +level 10%x70% \) \
\( -clone 0 -alpha extract -negate \) \
\( -clone 1 -clone 2 -compose multiply -composite \) \
-delete 1,2 \
-alpha off -compose copy_opacity -composite result.tif
At a first glance, it seems to worked but all alpha values are set to the same value, except for the original background color. (IM 6.9.1.9 Q16 Windows but I call it from Cygwin bash, so syntax is same.)

...
Alpha: cmyka(0,0,0,9,0) #0000000900
Colors: 116
Histogram:
19785: ( 0, 0, 0, 9, 0) #0000000900 cmyka(0,0,0,9,0)
463: ( 0, 0, 0,210,230) #000000D2E6 cmyka(0,0,0,210,0.901961)
458: ( 0, 0, 0,216,230) #000000D8E6 cmyka(0,0,0,216,0.901961)
405: ( 0, 0, 0,213,230) #000000D5E6 cmyka(0,0,0,213,0.901961)
374: ( 0, 0, 0, 51,230) #00000033E6 cmyka(0,0,0,51,0.901961)
351: ( 0, 0, 0,219,230) #000000DBE6 cmyka(0,0,0,219,0.901961)
329: ( 0, 0, 0,207,230) #000000CFE6 cmyka(0,0,0,207,0.901961)
...

Well, asking CMYK is just my curiosity, as I had thought the original script should work without modifications so I think it is useful to make it a filter with its percentages as parameters.

I begin to confusing again, so when I really need it, I would think use another approach :D , but if I feel something strange in CMYK, I would ask in another thread.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: how can I add a transparency gradient in a grayscale to an image?

Post by fmw42 »

I still get a transparent CMYK tiff as the result of my commands above. Perhaps you are missing the one transparent color in the histogram of the result.

Image: result.tif
Format: TIFF (Tagged Image File Format)
Mime type: image/tiff
Class: DirectClass
Geometry: 176x202+0+0
Resolution: 72x72
Print size: 2.44444x2.80556
Units: Undefined
Type: ColorSeparationAlpha
Base type: ColorSeparation
Endianess: LSB
Colorspace: CMYK
Depth: 8-bit
Channel depth:
cyan: 1-bit
magenta: 1-bit
yellow: 1-bit
black: 8-bit
alpha: 8-bit
... etc
Total ink density: 93.7255%
Alpha: cmyka(0,0,0,9,0) #0000000900
Colors: 116
Histogram:
19785: ( 0, 0, 0, 9, 0) #0000000900 cmyka(0,0,0,9,0))
463: ( 0, 0, 0,210,229) #000000D2E5 cmyka(0,0,0,210,0.898039)
458: ( 0, 0, 0,216,229) #000000D8E5 cmyka(0,0,0,216,0.898039)
405: ( 0, 0, 0,213,229) #000000D5E5 cmyka(0,0,0,213,0.898039)
374: ( 0, 0, 0, 51,229) #00000033E5 cmyka(0,0,0,51,0.898039)
351: ( 0, 0, 0,219,229) #000000DBE5 cmyka(0,0,0,219,0.898039)
329: ( 0, 0, 0,207,229) #000000CFE5 cmyka(0,0,0,207,0.898039)
310: ( 0, 0, 0,205,229) #000000CDE5 cmyka(0,0,0,205,0.898039)
301: ( 0, 0, 0,197,229) #000000C5E5 cmyka(0,0,0,197,0.898039)
270: ( 0, 0, 0,200,229) #000000C8E5 cmyka(0,0,0,200,0.898039)
... etc
246246
Posts: 190
Joined: 2015-07-06T07:38:22-07:00
Authentication code: 1151

Re: how can I add a transparency gradient in a grayscale to an image?

Post by 246246 »

fmw42 wrote:I still get a transparent CMYK tiff as the result of my commands above. Perhaps you are missing the one transparent color in the histogram of the result.
Yes, I see one transparent color. But when compare with RGB case with your original script,

Alpha: graya(255,0) #FFFFFF00
Colors: 116
Histogram:
1: ( 17, 17, 17,239) #111111EF graya(17,0.937255)
16: ( 25, 25, 25,232) #191919E8 graya(25,0.909804)
85: ( 31, 31, 31,227) #1F1F1FE3 graya(31,0.890196)
181: ( 37, 37, 37,223) #252525DF graya(37,0.87451)
351: ( 42, 42, 42,219) #2A2A2ADB graya(42,0.858824)
458: ( 46, 46, 46,216) #2E2E2ED8 graya(46,0.847059)
405: ( 50, 50, 50,213) #323232D5 graya(50,0.835294)
...
19785: (255,255,255, 0) #FFFFFF00 graya(255,0)
...

it contains various alpha which is a desired result of the original question.
So I expected the same result even if the input is CMYK.
Post Reply