6

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> 
bastelflp
  • 9,362
  • 7
  • 32
  • 67
A.J.
  • 8,557
  • 11
  • 61
  • 89
  • Take a look at [EXIF](http://en.wikipedia.org/wiki/Exif) and at [modules matching "exif"](https://pypi.python.org/pypi?%3Aaction=search&term=exif&submit=search) in PyPI – lanzz Mar 04 '14 at 13:59
  • 1
    Ok thanks, i checked https://pypi.python.org/pypi/ExifRead/1.4.2 but it didnt find the title field but have found others. – A.J. Mar 04 '14 at 14:26
  • 2
    I was unable to find and read the title in the exif. I think you should look in to them also?, or you can use the image in the question. and try to read the title – A.J. Mar 04 '14 at 14:44

2 Answers2

1

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
}
Community
  • 1
  • 1
jproffitt
  • 6,225
  • 30
  • 42
  • Thanks, for the answer, but i am looking for the Title field, it still unavailable in the exif. – A.J. Mar 04 '14 at 14:38
  • I was unable to find and read the title in the exif. I think you should look in to them also?, or you can use the image in the question. and try to read the title – A.J. Mar 04 '14 at 14:45
  • I will try it. What are you expecting the title to be in that example image? – jproffitt Mar 04 '14 at 14:47
  • Download the image, https://dl.dropboxusercontent.com/u/10712627/Lahore%20Fort,%20Lahore,%20Pakistan..jpg and then try to find it under the properties. Details. Description. Title. it says "Lahore Fort, Lahore, Pakistan." – A.J. Mar 04 '14 at 14:49
  • That looks like just the name of the file. To extract the name, minus the extension use: `os.path.splitext(img.filename)[0]`. – jproffitt Mar 04 '14 at 14:54
  • If your using, django, you don't even need PIL to get the file name, just do this: `request.FILES['my_file'].name` – jproffitt Mar 04 '14 at 14:55
  • Oh sorry, accidently that looks just like the name. I changed it to "This is my hidden title", u can re-download it and check this out. – A.J. Mar 04 '14 at 15:05
  • Well Now i can see the title in the exif. Do what I said in the answer, then `exif['ImageDescription']` is `u'This is my hidden title'` – jproffitt Mar 04 '14 at 15:14
  • perfect, worked without anyproblem, this is what i was looking for. +1 – A.J. Mar 06 '14 at 06:08
  • This is not that easy as it looks like? after playing some around, i came to find out that. There is a little problem. Let me eidt the question. – A.J. Mar 06 '14 at 06:47
1

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

vicarage
  • 83
  • 1
  • 6