How do i save an image in a specific directory in python?

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?

Phix

8,7304 gold badges33 silver badges59 bronze badges

asked Jul 15, 2015 at 15:11

1

Try this

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

answered Jul 15, 2015 at 15:20

How do i save an image in a specific directory in python?

itzMEonTVitzMEonTV

19.1k3 gold badges38 silver badges48 bronze badges

2

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

answered Sep 21, 2021 at 1:17

How do i save an image in a specific directory in python?

In this Python tutorial, we will learn how to save an image to file in python and also we will cover these topics:

  • How to save an image in python
  • Python show image
  • Python save the image OpenCV2
  • Python save an image to file from URL
  • Read jpg from window clipboard in python
  • Convert string in base64 to image and save in file python
  • Python save the image file to a folder
  • Python write an image to file
  • Python save the image to file open cv

How to save an image using a pillow in python

Here, we can see how to save an image in python.

  • In this example, I have imported a module called Image from PIL and declared a variable picture, and assigned Image.open(r’Downloads\3.jpg’) the path and the name of the image along with extension.
  • And declared another variable and assigned picture.save(“dolls.jpg”) . Here, doll.jpg is the new name of the image.

Example:

from PIL import Image  
import PIL  
picture = Image.open(r'Downloads\3.jpg')  
picture = picture.save("dolls.jpg") 

In the below screenshot you can the image is saved as dolls.jpg.

How do i save an image in a specific directory in python?
How to save an image using a pillow in python

Here, we can see how to show image in python.

  • In this example, I have imported a module called Image from PIL module and to open the image.
  • I have used image = Image.open(‘newpic.jpg’) to open the image. Image.open along with the name of the image and the extension.
  • The image.show() is used to show the image in python.

Example:

from PIL import Image
image = Image.open('newpic.jpg')
image.show()

How do i save an image in a specific directory in python?
Python show image

Python save the file with OpenCV2

Here, we can see how to save the file with opencv2 in python.

  • In this example, I have imported a module called cv2 and os and taken a variable as a path and assigned a path and taken a directory as another variable, and assigned the path of the directory.
  • The imread is used to specify the way in which theimage should be read, the os.chdir(directory) method to change current directories to a given path.
  • The image dora.jpg from the path is copied to the directory by changing the name dora.jpg to cat.jpg and listing the file name from the directory after save the cat.jpg file.
  • The os.listdir is used to list the directories, the directories will be listed before and after saving the image.

Example:

import cv2 
import os 
path = r'C:\Users\Administrator.SHAREPOINTSKY\Downloads\dora.jpg'
directory = r'C:\Users\Administrator.SHAREPOINTSKY\Desktop\Work'
img = cv2.imread(path) 
os.chdir(directory) 
print("Before saving")   
print(os.listdir(directory))   
filename = 'cat.jpg'
cv2.imwrite(filename, img) 
print("After saving")  
print(os.listdir(directory))   

Here, we can the list of directories before and after saving as the output. You can refer to the below screenshot for the output:

How do i save an image in a specific directory in python?
Python save the file with OpenCV2

Python save an image to file from URL

Here, we can see how to save an image to file from URL in python.

  • In this example, I have imported a module called urllib.request. The urllib.request module defines functions and classes that help to open the URL.
  • I have imported the Image module from PIL, the urlretrieve method of the module used for retrieving the files, and the URL is assigned “https://bit.ly/3oAeohK”.
  • We can shorten the length of the URL by using URL Shortner tools.
  • The PIL.Image.open(“new.png”) is used to open the image, new.png is the name of the file.
  • The image.show() is used to display the image from the file.

Example:

import urllib.request
from PIL import Image  
import PIL
print(urllib.request.urlretrieve("https://bit.ly/3oAeohK"))
image = PIL.Image.open("new.png")
image.show()

The URL is saved in the image format as the output in the below screenshot.

How do i save an image in a specific directory in python?
Python save an image to file from URL

Read jpg from window clipboard in python

Here, we can see how to read jpg from window clipboard in python.

  • In this example, I have imported a module called win32clipboard. The .OpenClipboard() is used to open the clipboard and prevent other applications to modify the content.
  • When the application calls GetClipboardData() function the system performs implicit data format conversion between clipboard formats and copies the data.
  • To read the jpg file, I have opened the file as “savedImage.jpg” along with an extension, the ‘w’ mode is used to write the file and used f.write to write the content of the file.
  • The CloseClipboard() is used to close the clipboard so that other windows can access the clipboard.

Install Python Pyperclip module

To install this module we have to use

pip install pyperclip

Example:

import win32clipboard
win32clipboard.OpenClipboard()
image = win32clipboard.GetClipboardData()
with open("savedImage.jpg", 'w') as f:
    print(f.write(image))
win32clipboard.CloseClipboard()

Below screenshot shows the output.

How do i save an image in a specific directory in python?
Read jpg from window clipboard in python

Convert string in base64 to image and save in file python

