简体   繁体   中英

cv2.imwrite not saving any image

I am trying to run a simple example code to write an image using opencv on python3. Code reference:1

import cv2
import os

image_path = r'C:\Users\840g1 touch\Desktop\B2.jpg'

directory = r'C:\Users\840g1 touch\Desktop'

img = cv2.imread(image_path)

os.chdir(directory)

print("Before saving image:")
print(os.listdir(directory))

# Filename
filename = 'savedImage.jpg'

cv2.imwrite(filename, img)

print("After saving image:")
print(os.listdir(directory))

print('Successfully saved')

Image is displaying and everything but the image is not getting saved anywhere. I am using Anaconda on windows. Not sure if the problem is related to the code or my PC.

Any help is much appreciated!

You did not provide a path for imwrite so it writes in your pythons current working directory .

change the line:

cv2.imwrite(filename, img)

to something like:

cv2.imwrite(os.path.join(directory,filename), img)

note: you can get your current working dir with

os.getcwd()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM