-2

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:\Users\manoj\OneDrive\Desktop\8a4c4b2b47cdcbb6d359140081f63478.jpg',r'C:\Users\manoj\OneDrive\Desktop')

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

cv2.error: OpenCV(4.5.5) D:\a\opencv-python\opencv-python\opencv\modules\imgcodecs\src\loadsave.cpp:730: error: (-2:Unspecified error) could not find a writer for the specified extension in function 'cv::imwrite_'

Christoph Rackwitz
  • 11,317
  • 4
  • 27
  • 36
akul07
  • 27
  • 1
  • 7
  • I think, we shouldn't downvote question like this. Thank to this question and answers below, I know this error `could not find a writer for the specified extension in function 'imwrite_'` is because of lack of extension. It's helpful in my case: my fastapi is ok, but it fail when running pytest, because of above error. This is the mistake of pytest, not me! – lam vu Nguyen Aug 31 '23 at 04:31

2 Answers2

3

You are overwriting your default value output.png with C:\Users\manoj\OneDrive\Desktop which does not have a valid file ending. Try

C:\Users\manoj\OneDrive\Desktop\output.png

as output in

im_border(r'C:\Users\manoj\OneDrive\Desktop\8a4c4b2b47cdcbb6d359140081f63478.jpg',r'C:\Users\manoj\OneDrive\Desktop\output.png')
Lukas Weber
  • 455
  • 6
  • 13
1

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.

WangSung
  • 259
  • 2
  • 5