# Imlib2 rotate problem

**URL:** <https://www.ruby-forum.com/t/imlib2-rotate-problem/202913>\
**Category:** Ruby-Gnome 2\
**Created:** [February 8, 2011, 9:52pm UTC](https://www.ruby-forum.com/t/imlib2-rotate-problem/202913 "2011-02-08T21:52:58Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![dubstep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@dubstep](https://www.ruby-forum.com/u/dubstep)\
**Post date:** [February 8, 2011, 9:52pm UTC](https://www.ruby-forum.com/t/imlib2-rotate-problem/202913/1 "2011-02-08T21:52:58Z")

</div>

hello there -  
i’ve been searching for ways to rotate images by ‘arbitrary’ angles.  
i’m basically trying to rotate an image based on a time thread - it  
seems that Gdk::Pixbuf only allows for rotations of 90, 180, or 270  
degrees - am i wrong? man i hope so… that would make things so very  
much easier. i’ve played around with the Cairo context rotate method,  
which accepts any angle, and provides a way to define the center of the  
rotation - but i’ve had a million headaches trying to incorporate  
cairo’s need to be painted when the window is realized into a  
Gtk::Layout or Gnome::Canvas, with no luck.  
assuming that in my hours of searching on google and here on the forum  
i haven’t overlooked some obvious methods that would solve my problems,  
i’ve been left trying various image libraries. i’ve tried RMagick which  
has a handy rotate method, but is too slow to be used in the way i would  
like to.  
i stumbled upon Imlib2, which is quick and well documented and seems  
like it should do everything i need - BUT…

this code:

##############  
require ‘gtk2’  
require ‘imlib2’

win = Gtk::Window.new()  
image = Imlib2::Image.load\_image(“TradyBlix.png”)

image.rotate! 0

image.save(“temp.png”)

gtkimg = Gtk::Image.new(“temp.png”)

win.add(gtkimg)

win.show\_all

Gtk.main

#############

…which ought to rotate my image by, well, 0 degrees -  
rotates it instead to what seems like a random angle. if i run the  
program several times in succession it rotates the image differently  
each time…

any ideas what the heck is happening here? i’ve searched and  
searched, and can’t seem to find any explanation.

any other ideas for ways to rotate an image in gtk would be  
appreciated too. relying on the library to rotate and then save a temp  
file to later load it under gtk seems barbaric, but i can’t figure out a  
better way to do it.

thanks in advance -

jk

---

<div class="post-metadata">

**Author:** ![j\_k](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e9bcb4/32.png) [@j\_k](https://www.ruby-forum.com/u/j_k)\
**Post date:** [February 9, 2011, 2:43pm UTC](https://www.ruby-forum.com/t/imlib2-rotate-problem/202913/2 "2011-02-09T14:43:37Z")

</div>

Have you tried cairo already?

m.

---

<div class="post-metadata">

**Author:** ![j\_k](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e9bcb4/32.png) [@j\_k](https://www.ruby-forum.com/u/j_k)\
**Post date:** [February 9, 2011, 4:16pm UTC](https://www.ruby-forum.com/t/imlib2-rotate-problem/202913/3 "2011-02-09T16:16:34Z")

</div>

[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote in post #980555:

> Have you tried cairo already?
> 
> m.

i have indeed, and it works nicely… i believe the problem i have  
implementing cairo in what i want is that i don’t fully understand how  
it works. i’ve got this:

#########  
require ‘gtk2’

file = “/home/jk/ruby/TradyBlix.png”  
image = Gdk::Pixbuf.new(file, 100, 100)

win = Gtk::Window.new  
win.set\_default\_size(500, 500)

draw\_area = Gtk::DrawingArea.new  
win.add(draw\_area)

x = 10

draw\_area.signal\_connect(“expose\_event”){|widget, event|  
@cc= widget.window.create\_cairo\_context  
@cc.translate(200, 200)  
@cc.rotate(x \* Math::PI / 180)  
@cc.set\_source\_pixbuf(image, 0, 0)  
@cc.paint  
}  
win.show\_all  
Gtk.main  
############

…and i’ve written something longer that ties it into a timer,  
rotating the image 6 degrees per second. this works just fine, and  
seems like the best solution. the problem i’m having is this: imagine  
that you wanted to use one image as the second hand of a stopwatch,  
rotating it on top of another image - this is similar to what i want to  
do. what i have tried is placing the Gtk::DrawingArea defined above  
along with another image into a Gtk::Layout, or a Gnome::Canvas.  
neither of these work. it seems i lose the expose\_event when i place  
the DrawingArea into the Layout… is this true? i’ve tried figuring  
out how to pass an expose signal through the layout to the drawing area,  
but have had no luck.  
do you know of a way to accomplish what i’m trying to do? i would  
prefer to do it with cairo, and not rely on any other image library.

thanks for getting back to me…

j

---

<div class="post-metadata">

**Author:** ![j\_k](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e9bcb4/32.png) [@j\_k](https://www.ruby-forum.com/u/j_k)\
**Post date:** [February 9, 2011, 10:43pm UTC](https://www.ruby-forum.com/t/imlib2-rotate-problem/202913/4 "2011-02-09T22:43:21Z")

</div>

Well, I am not so experienced in GTK widgets internals that I could  
reply to your questions…

But I was able to do something working - it’s a volume peakmeter.  
Usage is quite simple:

1. it takes current volume level from GStreamer’s “volume” element  
every 1/n second, but of course there could be any other source of the  
data,
2. sets @peak and @rms variables to some negative values in the dB,
3. call on the widget’s “update” method.

And it works well in many of my projects.

I don’t remember where I found the inspiration for that, I mean the  
usage of expose signal and queue\_draw(), but it works so I hadn’t  
checked the internals… Like regular script kiddie, uh 😕

Please see the attached file, maybe it will be helpful. Please ask in  
case of any questions,

m.

---

<div class="post-metadata">

**Author:** ![j\_k](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e9bcb4/32.png) [@j\_k](https://www.ruby-forum.com/u/j_k)\
**Post date:** [February 10, 2011, 12:05pm UTC](https://www.ruby-forum.com/t/imlib2-rotate-problem/202913/5 "2011-02-10T12:05:52Z")

</div>

[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote in post #980665:

> …But I was able to do something working - it’s a volume peakmeter.  
> Usage is quite simple…

hi m -  
thanks for getting back - going to have a good look at your script  
when i get some free time… i was thinking of working on a peakmeter  
myself, so i’m very interested to see what you’ve got there. sounds  
like there could be some clues to solving my other problem too - thanks  
again!

- i’ll get back to you after looking at the script…  
ciao -

j

---

<div class="post-metadata">

**Author:** ![j\_k](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e9bcb4/32.png) [@j\_k](https://www.ruby-forum.com/u/j_k)\
**Post date:** [February 13, 2011, 9:47pm UTC](https://www.ruby-forum.com/t/imlib2-rotate-problem/202913/6 "2011-02-13T21:47:27Z")

</div>

Of course, feel free to use it. Just please delete Radion module from  
the module tree - it is reserved for my project’s name 😃

m.

2011/2/13 J. K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

---

<div class="post-metadata">

**Author:** ![j\_k](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e9bcb4/32.png) [@j\_k](https://www.ruby-forum.com/u/j_k)\
**Post date:** [February 13, 2011, 4:02am UTC](https://www.ruby-forum.com/t/imlib2-rotate-problem/202913/7 "2011-02-13T04:02:44Z")

</div>

hi marcin - jk here again…  
thanks for the post - looks like it was just the #.queue\_draw method  
that i was looking for! rewrote something small using cairo that  
rotates the images when a button is pushed - easy enough to tie into a  
signal from the timer i’ve got.  
the peakmeter looks great. if you don’t mind i’ll try to work it into  
the audio player i’m writing (certainly give you credit when and if i  
post the thing…)

thanks again -

j
