Adjust opacity leaving fully transparent sections unchanged

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
pjfingers
Posts: 2
Joined: 2016-06-07T12:30:46-07:00
Authentication code: 1151

Adjust opacity leaving fully transparent sections unchanged

Post by pjfingers »

Hello!

I have what seems to be a super simple task, yet I have spent hours looking through this forum as well as the IM docs trying to find a solution. Having tried 100s of variations I am still stumped. :shock:

I am starting with a PNG image (it's a map tile) that has areas full of color and a transparent background everywhere else:

https://www.dropbox.com/s/4ssdbehbbl0mm ... c.png?dl=0

I want to adjust the transparency of the colorful area, making it more transparent. When I run the command I have assembled I succeed in setting the transparency of the colorful section, however it also sets the previously totally transparent sections to the new level as well making those areas less transparent and filling in a gray background.

Code: Select all

convert pre-opac.png -channel A -evaluate set 39% post-opac.png
yeilds:

https://www.dropbox.com/s/01crbvjyvza9c ... c.png?dl=0

Looking at my code, it makes sense. As I read it, I'm setting the Alpha channel to 39% everywhere. So how can I dial down the opacity across the board so that the 0 areas stay at 0 and everything else comes down 39%?

I'm running
Version: ImageMagick 6.7.2-7 2015-02-27 Q16 http://www.imagemagick.org
OS: RHEL 6 x86_64
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Adjust opacity leaving fully transparent sections unchanged

Post by snibgo »

Perhaps multiplying by 0.39 does what you want.
snibgo's IM pages: im.snibgo.com
audas
Posts: 10
Joined: 2016-01-27T02:27:24-07:00
Authentication code: 1151

Re: Adjust opacity leaving fully transparent sections unchanged

Post by audas »

Wow - I wish someone would answer this question as it is a real problem.

I have spent all day on this very issue. Heartbreaking stuff.

Even assigning a specific colour and then trying to remove it does not work. If someone has ANY idea on what to do here it would be a great help - this is something that REALLY should be addressed in the docs. There is literally no way of reducing the opacity of a colour bounded by transparency without raising the transparency of the (transparency) masked area.

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

Re: Adjust opacity leaving fully transparent sections unchanged

Post by snibgo »

Didn't I answer the question? Multiply the alpha by whatever factor you want.

Code: Select all

convert in.png -channel A -evaluate Multiply 0.39 +channel out.png
Where the alpha was zero (transparent), it remains zero. Where it was one (opaque), it becomes 0.39 (mostly transparent). Etc.
snibgo's IM pages: im.snibgo.com
pjfingers
Posts: 2
Joined: 2016-06-07T12:30:46-07:00
Authentication code: 1151

Re: Adjust opacity leaving fully transparent sections unchanged

Post by pjfingers »

Yep, multiply by desired opacity works great! Thanks for posting that one! 8)

I also figured out a second solution as follows:

Code: Select all

convert in.png -channel A +level 39%,100% out.png
Both work. The multiply opacity solution seems a little more obvious for anyone visiting the code in the future, so I went with that one in the end.
Post Reply