Maarten
Maarten

Reputation: 11

Where are images saved when creating images with Pillow in Python

In these lines of code, I create a small red image and save it as "new_image.png". But I can't find the saved image, where is it saved? And can I change the place where I want to save my image?

from PIL import Image
img = Image.new('RGB', (60, 30), color = 'red')
img.save("new_image.PNG")

Upvotes: 1

Views: 1166

Answers (2)

Anthony Aniobi
Anthony Aniobi

Reputation: 327

If you don't specify a path, the image is saved in the same path as the python file generating the image

from PIL import Image
img = Image.new('RGB', (60, 30), color = 'red')
img.save("new_image.PNG")

To save image in a directory you specify, you can use

from PIL import Image
import os

image_path = "path/to/image"
image = image.save(f"{image_path}/image.png")

Note: If this directory does not exist, you would have to create it before saving your image in it.

from PIL import Image
import os

image_path = "path/to/image"

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

Upvotes: 0

Frank
Frank

Reputation: 1249

I tested your code:

from PIL import Image
img = Image.new('RGB', (60, 30), color = 'red')
img.save("new_image.PNG")

It works well for me: enter image description here

The reason may because your current work path is not as you thought. See my answer here: https://stackoverflow.com/a/66449241/12838403

Upvotes: 1

Related Questions