ruby,image,imagemagick,png,rmagick , RMagick replacing transparent color

## Question:

Tag: ruby,image,imagemagick,png,rmagick

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?

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.

# Related:

## Heroku RAM not increasing with upgraded dynos

ruby-on-rails,ruby,ruby-on-rails-3,memory,heroku
I have a massive function i have been calling manually through the heroku rails console. I have been receiving the error rapid fire in my logs: 2015-06-22T14:56:42.940517+00:00 heroku[run.9877]: Process running mem=575M(112.4%) 2015-06-22T14:56:42.940517+00:00 heroku[run.9877]: Error R14 (Memory quota exceeded) A 1X dyno is suppose to have 512 MB of RAM. I...

## Map with accumulator on an array

ruby,inject
I'm looking to create a method for Enumerable that does map and inject at the same time. For example, calling it map_with_accumulator, [1,2,3,4].map_with_accumulator(:+) # => [1, 3, 6, 10] or for strings ['a','b','c','d'].map_with_accumulator {|acc,el| acc + '_' + el} # => ['a','a_b','a_b_c','a_b_c_d'] I fail to get a solution working. I...

## Same enum values for multiple columns

ruby-on-rails,ruby,enums
I need to do something like this: class PlanetEdge < ActiveRecord::Base enum :first_planet [ :earth, :mars, :jupiter] enum :second_planet [ :earth, :mars, :jupiter] end Where my table is a table of edges but each vertex is an integer. However, it seems the abvove is not possible in rails. What might...

php,mysql,image
I have a MySQL table with a column "rounds" and each "rounds" has his own photos. Exemple round1 has photos from start=380 end=385. This means it has 6 photos and the name of the photos contains 380,381,382,383,384 or 385 inside. I use this PHP code to display the photos from...

ruby-on-rails,ruby,rest,activerecord,one-to-many
I'm creating a rails application that is a backend for a mobile application. The backend is implemented with a RESTful web API. Currently I am trying to add gamification to the platform through the use of badges that can be earned by the user. Right now the badges are tied...

## Keep leading zeroes when converting string to integer

ruby
For no particular reason, I am trying to add a #reverse method to the Integer class: class Integer def reverse self.to_s.reverse.to_i end end puts 1337.reverse # => 7331 puts 1000.reverse # => 1 This works fine except for numbers ending in a 0, as shown when 1000.reverse returns 1 rather...

ruby