A.J.
A.J.

Reputation: 9005

How to get image title in Python/Django

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 name)
  2. Image Description.Title (you can check the picture properties and go to Details, and see Title under description heading, this is implicit and not visible title)

While we upload picture to Facebook and Flicker, they extract this information from image, and set as a title.

How can i do that with Python / Django / PIL?

Find this image as an example. Download and check its properties, Details, Description and Title. Try to upload on Facebook and Flicker, they extract this info.


EDIT

Here is the image that shows what i am trying. Look The title field has been populated, And facebook is extracting this info, but Not the Python code. When i add Subject too?(that's a field under the title), i was able to get the imageDescripiton. Here are two images.

enter image description here

Second Image.

enter image description here

Image Links

Image One(with title only) and Image Two(with title and Subject)

NOTE:

When i open the file in notepad, i can see the description there..

<dc:description> <rdf:Alt> <rdf:li xml:lang="x-default">heeeeeeeeeeeeeeeee</rdf:li> </rdf:Alt> </dc:description> 

Upvotes: 6

Views: 8291

Answers (2)

vicarage
vicarage

Reputation: 93

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

Upvotes: 1

jproffitt
jproffitt

Reputation: 6355

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
}

Upvotes: 1

Related Questions