# Wx::Image.resize

**URL:** <https://www.ruby-forum.com/t/wx-image-resize/179310>\
**Category:** wxRuby\
**Created:** [November 21, 2009, 7:42pm UTC](https://www.ruby-forum.com/t/wx-image-resize/179310 "2009-11-21T19:42:44Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![vanik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/ac8455/32.png) [@vanik](https://www.ruby-forum.com/u/vanik)\
**Post date:** [November 21, 2009, 7:42pm UTC](https://www.ruby-forum.com/t/wx-image-resize/179310/1 "2009-11-21T19:42:44Z")

</div>

## 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:in`initialize’  
from bitmap\_image-1.rb:90:in `new' from bitmap_image-1.rb:90: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: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

---

<div class="post-metadata">

**Author:** ![vanik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/ac8455/32.png) [@vanik](https://www.ruby-forum.com/u/vanik)\
**Post date:** [November 22, 2009, 10:23pm UTC](https://www.ruby-forum.com/t/wx-image-resize/179310/2 "2009-11-22T22:23:43Z")

</div>

Hi Gary,

2009/11/21 Gary H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> ## 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](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](http://wxruby.rubyforge.org/doc/wxruby_intro.html) for Size and  
Point shorthands.

Cheers.  
Chauk-Mean.

---

<div class="post-metadata">

**Author:** ![vanik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/ac8455/32.png) [@vanik](https://www.ruby-forum.com/u/vanik)\
**Post date:** [November 23, 2009, 4:08am UTC](https://www.ruby-forum.com/t/wx-image-resize/179310/3 "2009-11-23T04:08:18Z")

</div>

Chauk-Mean P. wrote:

> Hi Gary,
> 
> 2009/11/21 Gary H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):
> 
> > ## 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](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](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?

---

<div class="post-metadata">

**Author:** ![vanik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/ac8455/32.png) [@vanik](https://www.ruby-forum.com/u/vanik)\
**Post date:** [November 23, 2009, 12:29pm UTC](https://www.ruby-forum.com/t/wx-image-resize/179310/4 "2009-11-23T12:29:58Z")

</div>

On Sun, Nov 22, 2009 at 10:08 PM, Gary H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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/](http://www.ruby-forum.com/).
> 
> * * *
> 
> wxruby-users mailing list  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> [http://rubyforge.org/mailman/listinfo/wxruby-users](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

---

<div class="post-metadata">

**Author:** ![vanik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/ac8455/32.png) [@vanik](https://www.ruby-forum.com/u/vanik)\
**Post date:** [November 23, 2009, 7:27pm UTC](https://www.ruby-forum.com/t/wx-image-resize/179310/5 "2009-11-23T19:27:08Z")

</div>

Mario S. wrote:

> On Sun, Nov 22, 2009 at 10:08 PM, Gary H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> 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/](http://www.ruby-forum.com/).
> > 
> > * * *
> > 
> > wxruby-users mailing list  
> > [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> > [http://rubyforge.org/mailman/listinfo/wxruby-users](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
