could not find a writer for the specified extension in function 'cv::imwrite_'

Question:

I am facing error while defining a function to add border in an image

import cv2

def im_border(path: str, output: str = "output.png"):
              im1 = cv2.imread(path)
              border = cv2.copyMakeBorder(
                            im1, 20, 20, 20, 20, cv2.BORDER_CONSTANT, value = [128, 128, 128])
              cv2.imwrite(output, border)

              return None 
              
im_border(r'C:UsersmanojOneDriveDesktop8a4c4b2b47cdcbb6d359140081f63478.jpg',r'C:UsersmanojOneDriveDesktop')

Why this is giving an error
The error is as follows –

cv2.error: OpenCV(4.5.5) D:aopencv-pythonopencv-pythonopencvmodulesimgcodecssrcloadsave.cpp:730: error: (-2:Unspecified error) could not find a writer for the specified extension in function ‘cv::imwrite_’

Asked By: akul07

||

Answers:

You are overwriting your default value output.png with C:UsersmanojOneDriveDesktop which does not have a valid file ending. Try

C:UsersmanojOneDriveDesktopoutput.png

as output in

im_border(r'C:UsersmanojOneDriveDesktop8a4c4b2b47cdcbb6d359140081f63478.jpg',r'C:UsersmanojOneDriveDesktopoutput.png')
Answered By: Lukas Weber

You should provide the correct extension to .imwrite(,).
The filename output must contain a path with the correct extension. (ex : .jpg, .png)

Please refer to the previous question.

Answered By: WangSung
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.