简体   繁体   中英

Image to pixel array in python

Info: I have 30,000 jpg images that I need to convert into (NumPy) pixel 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.

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.

I think what is taking the longest is the 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? (if you image had 1000 pixels, you'd already be trying to allocate more than 30Mb)

In PIL and openCV the read or open` functions directly make them jumpy arrays.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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