Here, we can how to convert string in base64 to image and save in file python.

  • In this example, I have imported a module called base64. The base64 is used to decode and encode also to convert the strings to byte format.
  • To open the file I have used with open(“book.png”, “wb”) as f , and to save and to write the file I have used print(f.write(base64.b64decode(‘book’))).
  • To decode the string to image base64.b64decode.

Example:

import base64
with open("book.png", "wb") as f:
  print(f.write(base64.b64decode('book')))

We can see the created file as the output. In the below screenshot.

How do i save an image in a specific directory in python?
Convert string in base64 to image and save in file python

How do i save an image in a specific directory in python?
Convert string in base64 to image and save in file python

Python save the image file to a folder

Here, we can see how to save image file to folder in python.

  • In this example, I have imported a module called cv2 and os and declared a variable as image and assigned image = cv2.imread(‘doll.jpg’). The doll.jpg is the name of the file.
  • The imread is used to load the image from the specified file, and the path of the folder is declared to save the image file to the folder.
  • To join the paths into a single path, I have used os.path.join.

Example:

import cv2
import os
image = cv2.imread('doll.jpg')
path = r'C:\Users\Administrator.SHAREPOINTSKY\Desktop\Newfolder'
(cv2.imwrite(os.path.join(path,'doll.jpg'), image))

In the below screenshot we can see that the image file is saved to the path that, we have specified.

How do i save an image in a specific directory in python?
Python save image file to folder

Python write an image to file

Now, we can see how to write an image to file in python

  • In this example, I have imported a module called Image from PIL and opened the dolls.jpg image file to read and the file which is read is read.
  • To write the image I have used opened the file named cartoon.png. To write the dolls.jpg image file into a new file called cartoon.png I have used file.write(). To close the file I have used file.close().

Example:

from PIL import Image
file = open('dolls.jpg','rb')
data = file.read()
file.close()
file = open('cartoon.png','wb')
file.write(data)
file.close()

We can see that image is written to another file as the output. You can refer to the below screenshot for the output.

How do i save an image in a specific directory in python?
Python write an image to file

Python save the image to file open cv

Now, we can see how to save the image to file open cv in python

  • In this example I have imported module cv2 as cv and sys , and declared a variable as image and assigned cv.imread(cv.samples.findFile(“doll.jpg”)). The doll.jpg is the name of the file.
  • Here, I have taken if condition is none to check whether if an image is present or not. If the image is not present it returns “No image found.”
  • If the image is present it displays the image by using cv.imshow().
  • The ord() accepts the string as an argument and returns Unicode which is equal to the passed argument.
  • After returning the string(“s”) the imagefile is showed by using cv.imwrite(“doll.jpg”, image).

Example:

import cv2 as cv
import sys
image = cv.imread(cv.samples.findFile("doll.jpg"))
if image is None:
    sys.exit("No image found.")
cv.imshow("showimage", image)
file = cv.waitKey(0)
if file == ord("s"):
    cv.imwrite("doll.jpg", image)

Below screenshot shows the image file as the output.

How do i save an image in a specific directory in python?
Python save the image to file open cv

You may like the following Python tutorials:

  • How to Create Date Time Picker using Python Tkinter
  • Python Pygame tutorial
  • Machine Learning using Python
  • How to go to next page in Python Tkinter Program
  • How to read a text file using Python Tkinter
  • Python get all files in directory
  • How to Take User Input and Store in Variable using Python Tkinter
  • Python catch multiple exceptions
  • Python Exceptions Handling

In this tutorial, we have learned about how to save an image to file in python, and also we have covered these topics:

  • How to save an image in python
  • Python show image
  • Python save the image OpenCV2
  • Python save an image to file from URL
  • Read jpg from window clipboard in python
  • Convert string in base64 to image and save in file python
  • Python save the image file to a folder
  • Python write an image to file
  • Python save the image to file open cv

How do i save an image in a specific directory in python?

Python is one of the most popular languages in the United States of America. I have been working with Python for a long time and I have expertise in working with various libraries on Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… I have experience in working with various clients in countries like United States, Canada, United Kingdom, Australia, New Zealand, etc. Check out my profile.

How do I save an image to a specific path in Python?

Here, we can see how to save an image in python..
In this example, I have imported a module called Image from PIL and declared a variable picture, and assigned Image. open(r'Downloads\3. jpg') the path and the name of the image along with extension..
And declared another variable and assigned picture. save(“dolls. jpg”) ..

How do I save a file in a specific directory in Python?

How to write a file to a specific directory in Python.
save_path = '/home'.
file_name = "test.txt".
completeName = os. path. join(save_path, file_name).
print(completeName).
file1 = open(completeName, "w").
file1. write("file information").
file1. close().

How do I loop an image in a directory in Python?

In this article, we will learn how to iterate through images in a folder in Python..
Method 1: Using os. listdir..
Method 2: Using pathlib module..
Method 3: Using glob.iglob().