使用ImageMagick的“convert”实用程序作为Python子进程

2024-10-01 17:34:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我想转换一个大目录的缩略图。在

我不想使用PythonMagick包装器,而是直接访问convert二进制文件(我有很多标志,我认为这对于大量的照片来说更有效)

有没有使用ImageMagick作为子流程的实例?或者,有更好的方法吗?在

具体地说,我不确定如何从类中启动和结束Python子进程。我的课叫ThumbnailGenerator。我希望能做出这样的事情:

>> t = ThumbnailGenerator()
>> t.makeThumbSmall('/path/to/image.jpg')  
>> True

Tags: 文件实例方法目录convert进程标志二进制
1条回答
网友
1楼 · 发布于 2024-10-01 17:34:19

以下是我在一个项目中使用的方法:

def resize_image(input, output, size, quality=None, crop=False, force=False):
    if (not force and os.path.exists(output) and
        os.path.getmtime(output) > os.path.getmtime(input)):
        return
    params = []
    if crop:
        params += ["-resize", size + "^"]
        params += ["-gravity", "Center", "-crop", size + "+0+0"]
    else:
        params += ["-resize", size]
    params += ["-unsharp", "0x0.4+0.6+0.008"]
    if quality is not None:
        params += ["-quality", str(quality)]
    subprocess.check_call(["convert", input] + params + [output])

这将在每次转换中启动一个进程。如果源映像不小于两个,则进程启动开销将相对较小。在

相关问题 更多 >

    热门问题