Home > Software engineering >  could not find a writer for the specified extension in function 'cv::imwrite_'
could not find a writer for the specified extension in function 'cv::imwrite_'

Time:01-15

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_'

CodePudding user response:

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')
  •  Tags:  
  • Related