如何在Python / Django中获取图像标题 [英] How to get image title in Python/Django
问题描述
我想知道Facebook和Flicker如何在上传图片时获得图片标题。
有两件事要注意。
- 图片标题(我们大多称呼名称)
- 图片说明。标题(您可以查看图片属性并转到详细信息,并参阅标题下的描述标题,这是隐含的,不可见的标题)
当我们上传图片到 Facebook
和 Flicker
,他们从图像中提取此信息,并设置为标题。
如何使用Python / Django / PIL?
查找
图像二(带标题和主题)
注意:
当我在记事本中打开文件时,我可以看到这里的描述。
; DC:描述> <的rdf:Alt和GT; < rdf:li xml:lang =x-default> heeeeeeeeeeeeeeeee< / rdf:li> < / RDF:Alt和GT; < / DC:描述>
您可以使用PIL:
从PIL导入图像
img = Image.open('img.jpg')
exif_data = img._getexif )
(我从这个答案中得到: https://stackoverflow.com/a/4765242/2761986 )
编辑:要获得一个更干净的字典,做这个(从上面的答案中借用):
从PIL.ExifTags导入TAGS
exif = {
标签[k]:v
for k,v in img._getexif()。items()
如果k在TAGS
}
I was wondering how Facebook and Flicker get the image title when you upload it.
There are two things to be noted.
- Image Title (Which we mostly call name)
- 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.
Second Image.
Image Links
Image One(with title only) 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>
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
}
这篇关于如何在Python / Django中获取图像标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!