Wx::Image.resize

Starting with the wxRuby sample: bitmap_image.rb
I changed one line to try to crop the image, using the resize method.
That produced the following error message:

gary@Z790:~/Ruby/wxExamples/drawing$ ruby bitmap_image-1.rb
bitmap_image-1.rb:28:in resize': Wrong type for wxSize parameter (TypeError) from bitmap_image-1.rb:28:ininitialize’
from bitmap_image-1.rb:90:in new' from bitmap_image-1.rb:90:inon_init’
from
/var/lib/gems/1.8/gems/wxruby-2.0.1-x86-linux/lib/wx/classes/app.rb:16:in
main_loop' from /var/lib/gems/1.8/gems/wxruby-2.0.1-x86-linux/lib/wx/classes/app.rb:16:inrun’
from bitmap_image-1.rb:89
bitmap_image-1.rb:28: [BUG] Segmentation fault
ruby 1.8.7 (2008-08-11 patchlevel 72) [i486-linux]

Aborted

The only change that was made to the sample file is shown below,
marked with #–>

The computer is running Ubuntu 9.04 (fully up to date),
ruby 1.8.7 (2008-08-11 patchlevel 72) [i486-linux]
wxruby-2.0.1-x86-linux

My understanding of the error is that the numbers in the resize argument
are of the wrong type. Yet changing resize to scale works, using the
same arguments.

Please explain what I need to do to get this to work, in order to crop
an image.

Full program code follows:

wxRuby2 Sample Code. Copyright © 2004-2008 wxRuby development team

Freely reusable code: see SAMPLES-LICENSE.TXT for details

begin
require ‘rubygems’
rescue LoadError
end
require ‘wx’

Bitmap sample (rewritten by Chauk-Mean P.)

This sample demonstrates how to draw the same image in various forms

(original, mirrored, greyscaled and blurred).

This sample uses :

- Wx::Image, which allows a wide range of manipulations such as

rescaling

and writing to files.

- Wx::Bitmap, which is a platform-specific representation of an image.

This is the class that must be used to actually display an image.

class ImageFrame < Wx::Frame
def initialize
super(nil, :title => ‘Simple image demo’, :size => [600, 600])

# Create the various images from the bitmap file
img_file = File.join( File.dirname(__FILE__), 'ruby-logo.jpg')
@image = Wx::Image.new(img_file)

#–> The only change made to the sample file is on this line, unchanged
below:

@mirrored_image = Wx::Image.new(img_file).mirror

#–> The following line produces the error:
@mirrored_image = Wx::Image.new(img_file).resize(100, 100)

#–> When resize above is change to scale below, no error.

@mirrored_image = Wx::Image.new(img_file).scale(100, 100)

@greyscaled_image = Wx::Image.new(img_file).convert_to_greyscale
@blurred_image = Wx::Image.new(img_file).blur(15)

# Create the corresponding bitmaps
compute_bitmaps

# Set up event handling
evt_size :on_size
evt_idle :on_idle
evt_paint :on_paint

end

Create a bitmap for the specified image and size

def compute_bitmap image, width, height
rescaled_image = Wx::Image.new(image).rescale(width, height)
rescaled_image.to_bitmap
end

Create the bitmaps corresponding to the images and with half the

size of the frame
def compute_bitmaps
width = client_size.x / 2
height = client_size.y / 2
@bitmap1 = compute_bitmap(@image, width, height)
@bitmap2 = compute_bitmap(@mirrored_image, width, height)
@bitmap3 = compute_bitmap(@greyscaled_image, width, height)
@bitmap4 = compute_bitmap(@blurred_image, width, height)
@done = true
end

Note to recompute the bitmaps on a resize

def on_size(event)
@done = false
event.skip
end

Recompute the bitmaps if needed, then do a refresh

def on_idle
if not @done
compute_bitmaps
refresh
end
end

Paint the frame with the bitmaps

def on_paint
paint do | dc |

  if @done
    width = client_size.x / 2
    height = client_size.y / 2
    dc.draw_bitmap(@bitmap1, 0, 0, false)
    dc.draw_bitmap(@bitmap2, width, 0, false)
    dc.draw_bitmap(@bitmap3, 0, height, false)
    dc.draw_bitmap(@bitmap4, width, height, false)
  end

end

end
end

Wx::App.run do
ImageFrame.new.show
end

Hi Gary,

2009/11/21 Gary H. [email protected]:

Starting with the wxRuby sample: bitmap_image.rb
I changed one line to try to crop the image, using the resize method.
That produced the following error message:

gary@Z790:~/Ruby/wxExamples/drawing$ ruby bitmap_image-1.rb
bitmap_image-1.rb:28:in `resize’: Wrong type for wxSize parameter
(TypeError)

#–> The following line produces the error:
@mirrored_image = Wx::Image.new(img_file).resize(100, 100)

#–> When resize above is change to scale below, no error.

@mirrored_image = Wx::Image.new(img_file).scale(100, 100)

The scale method takes two mandatory integer parameters : a width and a
height.
Conversely, the resize method takes two mandatory parameters of
different type : a Size and a Point.
See http://wxruby.rubyforge.org/doc/image.html.

If you want to crop from the position x=10, y=20 and with the size
w=50, h=60, you should write :
@mirrored_image = Wx::Image.new(img_file).resize([50, 60], [10, 20])

See http://wxruby.rubyforge.org/doc/wxruby_intro.html for Size and
Point shorthands.

Cheers.
Chauk-Mean.

Chauk-Mean P. wrote:

Hi Gary,

2009/11/21 Gary H. [email protected]:

Starting with the wxRuby sample: bitmap_image.rb
I changed one line to try to crop the image, using the resize method.
That produced the following error message:

gary@Z790:~/Ruby/wxExamples/drawing$ ruby bitmap_image-1.rb
bitmap_image-1.rb:28:in `resize’: Wrong type for wxSize parameter
(TypeError)

