简体   繁体   English

python 中的图像到像素阵列

[英]Image to pixel array in python

Info: I have 30,000 jpg images that I need to convert into (NumPy) pixel arrays.信息:我有 30,000 张 jpg 图像需要转换为(NumPy)像素 arrays。

Problem: I have tried using Pillow to do the image conversions but it does about 2 images a second which would take hours to complete.问题:我曾尝试使用 Pillow 进行图像转换,但它每秒处理大约 2 张图像,这需要数小时才能完成。

from PIL import Image

for img_num in range(30_000):
   img = Image.open(img_dir+img_num+extension)
   img_list.append(np.array(img))

Question: What is the best and fastest way to convert a large number of jpg images to pixel arrays using python.问题:使用 python 将大量 jpg 图像转换为像素 arrays 的最佳和最快方法是什么。

I think what is taking the longest is the append() function.我认为花费时间最长的是append() function。

Also, you are appending 30000 images to img_list , this means this single variable is extremely heavy in memory, do you actually need it?另外,您将 30000 张图像附加到img_list ,这意味着这个单个变量在 memory 中非常重,您真的需要它吗? (if you image had 1000 pixels, you'd already be trying to allocate more than 30Mb) (如果您的图像有 1000 像素,那么您已经尝试分配超过 30Mb)

In PIL and openCV the read or open` functions directly make them jumpy arrays.PILopenCV中, read or open` 函数直接使它们跳起来 arrays。

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

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