2

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:

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!

PyMan
  • 132
  • 13

2 Answers2

2

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)
ItayB
  • 10,377
  • 9
  • 50
  • 77
-2

Try creating a pandas Dataframe from the dict you have. You can later add row per each dict. Don't forget to import pandas.

For reference: https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.from_dict.html