【问题标题】:Python Extract URL (`where from`) information of downloaded imagePython 提取下载图像的 URL(`where`)信息
【发布时间】:2019-11-13 21:35:56
【问题描述】:

我从网站下载了一张图片,我可以手动看到我下载图片的 URL 出现在图片的信息对话窗口中。

我想用python来提取这个字段的最后一部分,即0001ss180819.png

我试过PIL:

from PIL import Image, ExifTags

img = Image.open("/Users/anonymous/Downloads/figs/image (1).jpeg")
exif = { ExifTags.TAGS[k]: v for k, v in img._getexif().items() if k in ExifTags.TAGS }

但我无法获得所需的信息,即where from 字段,如下所示。

我的目标是根据这些信息重命名照片。

【问题讨论】:

    标签: python macos python-imaging-library cv2


    【解决方案1】:

    下载文件的 url 不在图像本身内部,而是作为扩展属性(来自 macOS)。见https://superuser.com/questions/214934/how-can-mac-os-x-save-details-about-the-url-from-which-a-file-has-been-downloade

    【讨论】:

    • 感谢您的回复。那么有没有办法用python做到呢?
    • 如果需要,您可以运行 xattrmdls 从 Python 中提取该信息。
    【解决方案2】:

    仅作记录,我已经设法在 python 中找到了解决方案:

    from subprocess import call
    import os, numpy as np
    
    path = "/Users//Downloads/photos/"
    files = [f for f in os.listdir(path)]
    n_images = len(files)
    
    for i in range(n_images):
        full_file_path = os.path.join(path + files[i])
        tmp = os.popen("mdls -name kMDItemWhereFroms '{}'".format(full_file_path)).read()
        ID = tmp.split('file=')[-1].split('.')[0].split('ss')[0]
        new_name = '/Users//Downloads/photos/' + ID + '.jpeg'
        call(["mv", "{}".format(full_file_path), "{}".format(new_name)])
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-02-28
      • 1970-01-01
      • 2023-04-02
      • 1970-01-01
      • 2013-09-09
      • 2019-08-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多