Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Most efficient method for reading/writing image files as numpy array

I am wondering what the most efficient methods for reading/writing image and PDF files as numpy arrays for processing.

So far I have seen scipy.ndimage.imread and using PIL and numpy, which yeild the following results:

import os
import glob
from scipy.ndimage import imread
from PIL import Image
import numpy as np
import timeit
iters = 2
def scipy_fun():
    for x in glob.glob("*.jpg"):
        px = imread(x)
def PIL_fun():
    for x in glob.glob("*.jpg"):
        with Image.open(x) as im:
            px = np.array(im)

print(timeit.Timer(scipy_fun).timeit(number=iters))
print(timeit.Timer(PIL_fun).timeit(number=iters))

running the script shows similar results with marginally better from scipy:

2.8794324089019234
3.0174482765699095

Are there any faster ways to do this?

like image 715
chase Avatar asked Sep 18 '26 13:09

chase


1 Answers

First, do this

pip install pdf2image

Then,

import numpy as np
from pdf2image import convert_from_path as read
import PIL
import cv2
#pdf in the form of numpy array to play around with in OpenCV or PIL
img = np.asarray(read('path to the pdf file')[0])#first page of pdf
like image 126
Santhosh Dhaipule Chandrakanth Avatar answered Sep 21 '26 03:09

Santhosh Dhaipule Chandrakanth



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!