Pgmagick 按宽度调整图像大小
我正在使用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缩放图像的宽度?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不幸的是,无法找到在 pgmagick 中实现“调整大小”的方法,因为它使用 c++ api,目前仅支持缩放操作。
然而它的实现却很丑陋:
如果宽度在调整大小时很重要,那么最终的大小分别根据宽度和高度进行调整。例如:240x360的图像将通过 resize('80x') 调整大小,然后该函数按 360*80/240 计算最终高度,并使用图像上的比例为 img.scale('80x120'),否则仅使用比例直接会产生 53.3x80 的新图像
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:
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