Menu

#309 Proportional height resize not working in Magick++

v1.0_(example)
closed-works-for-me
None
5
2016-02-22
2015-07-27
tecywiz121
No

I'm not sure if this is really a bug; I'm just getting started with GraphicsMagick.

Here's the problem I'm having: I want to resize an image, keeping aspect ratio, to have a specified width.

The imagemagick command I would use is convert in.jpg -resize 2000x out.jpg which works as expected.

Using graphicsmagick, with the command gm convert in.jpg -resize 2000x out.jpg does not work, and simply renders the original image.

This also happens using the STL api.

I'm using an older version of graphicsmagick from ubuntu trusty:
GraphicsMagick 1.3.18 2013-03-10 Q8

Discussion

  • Glenn Randers-Pehrson

    What are the dimensions of in.jpg ?

    In some versions, the omitted y-dimension isn't handled correctly and the documentation is (or was) ambiguous about that. For any version you can get what you want by using an arbitrarily large y-dimension:

    convert in.jpg -resize 2000x32000 out.jpg

    This works for ImageMagick also.

     

    Last edit: Glenn Randers-Pehrson 2015-07-27
  • Bob Friesenhahn

    Bob Friesenhahn - 2015-07-28
    • status: open --> closed
    • assigned_to: Bob Friesenhahn
     
  • Bob Friesenhahn

    Bob Friesenhahn - 2015-07-28

    This problem was fixed on 2014-04-23 and so it was already included in release 1.3.20 as of August 16, 2014. The current release is 1.3.22. Please lobby your OS distribution to update.

     
  • tecywiz121

    tecywiz121 - 2015-07-28

    Great, thanks for the information.

     
  • tecywiz121

    tecywiz121 - 2015-07-28

    I built the latest version from SF (1.3.21) and I'm still having the same problem. The command line tool works, but I can't figure out how to make it work using the API.

    I've attached a minimal example that shows the problem I'm having.

    I build it using:

    g++ main.cpp $(GraphicsMagick++-config --libs --cppflags --ldflags --cxxflags)
    
     
  • Bob Friesenhahn

    Bob Friesenhahn - 2015-07-29

    To be clear, this implemented feature is for the command-line which has no/little efficient access to the current image dimensions. In a C++ program you have complete access to the current image dimensions and can do any necessary math yourself.

    This is the C code (from GetMagickGeometry()) which is used to support the feature:

          /*
            Deal with width or height being missing from geometry.  Supply
            missing value required to preserve current image aspect ratio.
          */
          if (((flags & WidthValue) && !(flags & HeightValue)))
            *height=(unsigned long) floor(((double) former_height/former_width)*
                                          (*width)+0.5);
          else if ((!(flags & WidthValue) &&  (flags & HeightValue)))
            *width=(unsigned long) floor(((double) former_width/former_height)*
                                         (*height)+0.5);
    

    I see that you are using STL function objects, which is way cool. In the mean time, I think that you could create your own function object similar to the existing resizeImage() but which computes the desired size using math similar to GetMagickGeometry(). However, please note that the Magick++ Geometry class is exact (width, height, x, y, are always specified) and it does not internally support a way to know if the width or height dimension is missing. I see that the C library TransformImage() does use GetMagickGeometry() so it seems that all that is needed is an upgrade to the Geometry class to remember/reproduce that width or height is missing.

     
    • tecywiz121

      tecywiz121 - 2015-07-29

      I did try doing the math myself, but I couldn't seem to get at the image's geometry. I kept getting:

      Magick: Image does not contain a geometry reported by Magick++/lib/Exception.cpp:435 (throwExceptionExplicit)
      

      When I called Image::geometry().

      On the other hand, setting a very large dimension instead of zero does do what I want.

       

      Last edit: tecywiz121 2015-07-29
  • Bob Friesenhahn

    Bob Friesenhahn - 2015-07-29
    • summary: Proportional height resize not working --> Proportional height resize not working in Magick++
    • status: closed --> open
     
  • Bob Friesenhahn

    Bob Friesenhahn - 2015-07-29

    The image current dimensions are available via the Image rows() and columns() methods. It seems that Image geometry() is used for obscure purposes related to composition (e.g. for DisplaceCompositeOp) and cropping. I need to improve its documentation.

    Image placement offset can be obtained via the Image page() method.

     
  • Bob Friesenhahn

    Bob Friesenhahn - 2016-02-22
    • status: open --> closed-works-for-me
     

Log in to post a comment.