I am trying to extract the Dpi value of an image Using python in one of my django powered web application.I am using following function to achieve my desired output that is the Dpi value of an image but i am facing an exception.
我试图在我的django驱动的web应用程序之一中使用python提取图像的Dpi值。我正在使用以下函数来实现我想要的输出,即图像的Dpi值,但我面临异常。
This is the Function to get DPI value of an Image
这是获取图像的DPI值的函数
def get_exif_data(fname):
"""Get embedded EXIF data from image file."""
ret = {}
try:
img = Image.open(fname)
if hasattr( img, '_getexif' ):
exifinfo = img._getexif()
if exifinfo != None:
for tag, value in exifinfo.items():
decoded = TAGS.get(tag, tag)
ret[decoded] = value
except IOError:
print 'IOERROR ' + fname
return ret
and this is the view where i have used that above function to get the DPI value of an Image.
这是我使用上面的函数来获取图像的DPI值的视图。
def get_dpi(request,image_id):
image = get_object_or_404(Photo,pk = image_id)
img = Image.open(image.photo)
dpi_info = get_exif_data(img)
context = RequestContext(request)
ctx = {'dpi':dpi_info}
return render_to_response('photo/download_image.html',ctx,context)
but i am facing the following exception
但我面临以下例外
1 个解决方案
#1
1
To begin, I have to ask why you want the DPI resolution. It's just a tag and doesn't really mean anything unless you are outputing to physical media. A 1000x1000 pixel image can be 10x10 at 100dpi or 100x100 at 10dpi, but it's still exactly the same image. Exactly the same pixels. It's hard to imagine scenarios where img.size
doesn't give you everything you need.
首先,我要问你为什么要DPI解决方案。除非您输出到物理媒体,否则它只是一个标记并不具有任何意义。 1000x1000像素的图像在100dpi时可以是10x10,在10dpi时可以是100x100,但它仍然是完全相同的图像。完全相同的像素。很难想象img.size不会为您提供所需的一切。
Having said that, if you want to get the exif tags for resolution try XResolution
from PIL.ExifTags
:
话虽如此,如果你想获得解析的exif标签,请尝试从PIL.ExifTags中获取XResolution:
import Image
from PIL.ExifTags import TAGS
img = Image.open("path/to/.jpg")
info = img._getexif()
exifObj = {}
if info != None:
for tag, value in info.items():
decoded = TAGS.get(tag, tag)
exifObj[decoded] = value
exifObj
now either empty or equals something like:
exifObj现在要么为空,要么等于:
{'YResolution': (720000, 10000), 'BitsPerSample': (8, 8, 8), 'ImageLength': 713, 'Orientation': 1, 'Copyright': 'Mark Meyer Photography', 'ExifImageWidth': 950, 'ExifImageHeight': 713, 'ColorSpace': 1, 'ResolutionUnit': 2, 'DateTime': '2015:01:30 21:37:51', 'XResolution': (720000, 10000), 'ExifOffset': 296, 'PhotometricInterpretation': 2, 'ExifVersion': '0221', 'Artist': 'MarkM', 'ImageWidth': 950, 'SamplesPerPixel': 3, 'Software': 'Adobe Photoshop CC 2014 (Macintosh)'}
DPI is:
DPI是:
exifObj['XResolution'][0]/exifObj['XResolution'][1]
72DPI in this case.
在这种情况下72DPI。
It's not clear in your example how you are trying to access the DPI value for the context. You're getting an attribute error, so maybe in your template you are trying to access ctx.dpi or something similar which doesn't exist.
在您的示例中,您不清楚如何尝试访问上下文的DPI值。你得到一个属性错误,所以也许在你的模板中你试图访问ctx.dpi或类似的不存在的东西。
#1
1
To begin, I have to ask why you want the DPI resolution. It's just a tag and doesn't really mean anything unless you are outputing to physical media. A 1000x1000 pixel image can be 10x10 at 100dpi or 100x100 at 10dpi, but it's still exactly the same image. Exactly the same pixels. It's hard to imagine scenarios where img.size
doesn't give you everything you need.
首先,我要问你为什么要DPI解决方案。除非您输出到物理媒体,否则它只是一个标记并不具有任何意义。 1000x1000像素的图像在100dpi时可以是10x10,在10dpi时可以是100x100,但它仍然是完全相同的图像。完全相同的像素。很难想象img.size不会为您提供所需的一切。
Having said that, if you want to get the exif tags for resolution try XResolution
from PIL.ExifTags
:
话虽如此,如果你想获得解析的exif标签,请尝试从PIL.ExifTags中获取XResolution:
import Image
from PIL.ExifTags import TAGS
img = Image.open("path/to/.jpg")
info = img._getexif()
exifObj = {}
if info != None:
for tag, value in info.items():
decoded = TAGS.get(tag, tag)
exifObj[decoded] = value
exifObj
now either empty or equals something like:
exifObj现在要么为空,要么等于:
{'YResolution': (720000, 10000), 'BitsPerSample': (8, 8, 8), 'ImageLength': 713, 'Orientation': 1, 'Copyright': 'Mark Meyer Photography', 'ExifImageWidth': 950, 'ExifImageHeight': 713, 'ColorSpace': 1, 'ResolutionUnit': 2, 'DateTime': '2015:01:30 21:37:51', 'XResolution': (720000, 10000), 'ExifOffset': 296, 'PhotometricInterpretation': 2, 'ExifVersion': '0221', 'Artist': 'MarkM', 'ImageWidth': 950, 'SamplesPerPixel': 3, 'Software': 'Adobe Photoshop CC 2014 (Macintosh)'}
DPI is:
DPI是:
exifObj['XResolution'][0]/exifObj['XResolution'][1]
72DPI in this case.
在这种情况下72DPI。
It's not clear in your example how you are trying to access the DPI value for the context. You're getting an attribute error, so maybe in your template you are trying to access ctx.dpi or something similar which doesn't exist.
在您的示例中,您不清楚如何尝试访问上下文的DPI值。你得到一个属性错误,所以也许在你的模板中你试图访问ctx.dpi或类似的不存在的东西。