# ImageMagick

**URL:** <https://www.ruby-forum.com/t/imagemagick/169278>\
**Category:** wxRuby\
**Created:** [June 1, 2009, 8:07pm UTC](https://www.ruby-forum.com/t/imagemagick/169278 "2009-06-01T20:07:04Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![pitosalas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a4c791/32.png) [@pitosalas](https://www.ruby-forum.com/u/pitosalas)\
**Post date:** [June 1, 2009, 8:07pm UTC](https://www.ruby-forum.com/t/imagemagick/169278/1 "2009-06-01T20:07:04Z")

</div>

I’d like to combine Wx (Wx::Image) with Rmagick/ImageMagick processing.  
Other than saving to disk constantly, how would I pass an  
Rmagick/ImageMagick image (e.g. RMagick:Image) to and from a Wx Image  
(e.g. Wx:Image)?

---

<div class="post-metadata">

**Author:** ![pitosalas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a4c791/32.png) [@pitosalas](https://www.ruby-forum.com/u/pitosalas)\
**Post date:** [June 1, 2009, 9:19pm UTC](https://www.ruby-forum.com/t/imagemagick/169278/2 "2009-06-01T21:19:12Z")

</div>

Pito S. wrote:

> I’d like to combine Wx (Wx::Image) with Rmagick/ImageMagick processing.  
> Other than saving to disk constantly, how would I pass an  
> Rmagick/ImageMagick image (e.g. RMagick:Image) to and from a Wx Image  
> (e.g. Wx:Image)?

If the image data from RMagick can be put into a string, you can use  
StringIO + Wx::Image.read and Wx::Image#write to exchange image data in  
a recognised format (PNG, TIF etc) in memory. See also  
Wx::Image#set\_data for low-level access to raw image data (see the  
images/maths\_drawing.rb for an example of using this).

a

---

<div class="post-metadata">

**Author:** ![pitosalas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a4c791/32.png) [@pitosalas](https://www.ruby-forum.com/u/pitosalas)\
**Post date:** [June 2, 2009, 9:27pm UTC](https://www.ruby-forum.com/t/imagemagick/169278/3 "2009-06-02T21:27:10Z")

</div>

Hi,

On Mon, Jun 1, 2009 at 9:17 PM, Alex F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> recognised format (PNG, TIF etc) in memory. See also Wx::Image#set\_data for  
> low-level access to raw image data (see the images/maths\_drawing.rb for an  
> example of using this).

I’m just about to add an RMagick sample to wxRuby.

The following code snippet allows direct exchange between RMagick and  
wxRuby :

magick\_image = Magick::ImageList.new(img\_file)

# some magick conversion

wx\_image = Wx::Image.new(magick\_image.columns, magick\_image.rows)  
wx\_image.set\_data(magick\_image.to\_blob { self.format = “RGB” }

The only problem is transparency.  
If img\_file is a PNG file with transparent areas, those transparent  
areas are lost (i.e. displaying a Wx::Bitmap corresponding to the  
Wx::Image shows black areas instead of transparent areas).

At the moment, the only way I found to keep the transparency is to  
save the magick\_image into a PNG file and read it into a Wx::Image,  
but this is not what you’re looking for.

Cheers.

Chauk-Mean.

---

<div class="post-metadata">

**Author:** ![pitosalas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a4c791/32.png) [@pitosalas](https://www.ruby-forum.com/u/pitosalas)\
**Post date:** [June 3, 2009, 9:39am UTC](https://www.ruby-forum.com/t/imagemagick/169278/4 "2009-06-03T09:39:35Z")

</div>

I dunno, in RMagick, is the format “RGBA” available? If it is, that  
should  
allow for transparencies to be retained. Just a thought, don’t have  
RMagick  
to test.

---

<div class="post-metadata">

**Author:** ![pitosalas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a4c791/32.png) [@pitosalas](https://www.ruby-forum.com/u/pitosalas)\
**Post date:** [June 3, 2009, 1:10pm UTC](https://www.ruby-forum.com/t/imagemagick/169278/5 "2009-06-03T13:10:34Z")

</div>

Chauk-Mean P. wrote:

> The only problem is transparency.  
> If img\_file is a PNG file with transparent areas, those transparent  
> areas are lost (i.e. displaying a Wx::Bitmap corresponding to the  
> Wx::Image shows black areas instead of transparent areas).
> 
> At the moment, the only way I found to keep the transparency is to  
> save the magick\_image into a PNG file and read it into a Wx::Image,  
> but this is not what you’re looking for.

Does Wx::Image#set\_alpha help here? it works like set\_data but for the  
alpha channel.

a

---

<div class="post-metadata">

**Author:** ![pitosalas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a4c791/32.png) [@pitosalas](https://www.ruby-forum.com/u/pitosalas)\
**Post date:** [June 4, 2009, 12:42am UTC](https://www.ruby-forum.com/t/imagemagick/169278/6 "2009-06-04T00:42:39Z")

</div>

On Wed, Jun 3, 2009 at 1:07 PM, Alex F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Does Wx::Image#set\_alpha help here? it works like set\_data but for the alpha  
> channel.

Following Mario’s suggestion, I tried “RGBA” format which is available  
in RMagick.  
I extracted the image and the alpha raw data.

But when I tried to use set\_alpha with the alpha raw data, an  
exception is raised :

alpha\_raw\_string = apha\_raw\_data.pack(“C\*”)  
image.set\_alpha(alpha\_raw\_string)

It seems that there is a SWIG problem.

Cheers.

Chauk-Mean.

---

<div class="post-metadata">

**Author:** ![pitosalas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a4c791/32.png) [@pitosalas](https://www.ruby-forum.com/u/pitosalas)\
**Post date:** [June 4, 2009, 1:47pm UTC](https://www.ruby-forum.com/t/imagemagick/169278/7 "2009-06-04T13:47:27Z")

</div>

On Thu, Jun 4, 2009 at 2:54 AM, Mario S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > Following Mario’s suggestion, I tried “RGBA” format which is available  
> > in RMagick.  
> > I extracted the image and the alpha raw data.
> 
> I just want to clarify, that you separated the RGB and the A values from  
> each pixel byte section (As I call it) in the blob that is returned from  
> RMagick, and tried applying it via image.set\_data, and image.set\_alpha,

Yes. Here is the detailed code I’m using :

wx\_image = Wx::Image.new(magick\_image.columns, magick\_image.rows)

rgba\_data = magick\_image.to\_blob {  
self.format = “RGBA”  
}

rgba\_array = rgba\_data.unpack(“C\*”)

# separate the RGB image and the alpha data

i = 0  
image\_array, alpha\_array = rgba\_array.partition do |val|  
i += 1  
if i % 4 != 0  
true  
else  
false  
end  
end

image\_data = image\_array.pack(“C\*”)  
alpha\_data = alpha\_array.pack(“C\*”)

wx\_image.data = image\_data  
wx\_image.set\_alpha(alpha\_data)

The set\_alpha method raises the following exception :

rmagic\_bitmap\_image.rb:109:in `set\_alpha’: Wrong arguments for  
overloaded method ‘wxImage.SetAlpha’. (ArgumentError)  
Possible C/C++ prototypes are:  
void wxImage.SetAlpha(unsigned char \*alpha, bool static\_data)  
void wxImage.SetAlpha(int x, int y, unsigned char alpha)

Chauk-Mean.

---

<div class="post-metadata">

**Author:** ![pitosalas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a4c791/32.png) [@pitosalas](https://www.ruby-forum.com/u/pitosalas)\
**Post date:** [June 4, 2009, 9:39pm UTC](https://www.ruby-forum.com/t/imagemagick/169278/8 "2009-06-04T21:39:09Z")

</div>

On Thu, Jun 4, 2009 at 6:45 AM, Chauk-Mean P.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:

> wx\_image.data = image\_data  
> wx\_image.set\_alpha(alpha\_data)

Alright, this may be a mistake in the Documentation, but try doing:  
wx\_image.set\_alpha(alpha\_data, false)

Something tells me that SWIG isn’t taking into consideration the default  
param for static\_data.

---

<div class="post-metadata">

**Author:** ![pitosalas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a4c791/32.png) [@pitosalas](https://www.ruby-forum.com/u/pitosalas)\
**Post date:** [June 4, 2009, 3:16am UTC](https://www.ruby-forum.com/t/imagemagick/169278/9 "2009-06-04T03:16:00Z")

</div>

On Wed, Jun 3, 2009 at 5:39 PM, Chauk-Mean P.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:

> > > but this is not what you’re looking for.
> > 
> > Does Wx::Image#set\_alpha help here? it works like set\_data but for the  
> > alpha  
> > channel.
> 
> Following Mario’s suggestion, I tried “RGBA” format which is available  
> in RMagick.  
> I extracted the image and the alpha raw data.

I just want to clarify, that you separated the RGB and the A values from  
each pixel byte section (As I call it) in the blob that is returned from  
RMagick, and tried applying it via image.set\_data, and image.set\_alpha,  
or  
did you just tried to do the entire image data blob set through  
image.set\_alpha?

I just want to make sure, there’s no confusion about the order that is  
going  
on, with this.

---

<div class="post-metadata">

**Author:** ![pitosalas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a4c791/32.png) [@pitosalas](https://www.ruby-forum.com/u/pitosalas)\
**Post date:** [June 5, 2009, 12:03am UTC](https://www.ruby-forum.com/t/imagemagick/169278/10 "2009-06-05T00:03:43Z")

</div>

Hi Mario,

On Thu, Jun 4, 2009 at 9:37 PM, Mario S.[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Alright, this may be a mistake in the Documentation, but try doing:  
> wx\_image.set\_alpha(alpha\_data, false)

I’ve already tried that with the true value 🙂 and it didn’t work.

> Something tells me that SWIG isn’t taking into consideration the default  
> param for static\_data.

Yes, there is a SWIG problem here. I don’t know what but this can be  
revealed simply with the following code (there is no need to have  
RMagick) :

require ‘wx’

wx\_image = Wx::Image.new(2,2)

image\_data = [0,0,0,64,64,64,128,128,128,255,255,255].pack(“C\*”)  
alpha\_data = [0,255,0,255].pack(“C\*”)

unique\_alpha\_data = [255].pack(“C”)

wx\_image.set\_data(image\_data)

#Following call works :  
wx\_image.set\_alpha()

#Following calls do not work :  
#wx\_image.set\_alpha(alpha\_data)  
#wx\_image.set\_alpha(alpha\_data, false)  
#wx\_image.set\_alpha(0,0,unique\_alpha\_data)

I’m not sufficiently comfortable with SWIG to fix such kind of issue.

Cheers.

Chauk-Mean.

---

<div class="post-metadata">

**Author:** ![pitosalas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a4c791/32.png) [@pitosalas](https://www.ruby-forum.com/u/pitosalas)\
**Post date:** [June 13, 2009, 2:00pm UTC](https://www.ruby-forum.com/t/imagemagick/169278/11 "2009-06-13T14:00:04Z")

</div>

Hi all,

On Thu, Jun 4, 2009 at 11:57 PM, Chauk-Mean P.[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> #wx\_image.set\_alpha(alpha\_data, false)  
> #wx\_image.set\_alpha(0,0,unique\_alpha\_data)
> 
> I’m not sufficiently comfortable with SWIG to fix such kind of issue.

I’ve finally been able to fix this SWIG issue which seems to be caused  
by :

- the use of a typemap for the first form of the SetAlpha C++ method
- and by the fact that this SetAlpha C++ method has an overloaded method

I’ve just hidden the overloaded method in the SWIG header for the  
Image class and now everything works.  
Maybe Alex will be able to provide a complete fix to this issue  
without having to hide the overloaded method.

I’ve also found a way to get directly the alpha raw data from a RMagick  
image.  
So here is the code to convert seamlessly a RMagick image to a wxRuby  
image :

wx\_img = Wx::Image.new(magick\_img.columns, magick\_img.rows)

# Set the image data

wx\_img.data = magick\_img.to\_blob { self.format = “RGB” }

# Set the alpha (transparency) if any

if magick\_img.alpha?  
wx\_img.alpha = magick\_img.to\_blob { self.format = “A” }  
end

I’ve just committed the fix and a complete sample  
(samples/drawing/rmagic\_bitmap\_image.rb) in subversion.

Cheers.

Chauk-Mean.
