使用 PIL 进行批量图像处理? [英] Batch image manipulation with PIL?

查看:72
本文介绍了使用 PIL 进行批量图像处理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做一个项目时刚刚遇到了 pythons PIL 库,所以对此很陌生.

I have just come across the pythons PIL library while working on a project, so very new to this.

我的简单程序从目录中导入一张图像,使用 PIL 应用所需的操作,然后将其保存到不同的文件夹中.

My simple program imports one image from a directory, applies the desired manipulation with PIL, then saves it into a different folder.

我的问题 - 我可以批量导入包含多个图像的目录并在 PIL 中对该目录中的所有图像运行所需的操作吗?

My question - Can I batch import a directory with multiple images and run the desired manipulation for all images within that directory in PIL?

推荐答案

一般来说,当处理对内存要求很高的图像时,将数千张图像批量加载到内存中,然后处理它们并不是最好的主意全部,然后将它们全部写出来,因为您对计算机的 RAM 产生了巨大的需求,这可能会减慢它的速度.这导致代码更像这样:

In general, when working with images which are often quite demanding on memory, it is not the best idea to batch load thousands of images into memory, then process them all, then write them all out as you create an enormous demand on your computer's RAM which can slow it down. That leads to code more like this:

#!/usr/bin/env python3

import glob
from PIL import Image

def ProcessOne(f):
   print(f'Opening {f}')
   im = Image.open(f)
   ... process ...
   ... process ...

if __name__ == '__main__':

   # Create a list of files to process
   files = [f for f in glob.glob("*.jpg")]

   for f in files:
       ProcessOne(f)

此外,如果您要对大量图像进行相同的处理,那么使用 Python 的 multiprocessing 模块通常是一个合理的想法,因为就其本身而言,Python 不会使用所有这些可爱的您为英特尔支付了如此丰厚的 CPU 内核,这是一个严肃的考虑,因为 CPU 继续变得更胖(更多内核)而不是更高(更多 GHz).这导致代码更像这个例子,几乎没有更难编写或阅读:

Also, if you are doing the same processing on lots of images, it is then generally a reasonable idea to use Python's multiprocessing module, because, on its own, Python will not use all those lovely CPU cores that you paid Intel so handsomely for and this is a serious consideration as CPUs continue to get fatter (more cores) rather than taller (more GHz). That leads to code more like this example which is hardly any more difficult to write or read:

#!/usr/bin/env python3

import glob
from multiprocessing import Pool
from PIL import Image

def ProcessOne(f):
    im = Image.open(f)
    ... process ...


if __name__ == '__main__':
    # Create a pool of processes to check files
    p = Pool()

    # Create a list of files to process
    files = [f for f in glob.glob("*.jpg")]

    print(f'Files to process: {len(files)}')

    # Map the list of files to check onto the Pool
    p.map(ProcessOne, files)


另请注意,您可以使用 ImageMagick 来简单地处理数百个文件并将结果写入不同的目录.因此,假设您想标准化一个充满 JPEG 的整个目录的亮度级别,并将修改后的文件写入名为 OUTPUT 的目录中,您可以在终端中执行此操作:


Note also that you can use ImageMagick to simply process hundreds of files and write the results into a different directory. So, say you wanted to normalise the brightness level of a whole directory full of JPEGs and write the modified files into a directory called OUTPUT, you could just do this in Terminal:

mkdir -p OUTPUT
magick mogrify -path OUTPUT -auto-level *.jpg

这篇关于使用 PIL 进行批量图像处理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