#–> The following line produces the error:
� �@mirrored_image = Wx::Image.new(img_file).resize(100, 100)

#–> When resize above is change to scale below, no error.

� @mirrored_image = Wx::Image.new(img_file).scale(100, 100)

The scale method takes two mandatory integer parameters : a width and a
height.
Conversely, the resize method takes two mandatory parameters of
different type : a Size and a Point.
See http://wxruby.rubyforge.org/doc/image.html.

If you want to crop from the position x=10, y=20 and with the size
w=50, h=60, you should write :
@mirrored_image = Wx::Image.new(img_file).resize([50, 60], [10, 20])

See http://wxruby.rubyforge.org/doc/wxruby_intro.html for Size and
Point shorthands.

Cheers.
Chauk-Mean.


Thanks for your help Chauk-Mean,
however, when I use your line of code:
@mirrored_image = Wx::Image.new(img_file).resize([50, 60], [10, 20])
I get the following error msg:

gary@Z790:~/Ruby/wxExamples/drawing$ ruby bitmap_image-1.rb
bitmap_image-1.rb:29:in resize': Expected argument 2 of type wxPoint const, but got Array [10, 20] (TypeError) in SWIG method 'Resize' from bitmap_image-1.rb:29:in initialize’
from bitmap_image-1.rb:91:in new' from bitmap_image-1.rb:91:in on_init’
from
/var/lib/gems/1.8/gems/wxruby-2.0.1-x86-linux/lib/wx/classes/app.rb:16:in
main_loop' from /var/lib/gems/1.8/gems/wxruby-2.0.1-x86-linux/lib/wx/classes/app.rb:16:in run’
from bitmap_image-1.rb:90
bitmap_image-1.rb:29: [BUG] Segmentation fault
ruby 1.8.7 (2008-08-11 patchlevel 72) [i486-linux]
Aborted
gary@Z790:~/Ruby/wxExamples/drawing$

So I changed that line as follows:
@mirrored_image = Wx::Image.new(img_file).resize( :size => [50, 60],
:pos => [10, 20] )
and now I get this error msg:

gary@Z790:~/Ruby/wxExamples/drawing$ ruby bitmap_image-1.rb
bitmap_image-1.rb:28:in resize': wrong # of arguments(1 for 2) (ArgumentError) from bitmap_image-1.rb:28:in initialize’
from bitmap_image-1.rb:91:in new' from bitmap_image-1.rb:91:in on_init’
from
/var/lib/gems/1.8/gems/wxruby-2.0.1-x86-linux/lib/wx/classes/app.rb:16:in
main_loop' from /var/lib/gems/1.8/gems/wxruby-2.0.1-x86-linux/lib/wx/classes/app.rb:16:in run’
from bitmap_image-1.rb:90
bitmap_image-1.rb:28: [BUG] Segmentation fault
ruby 1.8.7 (2008-08-11 patchlevel 72) [i486-linux]
Aborted
gary@Z790:~/Ruby/wxExamples/drawing$

What is wrong with each of these alternatives?

On Sun, Nov 22, 2009 at 10:08 PM, Gary H. [email protected]
wrote:

(TypeError)
The scale method takes two mandatory integer parameters : a width and a
Point shorthands.


from
:pos => [10, 20] )
`main_loop’

Posted via http://www.ruby-forum.com/.


wxruby-users mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/wxruby-users

This is a problem with the Ruby wrappers themselves. The only method to
accept named parameters are the Constructors, EG: Window.new, Frame.new,
Button.new, etc, etc. So your second alternative to use named
parameters,
was not recognized by the resize method.

The first one, should be correct, however the Ruby wrappers are not
converting the array into a Point / Size, like it is suppose to, the
alternative to this, is to do the following:

@mirroed_image = Wx::Image.new(img_file).resize(Wx::Point.new(10,20),
Wx::Size.new(50,60))

This should currently work, and I will add this as a bug for us to fix
for
wxRuby 2.0.2 release.

hth,

Mario

Mario S. wrote:

On Sun, Nov 22, 2009 at 10:08 PM, Gary H. [email protected]
wrote:

(TypeError)
The scale method takes two mandatory integer parameters : a width and a
Point shorthands.


from
:pos => [10, 20] )
`main_loop’

Posted via http://www.ruby-forum.com/.


wxruby-users mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/wxruby-users

This is a problem with the Ruby wrappers themselves. The only method to
accept named parameters are the Constructors, EG: Window.new, Frame.new,
Button.new, etc, etc. So your second alternative to use named
parameters,
was not recognized by the resize method.

The first one, should be correct, however the Ruby wrappers are not
converting the array into a Point / Size, like it is suppose to, the
alternative to this, is to do the following:

@mirroed_image = Wx::Image.new(img_file).resize(Wx::Point.new(10,20),
Wx::Size.new(50,60))

This should currently work, and I will add this as a bug for us to fix
for
wxRuby 2.0.2 release.

hth,

Mario

Thanks for your help Mario!

While your code line had point & size reversed, the generated error
message allowed me to recognize and correct the code. The following line
worked for me:
@mirrored_image =
Wx::Image.new(img_file).resize(Wx::Size.new(4000,3000),
Wx::Point.new(200,150))

Regards,
Gary