问题描述
我正在尝试从下载的图像中读取Exif数据.我通过一个功能将图像保存到计算机,然后尝试通过另一个功能读取数据,但是我不断收到错误的模式错误.我已经能够从预先保存的图像中读取数据,并且只使用了._getexif(),但是当我尝试对图像进行相同的操作时,我无法下载该图像.我在做什么错了?
I am trying to read the Exif data from a downloaded image. I save the image to my computer in one function then try to read the data in another function, but I keep getting a bad mode error. I have been able to read the data from a pre saved image and just used ._getexif() but when I try to do the same thing with the image I download it does not work. What am I doing wrong?
这是调用这两个函数的循环.
Here is the loop that calls the two functions.
else:
imgTags = findImages(url)
for imgTag in imgTags:
imgFileName = downloadImage(imgTag)
testForExif(imgFileName)
从页面查找并下载图像
def findImages(url):
print '[*] Finding images on ' + url
urlContent = urllib2.urlopen(url).read()
soup = BeautifulSoup(urlContent)
imgTags = soup.findAll('img')
return imgTags
def downloadImage(imgTag):
try:
print '[+] Downloading image...'
imgSrc = imgTag['src']
imgContent = urllib2.urlopen(imgSrc).read()
imgFileName = basename(urlsplit(imgSrc)[2])
imgFile = open(imgFileName, 'wb')
imgFile.write(imgContent)
imgFile.close()
return imgFileName
except:
return ''
读取exif数据
def testForExif(imgFileName):
exifData = {}
imgFile = Image.open(imgFileName, 'rb')
info = imgFile._getexif()
print '\n\n' + str(info) + '\n\n'
if info:
for (tag, value) in info.items():
decoded = TAGS.get(tag, tag)
exifData[decoded] = value
exifGPS = exifData['GPSInfo']
if exifGPS:
print '[+] ' + imgFileName + ' contains GPS MetaData'
我相信在前两行中的testsForExif函数中会触发错误模式"错误.它永远不会到达第一个打印语句.
I believe the 'bad mode' error is triggered in teh testsForExif function, somewhere in the first couple of lines. It never makes it to the first print statement.
我得到的确切错误是.
回溯(最近通话最近):文件"C:\ Users \ HeyNow \ Downloads \ Python \ Cookbook \ Forensics \ metaurl.py"行59英寸main()文件"C:\ Users \ HeyNow \ Downloads \ Python \ Cookbook \ Forensics \ metaurl.py",行56,主要testForExif(imgFileName)文件"C:\ Users \ HeyNow \ Downloads \ Python \ Cookbook \ Forensics \ metaurl.py",行31,在testForExif中imgFile = Image.open(imgFileName,'rb')文件"C:\ Python27 \ lib \ site-packages \ PIL \ Image.py",行1947,打开引发ValueError(坏模式")ValueError:坏模式
Traceback (most recent call last): File "C:\Users\HeyNow\Downloads\Python\Cookbook\Forensics\metaurl.py", line 59 , in main() File "C:\Users\HeyNow\Downloads\Python\Cookbook\Forensics\metaurl.py", line 56 , in main testForExif(imgFileName) File "C:\Users\HeyNow\Downloads\Python\Cookbook\Forensics\metaurl.py", line 31 , in testForExif imgFile = Image.open(imgFileName, 'rb') File "C:\Python27\lib\site-packages\PIL\Image.py", line 1947, in open raise ValueError("bad mode") ValueError: bad mode
玩弄它我也得到了一个.
From playing around with it I have also gotten a.
错误.
我迷路了.
如果我更改:
imgFile = Image.open(imgFileName, 'rb')
到
imgFile = Image.open(imgFileName)
我得到 AttributeError:_getexif()
追溯:
推荐答案
我认为由于"downloadImages"的工作方式而出现错误.如果发生任何类型的错误,您只是返回一个空字符串.但是,如果您在尝试打开文件名之前没有检查文件名是否不是空字符串,请返回testforex.
I think you are getting errors because of the way "downloadImages" works. You are just returning an empty string in the event that there are any sort of errors. However, back in your testforexif you don't check to see if the filename is not an empty string before attempting to open it.
Image.open('')
将导致imgfile为None.因此,它没有属性,并且会出现属性错误.
will cause imgfile to be None. So then it has no attributes and you get the attribute error.
您在解析网页或处理文件名时可能会出现一些错误,这会引发错误.无法正确处理错误是非常糟糕的形式,在这种情况下,这将导致您的程序完全无法运行.您具有try/except语句,但是即使有错误,也可以继续操作.您需要做的是更改except子句以在出现错误(或为空)时跳过该文件名.希望有帮助.
There is probably some error in your parsing of the webpage or processing of the filenames that is throwing errors. It is very bad form to not properly handle errors and in this case it is causing your program not to function at all. You have the try/except statement but you simply proceed even if there are errors. What you need to do is change the except clause to skip that filename if there are errors (or it is null). Hope that helps.
尝试随便打印变量(例如文件名),以确保它们正确无误并存在图像.这也可能是文件类型的问题.例如,也许您的脚本正在寻找一些非jpg图像文件,并试图在bmp上打开exif数据或您拥有什么.
Try printing your variables as you go (such as the filenames) just to make sure that they are correct and the images exist. It may also be an issue of filetypes. For example, perhaps your script is finding some non-jpg image files and is trying to open the exif data on a bmp or what have you.
这篇关于如何使用Python PIL下载图像并提取Exif数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!