0

I have some code that prints the file name name however it prints out the whole file path instead of just the name

Here is what it prints now:

C:\Users\edward\OneDrive\Pics\X00DL0027.jpg 

Here is what I want

X00DL0027.jpg

Here is the 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')

I am not sure what to change within the script.

Any help would be great

PyMan
  • 132
  • 13

1 Answers1

1

Use this: "Filename": os.path.basename(image.filename),

Edward Wynman
  • 363
  • 1
  • 10