【问题标题】:Pgmagick resize image by widthPgmagick 按宽度调整图像大小
【发布时间】:2011-12-25 10:49:58
【问题描述】:

我正在使用 graphicsmagick 对图像进行简单的操作,例如调整大小、裁剪等,我正在尝试通过 python 使用 pgmagick。

为了调整大小,脚本发出以下命令

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

根据宽度调整法师的大小是我主要关心的问题,上面的命令按我的预期工作,只考虑调整大小的宽度。 使用 pgmagick,我尝试了下面给出的相同操作:

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

pgmagick 的比例似乎不尊重该参数。上面的代码按适当的宽度或高度缩放图像。例如,如果图像尺寸为 240x320,则生成的图像尺寸为 60x80。就 graphicsmagick 而言,它的行为类似于

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

如何使用 pgmagick 根据宽度缩放图像?

【问题讨论】:

    标签: python image-resizing graphicsmagick


    【解决方案1】:

    不幸的是,无法在 pgmagick 中找到实现“resize”的方法,因为它使用的是 c++ api,目前仅支持 scale op。

    但实现起来却很丑:

    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)
    

    如果宽度在调整大小时很重要,则最终大小分别根据宽度和高度进行调整。例如:一张240x360的图片会通过resize('80x')来调整大小,然后函数计算最终高度为360*80/240,并使用图片上的缩放为img.scale('80x120'),否则只使用缩放直接会产生 53.3x80 的新图像

    【讨论】:

      猜你喜欢
      • 2013-04-28
      • 1970-01-01
      • 2013-04-27
      • 1970-01-01
      • 1970-01-01
      • 2015-05-22
      • 2013-11-03
      • 2012-01-24
      • 1970-01-01
      相关资源
      最近更新 更多