简体   繁体   English

Python - 尝试将元数据打印到表中

[英]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我有一个 python 脚本,它现在可以获取照片的元数据并将其打印到一个文本文件中,但是我想将它打印到一个看起来像这样的表中

| 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!任何帮助都会很棒!

You can change your second for loop to print it horizontally:您可以更改第二个for循环以水平打印:

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)

Try creating a pandas Dataframe from the dict you have.尝试从您拥有的 dict 创建一个 pandas 数据框。 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供参考: https ://pandas.pydata.org/docs/reference/api/pandas.DataFrame.from_dict.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM