Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Time printed but image not loaded

I created a dummy function to let me know which image is loading and how long it takes:

from PIL import Image
from timeit import default_timer as timer

def ImageLoad(path: str):
    print("Loading '" + path + "'... ", end='')
    start = timer()
    im = Image.open(path)
    print("successfully (%.2fs)." % (timer()-start))
    return im

But when I use it, it always indicate 0 second even though the images are huge (about 20K x 60K pixels):

Loading 'Image0.png'... successfully (0.00s).
Loading 'Image1.png'... successfully (0.00s).

I added another print after the function is called:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

image = ImageLoad(path)
print("Image loaded").

I see the second print being displayed at least 40 seconds after, which makes perfect sense.

Why am I getting the the message immediately displayed despite the fact that the image is not loaded?

>Solution :

According to the Pillow docs:

This is a lazy operation; this function identifies the file, but the file remains open and the actual image data is not read from the file until you try to process the data

PIL.Image.open doesn’t immediately open the image file. If you want to immediately load the file, call the load() method on the file.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading