实用程序作为Python子进程

实用程序作为Python子进程

本文介绍了使用ImageMagick的“转换”实用程序作为Python子进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找转换大型缩略图目录。

I'm looking to convert a large directory of thumbnails.

而不是使用PythonMagick包装器我想直接访问转换二进制文件(我有很多标志,并认为这对于大量数据更有效照片。)

Instead of using the PythonMagick wrapper I'd like to access the convert binary directly (I have a lot of flags, and think this would be more efficient for a large quantity of photos.)

是否有使用ImageMagick作为子进程的工作示例?或者,有更好的方法吗?

Are there any working examples of using ImageMagick as a subprocess? Or, is there a better way to do this?

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

Specifically, I'm not sure how to start and end a Python subprocess from within a class. My class is called ThumbnailGenerator. I'm hoping to make something like this:

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


推荐答案

这是我在一个项目中使用的内容:

Here's what I've used in one project:

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])

这将为每次转换启动一个进程。如果源图像不是两个小的,则进程启动开销会相对较小。

This will start one process per conversion. If the source images aren't two small, the process startup overhead will be comparatively small.

这篇关于使用ImageMagick的“转换”实用程序作为Python子进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 06:34