Question

I'm using graphicsmagick to do simple operations on images such as resizing, croppping etc, and i'm trying to use pgmagick via python instead.

For resizing, a script issues the following commands

gm convert image.jpg -resize 80x ...

resizing the mages according to width is my main concern, and the command above works as i expect, considers only the width for resizing. Using pgmagick, i try the same operation as given below:

import pgmagick as p
img = p.Image(p.Blob(open('image.jpg','rb').read()))
img.scale('80') # or img.scale('80x')

pgmagick's scale does not seem to respect the parameter. code above scales the image by width or height, which ever is appropriate. for example if the image has 240x320 dimensions, the resulting image has 60x80 dimensions. in terms of graphicsmagick it behaves like

gm convert image.jpg -resize \>80x -resize x80\> ...

How can i scale the image in respect to width using pgmagick?

Was it helpful?

Solution

Unfortunately could not find a way to implement "resize" in pgmagick for it uses the c++ api, which only has support for scale op for now.

Yet implemented it as ugly as:

def resize(new_size):
     g = pgmagick.Geometry(new_size) # distinguishes whether width, height or both given
     image = pgmagick.Image(img)
     rw, rh = image.size()
     w, h = g.width(), g.height()
     if w and not h:
         g.height(int(rh * w * 1.0/rw))
     elif h and not w:
         g.width(int(rw * h * 1.0/rh))

     image.scale(g)

if width is important in resizing, then the final size adjusted according to width, and for height respectively. For example: an image of 240x360 will be resized by resize('80x'), then the function calculates the final height by 360*80/240 and uses scale on image as img.scale('80x120'), otherwise using only scale directly would result in a new image of 53.3x80

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top