Troll_Hunter
Troll_Hunter

Reputation: 515

How do I use Python PIL to save an image to a particular directory?

For example I want to manipulate some image and then save it to a particular directory. How do I save to a specific directory other than the one I am in? I understand that this will save to the directory I am in:

from PIL import Image
""" Some Code """
img.save("sample.png", "")

How do I save to a different directory?

Upvotes: 21

Views: 135797

Answers (3)

Code for saving image to a specific folder

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'))

Upvotes: 0

Anthony Aniobi
Anthony Aniobi

Reputation: 327

First create the directory before saving the image to the directory

from PIL import Image
import os

image_path = "path/to/image"

os.mkdir(image_path)
image = image.save(f"{image_path}/image.png")

Upvotes: 13

itzMEonTV
itzMEonTV

Reputation: 20349

Try this

img.save('/absolute/path/to/myphoto.jpg', 'JPEG')

Upvotes: 37

Related Questions