【发布时间】:2012-03-25 09:44:51
【问题描述】:
我有一个充满 html 文件的目录,每个文件中都有一个牛皮癣患者的临床图像。我想打开每个文件,找到图片,然后保存在同一个目录中。
import os, os.path
import Image
from BeautifulSoup import BeautifulSoup as bs
path = 'C:\Users\gokalraina\Desktop\derm images'
for root, dirs, files in path:
for f in files:
soup = bs(f)
for image in soup.findAll("img"):
print "Image: %(src)s" % image
im = Image.open(image)
im.save(path+image["src"], "JPEG")
我收到此错误:
Traceback (most recent call last):
File "C:\Users\gokalraina\Desktop\modfile.py", line 7, in <module>
for root, dirs, files in path:
ValueError: need more than 1 value to unpack
即使在谷歌上搜索错误之后,我也不知道出了什么问题,或者我是否正确地执行了此操作。请记住,我是 python 新手。
编辑:对程序进行建议的更改后,我仍然收到错误:
Traceback (most recent call last):
File "C:\Users\gokalraina\Desktop\modfile.py", line 25, in <module>
im = Image.open(image)
File "C:\Python27\lib\site-packages\PIL\Image.py", line 1956, in open
prefix = fp.read(16)
TypeError: 'NoneType' object is not callable
这是修改后的代码(感谢 nightcracker)
import os, os.path
import Image
from BeautifulSoup import BeautifulSoup as bs
path = 'C:\Users\gokalraina\Desktop\derm images'
for root, dirs, files in os.walk(path):
for f in files:
soup = bs(open(os.path.join(root, f)).read())
for image in soup.findAll("img"):
print "Image: %(src)s" % image
im = Image.open(image)
im.save(path+image["src"], "JPEG")
【问题讨论】:
-
modfile.py是您发布的来源吗?第 7 行似乎是一个空行,所以我猜不是。您需要将modfile.py添加到您的帖子中。 -
是的,modfile.py 是贴出的代码。
标签: python image jpeg beautifulsoup