举例来说,我想操作一些图像并将其保存到特定目录。如何保存到除当前目录以外的特定目录?我知道以下代码会保存到当前目录:
from PIL import Image
""" Some Code """
img.save("sample.png", "")
我该如何保存到不同的目录?
试试这个
img.save('/absolute/path/to/myphoto.jpg', 'JPEG')
img
是什么。楼主导入了Image
,但似乎没有使用它或者只是没有透露出来。哪些对象可以调用.save()
函数? - Bowen Liuimg
似乎是 Image
类的一个实例,该类具有 .save()
函数。请参阅 https://pillow.readthedocs.io/en/stable/reference/Image.html。 - Faraz M.在保存图像到目录之前,首先创建目录。
from PIL import Image
import os
image_path = "path/to/image"
os.mkdir(image_path)
image = image.save(f"{image_path}/image.png")
将图像保存到特定文件夹的代码
import urllib.request
from PIL import Image
from pathlib import Path
import os
url="https://bimber.bringthepixel.com/main/wp-content/uploads/sites/17/2018/07/neon_type2_v02.jpg"
urllib.request.urlretrieve(url, os.path.join('E:/new projects of python/picture download','1.jpg'))