Search code examples
pythonkeyerror

Python - KeyError: 'dpi'


I have a python script that lists the metadata of photos inside a directory. It works however it is not fully working. When it runs I can get data from ~250 files then I get an error

This is the error:

  File "c:\Users\edward\OneDrive - ISC Industries\Summer Intern 2022\Scripts\metadata.py", line 22, in <module>
"Image DPI": image.info['dpi'],
KeyError: 'dpi'

I am not sure why this is since it works for some files but there is over 17000 files so I would like for all of the data to be printed.

Here is the full script:

import json
from PIL import Image
from PIL.ExifTags import TAGS
import os
import os.path
import PIL
from pandas import json_normalize

PIL.Image.MAX_IMAGE_PIXELS = 384000000
rootdir = r"C:\Users\edward\OneDrive\Pics"

newfile = newfile = open('meta.txt', 'w')
newfile.write("Filename                                     |  Image DPI                    | Image Height                  |   Image Width                 |   Image Format                |   Image Mode                  |   Image Frames                |\n")
for file in os.listdir(rootdir):
    # read the image data using PIL
    image = Image.open(os.path.join(rootdir, file))

# extract other basic metadata
info_dict = {
    "Filename": image.filename,
    "Image DPI": image.info['dpi'],
    "Image Height": image.height,
    "Image Width": image.width,
    "Image Format": image.format,
    "Image Mode": image.mode,
    "Frames in Image": getattr(image, "n_frames", 1)
}

line = ""
for label, value in info_dict.items():
    line += f"|{str(value):<30} "  
line += " |"  
newfile.write(line + '\n')

Thank you


Solution

  • You could use try and except Just reuse your code:

    for file in os.listdir(rootdir):
    try:
        # read the image data using PIL
        image = Image.open(os.path.join(rootdir,file))
    
        # extract other basic metadata
        info_dict = {
            "Filename": os.path.basename(image.filename),
            "Image DPI": image.info['dpi'],
            "Image Height": image.height,
            "Image Width": image.width,
            "Image Format": image.format,
            "Image Mode": image.mode,
            "Frames in Image": getattr(image, "n_frames", 1)
        }
    
        line = ""
        for label, value in info_dict.items():
            line += f"|{str(value):<30} "  
        line += " |"  
        newfile.write(line + '\n')
    except:
        # read the image data using PIL
        image = Image.open(os.path.join(rootdir,file))
    
        # extract other basic metadata
        info_dict = {
            "Filename": os.path.basename(image.filename),
            "Image Height": image.height,
            "Image Width": image.width,
            "Image Format": image.format,
            "Image Mode": image.mode,
            "Frames in Image": getattr(image, "n_frames", 1)
        }
    
        line = ""
        for label, value in info_dict.items():
            line += f"|{str(value):<30} "  
        line += " |"  
        newfile.write(line + '\n')