Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 831
Troll_Hunter Avatar asked Jul 15 '15 15:07

Troll_Hunter


People also ask

How do I save a picture from a PIL?

Saving an image can be achieved by using . save() method with PIL library's Image module in Python.

How do I save a generated image in Python?

The PIL module is used for storing, processing, and displaying images in Python. To save images, we can use the PIL. save() function. This function is used to export an image to an external file.


2 Answers

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")
like image 68
Anthony Aniobi Avatar answered Oct 18 '22 18:10

Anthony Aniobi


Try this

img.save('/absolute/path/to/myphoto.jpg', 'JPEG')
like image 45
itzMEonTV Avatar answered Oct 18 '22 18:10

itzMEonTV