【问题标题】:Polaroid Effect with Images in PythonPython中带有图像的宝丽来效果
【发布时间】:2016-12-20 00:46:30
【问题描述】:

我正在尝试拍摄图像并从 imagemagick http://www.imagemagick.org/Usage/transform/#polaroid 添加以下效果。我搜索了 Python 代码示例并没有成功。我不需要使用 imagemagick(wand, pythonmagick, etc.) 这只是我能找到的唯一例子。我不想使用列出的命令行示例。我希望能够将它包含在我的照相亭 python 代码中。

【问题讨论】:

  • 您为什么不想使用基于子流程/命令行的方法?这是最简单、最强大的(访问所有选项)。 Wand 似乎不支持它(宝丽来),Pythonmagick 甚至没有在我的系统上编译,我不知道这些库交织在一起有多深(支持一切)。使用 python 的tempfile,基于 cli 的方法可能非常干净。
  • 我不熟悉 python 的 tempfile 模块。你有没有一个例子可以在 python 脚本中干净地运行宝丽来命令?

标签: python imagemagick python-imaging-library imagemagick-convert pythonmagick


【解决方案1】:

使用,您需要实现C-API 方法MagickPolaroidImage & MagickSetImageBorderColor

import ctypes

from wand.api import library
from wand.color import Color
from wand.drawing import Drawing
from wand.image import Image

# Tell Python about C library
library.MagickPolaroidImage.argtypes = (ctypes.c_void_p,  # MagickWand *
                                        ctypes.c_void_p,  # DrawingWand *
                                        ctypes.c_double)  # Double

library.MagickSetImageBorderColor.argtypes = (ctypes.c_void_p,  # MagickWand *
                                              ctypes.c_void_p)  # PixelWand *


# Define FX method. See MagickPolaroidImage in wand/magick-image.c
def polaroid(wand, context, angle=0.0):
    if not isinstance(wand, Image):
        raise TypeError('wand must be instance of Image, not ' + repr(wand))
    if not isinstance(context, Drawing):
        raise TypeError('context must be instance of Drawing, not ' + repr(context))
    library.MagickPolaroidImage(wand.wand,
                                context.resource,
                                angle)

# Example usage
with Image(filename='rose:') as image:
    # Assigne border color
    with Color('white') as white:
        library.MagickSetImageBorderColor(image.wand, white.resource)
    with Drawing() as annotation:
        # ... Optional caption text here ...
        polaroid(image, annotation)
    image.save(filename='/tmp/out.png')

【讨论】:

  • 有没有办法操纵阴影?使用较大的图像,阴影看起来不是很吸引人!
猜你喜欢
  • 2012-09-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-01
  • 2013-04-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多