# Metrics rmagick problem

**URL:** <https://www.ruby-forum.com/t/metrics-rmagick-problem/133443>\
**Category:** Ruby\
**Created:** [March 31, 2008, 6:00pm UTC](https://www.ruby-forum.com/t/metrics-rmagick-problem/133443 "2008-03-31T18:00:38Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![edbond](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/13edae/32.png) [@edbond](https://www.ruby-forum.com/u/edbond)\
**Post date:** [March 31, 2008, 6:00pm UTC](https://www.ruby-forum.com/t/metrics-rmagick-problem/133443/1 "2008-03-31T18:00:38Z")

</div>

I get a strange rmagick result.

I need to add a centered text to image.  
Draw#inspect tell me that all is good, but on result image text isn’t  
centered.

See [Parked at Loopia](http://pastie.caboo.se/173196)  
for code and link to result image.

Thanks for any help in advance.

---

<div class="post-metadata">

**Author:** ![edbond](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/13edae/32.png) [@edbond](https://www.ruby-forum.com/u/edbond)\
**Post date:** [March 31, 2008, 6:29pm UTC](https://www.ruby-forum.com/t/metrics-rmagick-problem/133443/2 "2008-03-31T18:29:44Z")

</div>

Eduard Bondarenko wrote:

> I get a strange rmagick result.
> 
> I need to add a centered text to image.  
> Draw#inspect tell me that all is good, but on result image text isn’t  
> centered.
> 
> See [Parked at Loopia](http://pastie.caboo.se/173196)  
> for code and link to result image.
> 
> Thanks for any help in advance.

You’re working too hard 🙂 Use gravity(Magick::CenterGravity), like the  
example for composite\_layers:  
[http://studio.imagemagick.org/RMagick/doc/ilist.html#composite\_layers](http://studio.imagemagick.org/RMagick/doc/ilist.html#composite_layers).

---

<div class="post-metadata">

**Author:** ![edbond](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/13edae/32.png) [@edbond](https://www.ruby-forum.com/u/edbond)\
**Post date:** [March 31, 2008, 6:55pm UTC](https://www.ruby-forum.com/t/metrics-rmagick-problem/133443/3 "2008-03-31T18:55:13Z")

</div>

Thanks Tim,

But centerGravity don’t works as expects.  
I’ve added  
d.gravity = Magick::CenterGravity

and changed  
d.text(0,0, text)

but the text isn’t in the image center.  
annotate give the same result.

Best regards,  
Eduard

Tim H. wrote:

> Eduard Bondarenko wrote:
> 
> > I get a strange rmagick result.
> > 
> > I need to add a centered text to image.  
> > Draw#inspect tell me that all is good, but on result image text isn’t  
> > centered.
> > 
> > See [Parked at Loopia](http://pastie.caboo.se/173196)  
> > for code and link to result image.
> > 
> > Thanks for any help in advance.
> 
> You’re working too hard 🙂 Use gravity(Magick::CenterGravity), like the  
> example for composite\_layers:  
> [http://studio.imagemagick.org/RMagick/doc/ilist.html#composite\_layers](http://studio.imagemagick.org/RMagick/doc/ilist.html#composite_layers).

---

<div class="post-metadata">

**Author:** ![edbond](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/13edae/32.png) [@edbond](https://www.ruby-forum.com/u/edbond)\
**Post date:** [April 1, 2008, 12:21am UTC](https://www.ruby-forum.com/t/metrics-rmagick-problem/133443/4 "2008-04-01T00:21:49Z")

</div>

Eduard Bondarenko wrote:

> annotate give the same result.

Sorry this is so much trouble.

Try this. For me, this exactly centers the word “RMagick” on the image.  
I’m using ImageMagick 6.4.0 and RMagick 2.3.0, but I would expect this  
to work with any relatively recent version of ImageMagick and RMagick.  
If it doesn’t work for you, please open a support request on the RMagick  
project on RubyForge, let me know what versions of ImageMagick and  
RMagick you’re using, include a simple reproduction of your problem and  
attach the output image you get.

require ‘RMagick’

img = Magick::Image.new(500, 500)

gc = Magick::Draw.new  
gc.stroke “red”  
gc.fill “white”  
gc.circle 249.5, 249.5, 249.5, 255  
gc.line 0, 0, 499, 499  
gc.line 0, 499, 499, 0  
gc.draw img

translucent\_blue = Magick::Pixel.from\_color “blue”  
translucent\_blue.opacity = Magick::QuantumRange \* 0.50  
translucent\_blue = translucent\_blue.to\_color(Magick::AllCompliance,  
true)

gc = Magick::Draw.new  
gc.gravity Magick::CenterGravity  
gc.text\_undercolor translucent\_blue  
gc.pointsize 48  
gc.fill “rgba(100%, 100%, 100%, 0.50)”  
gc.text 0, 0, “RMagick”  
gc.draw img

img.matte = false  
img.display

---

<div class="post-metadata">

**Author:** ![edbond](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/13edae/32.png) [@edbond](https://www.ruby-forum.com/u/edbond)\
**Post date:** [April 1, 2008, 10:35am UTC](https://www.ruby-forum.com/t/metrics-rmagick-problem/133443/5 "2008-04-01T10:35:29Z")

</div>

ImageMagick 6.4.0 03/28/08 Q16  
rmagick (2.3.0)  
Rails 2.0.2

I guess I found a problem.  
When I change Image.read to Image.new  
with the same dimensions text written at the  
desired location.

SOLUTION (possible):  
image.page was not right.  
public/print\_images/bc5085fc45bb5697aec6e181e33ebe78.png PNG 535x535  
768x768+177+79 DirectClass 16-bit 68kb  
width=768, height=768, x=177, y=79  
width=535, height=535, x=0, y=0

I assign image.page to new Rectangle.  
Is there any better solution?

Thanks a lot Tim!

Tim H. wrote:

> Eduard Bondarenko wrote:
> 
> > annotate give the same result.
> 
> Sorry this is so much trouble.
> 
> Try this. For me, this exactly centers the word “RMagick” on the image.  
> I’m using ImageMagick 6.4.0 and RMagick 2.3.0, but I would expect this  
> to work with any relatively recent version of ImageMagick and RMagick.  
> If it doesn’t work for you, please open a support request on the RMagick  
> project on RubyForge, let me know what versions of ImageMagick and  
> RMagick you’re using, include a simple reproduction of your problem and  
> attach the output image you get.
> 
> require ‘RMagick’
> 
> img = Magick::Image.new(500, 500)
> 
> gc = Magick::Draw.new  
> gc.stroke “red”  
> gc.fill “white”  
> gc.circle 249.5, 249.5, 249.5, 255  
> gc.line 0, 0, 499, 499  
> gc.line 0, 499, 499, 0  
> gc.draw img
> 
> translucent\_blue = Magick::Pixel.from\_color “blue”  
> translucent\_blue.opacity = Magick::QuantumRange \* 0.50  
> translucent\_blue = translucent\_blue.to\_color(Magick::AllCompliance,  
> true)
> 
> gc = Magick::Draw.new  
> gc.gravity Magick::CenterGravity  
> gc.text\_undercolor translucent\_blue  
> gc.pointsize 48  
> gc.fill “rgba(100%, 100%, 100%, 0.50)”  
> gc.text 0, 0, “RMagick”  
> gc.draw img
> 
> img.matte = false  
> img.display

---

<div class="post-metadata">

**Author:** ![edbond](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/13edae/32.png) [@edbond](https://www.ruby-forum.com/u/edbond)\
**Post date:** [April 1, 2008, 1:22pm UTC](https://www.ruby-forum.com/t/metrics-rmagick-problem/133443/6 "2008-04-01T13:22:50Z")

</div>

Eduard Bondarenko wrote:

> image.page was not right.  
> public/print\_images/bc5085fc45bb5697aec6e181e33ebe78.png PNG 535x535  
> 768x768+177+79 DirectClass 16-bit 68kb  
> width=768, height=768, x=177, y=79  
> width=535, height=535, x=0, y=0
> 
> I assign image.page to new Rectangle.  
> Is there any better solution?

That’s the best solution.
