【问题标题】:Python Wand how to resize keeping aspect ratio and filling in the remaining space with an transparencyPython Wand如何调整大小保持纵横比并用透明度填充剩余空间
【发布时间】:2014-08-22 01:37:02
【问题描述】:

我正在尝试使用 python Wand 调整图像大小,保留纵横比并用透明度填充剩余位置。例如,我想在保持纵横比的同时调整一个大矩形的大小以适应 100x100 图像,但我想将图像返回为 100x100,并在间隙中填充透明度。这可以使用魔杖吗?

我测试了 img.resize() 和 img.transform() ,它似乎只保留了纵横比,而不是请求的图像大小。

如果 Wand 无法做到这一点,还有其他库可以做到吗?

感谢您的帮助!

【问题讨论】:

    标签: python image imagemagick wand


    【解决方案1】:

    我的解决方案是创建一个空白的外部图像来覆盖转换后的图像。这会产生预期的宽度和高度,同时在保留纵横比的范围内拟合图像。

    def resizeImg(sourceFile, destFile, width, height):                                                                                                                                                                                                                                       
      with Image(width=width, height=height) as outerImg:                                                                                                                                                                                                                                     
        with Image(filename=sourceFile) as img:                                                                                                                                                                                                                                               
          img.transform(resize="%dx%d>" % (width, height))                                                                                                                                                                                                                                    
          outerImg.format = img.format.lower()                                                                                                                                                                                                                                                
          outerImg.composite(img, left=(width - img.width) / 2, top=(height - img.height) / 2)                                                                                                                                                                                                
          outerImg.save(filename=destFile)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-14
      • 1970-01-01
      • 2019-08-10
      • 2014-01-08
      • 2012-11-15
      相关资源
      最近更新 更多