简体   繁体   中英

PermissionError in Images from PIL

I'm making a bing image scraper and saving them to a directory within the project folder, but when it runs the .save() Pillow is returning this error PermissionError: [Errno 13] Permission denied: './scraped_images/'

The following is my code, using Pyhton3.7 and 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)

any help would be greatly appreciated

You're passing the directory for the filename... See the documentation here pillow.readthedocs.io/en/3.1.x/reference/Image.html

Make sure the filenames you have are also valid or you'll also get an error. The name itself should not have any excluded characters and follow standard conventions and end with a valid image extension.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM