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 rake db:migrate failing - uninitialized constant

ruby-on-rails,ruby,heroku
My app is working fine locally and my push to Heroku was successful. But, when I run heroku run rake db:migrate, I get the following error: NameError: uninitialized constant AddWeightToExercises Here is the failed migration: class AddWeightToExercise < ActiveRecord::Migration def change add_column :exercises, :weight, :float end end edit: Thanks for...

## How to handle backslash “\” escape characters in q string and heredocument

ruby
Ruby Newbie here. I do not understand why Ruby looks inside %q and escapes the \. I am using Ruby to generate Latex code. I need to generate \\\hline which is used in Latex for table making. I found \\\hline as input generated \hline even though the string was inside...

## 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...

## Get X days out of an Array

ruby,ruby-on-rails-4
I have an array filled with Datetime objects: [Mon, 22 Jun 2015, Tue, 23 Jun 2015, Wed, 24 Jun 2015, Thu, 25 Jun 2015, Fri, 26 Jun 2015, Sat, 27 Jun 2015, Sun, 28 Jun 2015] I know how to select what I want from the array ex: week.select{|x|x.monday? ||...

## Ruby gsub group parameters do not work when preceded by escaped slashes

ruby,regex
I am trying to perform a trivial substitution, that in any other language I have come across, work as per the documentation. However, my substitution fails for some reason. The documentation examples list: "hello".gsub(/[aeiou]/, '*') #=> "h*ll*" "hello".gsub(/([aeiou])/, '<\1>') #=> "h<e>ll<o>" "hello".gsub(/./) {|s| s.ord.to_s + ' '} #=> "104 101...

## 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...

## How to get return value from a forked / spawned process in Ruby?

ruby,process,output,fork,spawn
My simple test program: pid = Process.spawn("sleep 10;date") How can I place the output (eg stdout) of the "date" command in a variable when it is available? I don't want to use a file for the data exchange....

## Ruby- get a xml node value

ruby,xml
can someone help me in extracting the node value for the element "Name". Type 1: I am able to extract the "name" value for the below xml by using the below code <Element> <Details> <ID>20367</ID> <Name>Ram</Name> <Name>Sam</Name> </Details> </Element> doc = Nokogiri::XML(response.body) values = doc.xpath('//Name').map{ |node| node.text}.join ',' puts values...

## Make instance variable accessible through hash in Ruby

ruby-on-rails,ruby,ruby-on-rails-4,activerecord
In Rails, ActiveRecord objects, attributes are accessible via method as well as through Hash. Example: user = User.first # Assuming User to be inheriting from ActiveRecord::Base user.name # Accessing attribute 'name' via method user[:name] # Attribute 'name' is accessible via hash as well How to make instance variables accessible through...