Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting error - 'could not find a writer'while giving imshow, imwrite command opencv

I am a beginner at opencv and python. I have just installed opencv2.4.9 and enthought canopy-32bit. I am getting error for the following:

import cv2
image = cv2.imread('Lena.jpg')
cv2.imwrite('Mypic',image)

This is what I get:

c:\users\nam\appdata\local\temp\tmpokspbt.py in <module>()
      3 
      4 image = cv2.imread('Lena.jpg')
----> 5 cv2.imwrite('Mypic',image)

error: ..\..\..\..\opencv\modules\highgui\src\loadsave.cpp:275: error: (-2) could not find a writer for the specified extension in function cv::imwrite_
like image 367
Matt Avatar asked Dec 04 '14 10:12

Matt


People also ask

What is Imwrite in OpenCV?

OpenCV-Python is a library of Python bindings designed to solve computer vision problems. cv2. imwrite() method is used to save an image to any storage device. This will save the image according to the specified format in current working directory. Syntax: cv2.imwrite(filename, image)

Why open CV is not working?

Your answerThis error may occur if you didn't install opencv module in your system. So first check this module is available or not. If it is not available, then install this module. But before that, try to check numpy module is available or not.

Does cv2 Imwrite overwrite python?

imwrite will overwrite existing files without outputting an error or asking for confirmation. Image of any format can be saved using this method.

What is cv2 Imshow ()?

cv2. imshow() method is used to display an image in a window. The window automatically fits to the image size. Syntax: cv2.imshow(window_name, image)


3 Answers

you need to give an extension to imwrite(), so it knows, how to save(compress) it.

cv2.imwrite('Mypic.png',image)
# jpg,bmp,png,ppm,pgm,tiff supported 'out-of-the-box,
# webp,jp2 depending on if you compiled in the resp. 3rd party support
# no gif or tga.
like image 112
berak Avatar answered Oct 05 '22 21:10

berak


You need to make sure you have the image type within the string you give to the imwrite(). imwrite() dose not have a default method to save, thus it is required within the name you give to it. instead of : cv2.imwrite('Mypic',image) you need to write :

cv2.imwrite('Mypic.The_format_you_want_to_save',image)

As an example:

cv2.imwrite('Mypic.jpg',image)
like image 34
Bananagod Avatar answered Oct 05 '22 22:10

Bananagod


Add an extension for the output file like .jpg, .png, etc based on the application.

For example in this case you could use,

import cv2
image = cv2.imread('Lena.jpg')
cv2.imwrite('Mypic.jpg',image)
like image 24
Archana Nair Avatar answered Oct 05 '22 21:10

Archana Nair