How to get image title in Python/Django

前端 未结 2 1537
独厮守ぢ
独厮守ぢ 2020-12-11 21:23

I was wondering how Facebook and Flicker get the image title when you upload it.

There are two things to be noted.

  1. Image Title (Which we mostly call n
相关标签:
2条回答
  • 2020-12-11 21:41

    I was looking to get image titles into caja-columns, and using the help here came up with

    Install caja-columns.py from https://gist.github.com/infirit/497d589c4dcf44ffe920

    Edit code to have

    from PIL import Image
    from PIL import ExifTags
    from PIL.ExifTags import TAGS
    
    ...
    
    
                                im = Image.open(filename)
                                exif_data = im._getexif()
                                exif = {
                                        TAGS[k]: v
                                        for k, v in im._getexif().items()
                                        if k in TAGS
                                }
    
                                file.add_string_attribute('title',exif['ImageDescription'])                                file.add_string_attribute('pixeldimensions',str(im.size[0])+'x'+str(im.size[1]))
    

    And you will hopefully get a populated Title field for images

    0 讨论(0)
  • 2020-12-11 21:53

    You can do it with PIL:

    from PIL import Image
    img = Image.open('img.jpg')
    exif_data = img._getexif()
    

    (I got that from this answer: https://stackoverflow.com/a/4765242/2761986)

    EDIT: To get a cleaner dictionary, do this (borrowed from answer above):

    from PIL.ExifTags import TAGS
    exif = {
        TAGS[k]: v
        for k, v in img._getexif().items()
        if k in TAGS
    }
    
    0 讨论(0)
提交回复
热议问题