PIL图片中的PermissionError

时间:2018-11-24 04:06:50

标签: python python-3.x python-imaging-library

我正在制作bing图像抓取器,并将其保存到项目文件夹中的目录中,但是当它运行.save()时,枕头将返回此错误PermissionError: [Errno 13] Permission denied: './scraped_images/'

以下是我的代码,使用Pyhton3.7和Pillow5.3.0

from bs4 import BeautifulSoup
import requests
from PIL import Image
from io import BytesIO


search = input("Search for: ")
params = {"q": search}
r = requests.get("https://www.bing.com/images/search", params=params)

soup = BeautifulSoup(r.text, "html.parser")
links = soup.findAll("a", {"class": "thumb"})

for item in links:
    img_obj = requests.get(item.attrs["href"])
    title = item.attrs["href"].split("/")[-1]
    img = Image.open(BytesIO(img_obj.content))
    img.save("./scraped_images/", img.format)

任何帮助将不胜感激

1 个答案:

答案 0 :(得分:0)

您正在传递文件名的目录...请参阅此处的文档pillow.readthedocs.io/en/3.1.x/reference/Image.html

确保您拥有的文件名也有效,否则也会收到错误消息。该名称本身不应包含任何排除的字符,并且应遵循标准约定并以有效的图像扩展名结尾。