Reputation: 6958
I have a PNG image (PNG 96x96 96x96+0+0 8-bit RGB 2.05KB 0.000u 0:00.000). It's an icon, with a fully transparent background, and a white symbol (its borders are a gradient from transparent to full white). I want to reduce the whole icon opacity to 30% of its initial value. With convert
, I can do it that way:
convert input.png -channel a -evaluate Multiply 0.3 +channel output.png
identify
gives the same output for the resulting image. However, I want to do this from a Ruby script, so I'm using RMagick.
Here's a little test:
source = Magick::Image::read(INPUT)[0]
source.write(OUTPUT)
This works: I have the exact same image. Still the same output with identify
. Now, let's change the opacity:
source = Magick::Image::read(INPUT)[0]
source.opacity = (Magick::QuantumRange * 0.3).floor
source.write(OUTPUT)
The result is wrong: a black background is added before the opacity is reduced. I end up with a 30% opacity black background, and a 30% opacity white icon (which is, for the icon, what I want). Here's the identify
output: PNG 96x96 96x96+0+0 8-bit RGB 1.06KB 0.000u 0:00.000
I tried to add PNG32:
in front of my output file name. If the identify
output changes (PNG 96x96 96x96+0+0 8-bit sRGB 1.29KB 0.000u 0:00.000), the visual result remains the same.
I tried to specify the background color:
source = Magick::Image::read(INPUT)[0]
source.opacity = (Magick::QuantumRange * 0.3).floor
source.write(OUTPUT) do
self.background_color = 'none'
end
But I end up with the exact same result.
Any idea of how I can avoid this black background?
Upvotes: 1
Views: 1539
Reputation: 207345
This looks like the command that most resembles the one you have had success with at the command-line:
img.quantum_operator(MultiplyQuantumOperator, 0.3, AlphaChannel)
For future reference, I found it here.
Upvotes: 2