简体   繁体   English

保存图像文件Python的快速方法

[英]Fast way to save Image Files Python

I have a folder of 10 000 images and I am iterating one by one in a for loop and every time after processing I am saving the modified image in a file. 我有一个包含10 000张图像的文件夹,并且在for循环中一个接一个地迭代,每次处理后,我都将修改后的图像保存在文件中。 Problem with the execution is it is taking long time to process even 500 images and I see CPU Usage in Windows Task Manager are going up to 80%. 执行的问题是处理500个图像需要花费很长时间,而且我看到Windows Task Manager中的CPU使用率上升了80%。

How to speed up below code? 如何加速以下代码? Anything like save all processed image in memory and write it at single shot? 像将所有处理过的图像保存在内存中并一次性写入一样吗?

    from PIL import Image
    from resizeimage import resizeimage
    for imgnm in range(0, samples):
        start = time.time()
        filename=filenames[imgnm]
        img = Image.open(os.path.join(imagedir,filename))
        img=resizeimage.resize_crop(img, [700, 700])
        (img.resize((700,700),Image.ANTIALIAS)).save(os.path.join(subdir,filename),quality=40)
        img.close()

How to speed up below code? 如何加速以下代码?

  1. Use latest Pillow version 使用最新的枕头版本
  2. Use Pillow-SIMD instead (drop-in replacement for CPUs with at least SSE4) 请改用Pillow-SIMD(至少具有SSE4的CPU的替代产品)
  3. Use the less expensive filter for resizing: Image.BICUBIC or even Image.BILINEAR 使用较便宜的过滤器来调整大小: Image.BICUBIC甚至Image.BILINEAR
  4. Do one resize and one crop (before resize) instead of using both resizeimage and img.resize . 进行一次调整大小和裁剪(在调整大小之前),而不要同时使用resizeimageimg.resize
  5. Save images to the fast format. 将图像保存为快速格式。 Different formats operate at a different speed. 不同的格式以不同的速度运行。 PNG in the slowest one, while JPEG and TIFF are the fastest. PNG速度最慢,而JPEG和TIFF速度最快。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM