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

Python – Trying to print metadata into a table

I have a python script that gets the meta data of photos and prints it out into a text file for now however I’d like to print it into a table that look like that

| Filename | DPI | Height | Width | Format | Mode | Frames |

Here is the script:

from PIL import Image
from PIL.ExifTags import TAGS
import os
import os.path
import PIL

PIL.Image.MAX_IMAGE_PIXELS = 384000000

rootdir = r"C:\Users\edward\OneDrive - ISC Industries\Summer Intern 2022\Suspensia Pictures"

newfile = newfile = open('meta.txt', 'w')

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)
    }

    for label, value in info_dict.items():
        #print(f"{label:25}: {value}")
        newfile.write(f"{label:25}: {value}"+'\n')

And the current output looks like this:

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

Filename                 : C:\Users\Eddie\Pictures\pics\X01CJ0035.JPG
Image DPI                : (72.0, 72.0)
Image Height             : 400
Image Width              : 600
Image Format             : JPEG
Image Mode               : RGB
Frames in Image          : 1

I would like to somehow print this data into a table and not have to have everything into a table and I am not sure how to do this.

Any help would be great!

>Solution :

You can change your second for loop to print it horizontally:

newfile.write("Filename   |  Image DPI    | ...\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 += " |\n"  
    #print(line)
    newfile.write(line)
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