tario / imageruby

flexible and easy to use ruby gem for image processing
http://tario.github.com/imageruby/doc/
GNU General Public License v3.0
16 stars 1 forks source link

scaling down a jpg or png #6

Closed slavajacobson closed 11 years ago

slavajacobson commented 11 years ago

Hi,

I've been trying to find a ruby gem which I can use to resize images and that won't require the user to install any additional software.

Is it possible to do it with imageruby?

tario commented 11 years ago

Imageruby only provides a common interface to avoid annoying dependencies common on image libraries such as ImageMagick, it applies when possible the Dependency Inversion principle (this imply that is not possible to make a "supporting all features" gem), the idea behind imageruby is to minimize the dependecies by specific features. To resize images using "imageruby" you must install both imageruby and imageruby-devil (https://github.com/tario/imageruby-devil) gems, imageruby-devil is a bridge with devil image library and provides many fuctions (including resize) and loaders for many formats including jpg and png. If you think that the resize effect should be available in a gem made with pure ruby, letme now and maybe I will try to implement something, but you should be warned that the performance will be very poor (comparing to C/C++ implementations on ImageMagick or devil library)

I will close this issue since your question was answered, if you found some specific bug on imageruby or imageruby-devil please open a separated issue

slavajacobson commented 11 years ago

It would be really nice if there was resizing method in pure ruby. Something like this: .resize(width: 150, height: 150).

I wonder how bad the performance will be though, if it would take more than 3 seconds to scale down a 1mb file, then i guess it wouldn't be worth it.

tario commented 11 years ago

I will try, maybe with the latest implementations (ruby2.0, may be), we can acchieve such that performance. You can sent me sample images to test if you want, but of course I can use my own test images

tario commented 11 years ago

Also, if you want you should try too

tario commented 11 years ago

Try this:

require "imageruby"
include ImageRuby

class Image
  def resize(newwidth, newheight)
    xproportion = width.to_f / newwidth
    yproportion = height.to_f / newheight
    Image.new(newwidth,newheight) do |x,y| 
      self[(x*xproportion).to_i,(y*yproportion).to_i] 
    end
  end
end

path = "/usr/lib/libreoffice/share/template/wizard/bitmap/MS-Import_2-1.bmp"
Image.from_file(path).resize(200,20).save("test.bmp", :bmp)

I will package this into a gem togheter with other useful methods when they are ready, if you want you can play with the code to get more sophisticated resize algorithms (for example, color interpolation, etc...)

slavajacobson commented 11 years ago

This is awesome! but is there a way to make it with with JPG files?

tario commented 11 years ago

This is another matter, you should use a JPG loader, these loader and loaders for other formats are included on imageruby-devil, but maybe it isn't what you want (imageruby-devil depends on ruby devil image library). If you found a pure ruby gem which loads JPGs please let me know, (dealing with JPG format is beyond my capabilities right now). If you want to write your own decoder/encoder, you can follow this example: https://github.com/tario/imageruby-bmp/blob/master/lib/imageruby-bmp.rb

Once you define the decoder, the only thing you need to do is simply using it with Image.from_file method, there is no need to setup anything else, only define the decoder class extending ImageRuby::Decoder