【问题标题】:Stack two images side by side using Wand使用 Wand 并排堆叠两个图像
【发布时间】:2017-07-20 00:12:01
【问题描述】:

如何在 wand (python) 中并排堆叠两个图像?复合方法是可用的,但它将一个图像放在另一个图像之上。 我想要类似 numpy.vstack 的东西。

【问题讨论】:

    标签: python image-processing wand


    【解决方案1】:

    您还可以使用smush() 与图像之间的可选间距,并且:

    • stacked=True 用于垂直(一个在另一个之上)堆叠,或
    • stacked=False 用于并排堆叠。

    from wand.image import Image
    
    im = Image(filename="rose:")
    with im as output:
        output.sequence.append(im)
        output.sequence.append(im)
        output.smush(True, 5)
        output.save(filename='result.png')
    

    如果您不希望图像之间有任何间距,请使用offset=0 或仅使用concat() 而不是smush(),但方式完全相同。

    【讨论】:

      【解决方案2】:

      wand.image.Image.composite 方法接受 top 和 left 参数。并排合成图像并不费力......

      with Image(filename="rose:") as left:
        with Image(filename="rose:") as right:
          with Image(width=left.width+right.width,
                     height=max(left.height, right.height)) as output:
            output.composite(image=left, left=0, top=0)
            output.composite(image=right, left=left.width, top=0)
            output.save(filename="hstack.png")
      

      ...或堆叠...

      with Image(filename="rose:") as top:
        with Image(filename="rose:") as bottom:
          with Image(width=max(top.width, bottom.width),
                     height=top.height + bottom.height) as output:
            output.composite(image=top, left=0, top=0)
            output.composite(image=bottom, left=0, top=top.height)
            output.save(filename="vstack.png")
      

      当然你可以把上面的例子简化,或者用wand.api.library实现MagickAppendImage。

      【讨论】:

      • 谢谢,我去看看。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-01-16
      • 2021-12-15
      • 1970-01-01
      • 2016-01-31
      • 2023-03-26
      • 2021-03-30
      • 1970-01-01
      相关资源
      最近更新 更多