OpenCV - specify the format when writing an image to a file (cv2.imwrite)

I want to save an image using opencv imwritewithout any extension. I know that the image format is cv2.imwriteselected based on the extension filename. Is there a way to specify the compression format when calling the function, or will I need to rename the file after it is created?

cv2.imwrite(filename, img)
[Out]: /home/travis/miniconda/conda-bld/work/opencv-3.1.0/modules/imgcodecs/src/loadsave.cpp:459: error: (-2) could not find a writer for the specified extension in function imwrite_
+6
source share
3 answers

I think it is not possible to specify image compression when saving it without extension. I would recommend saving it with an extension and then using os.rename():

import os
import cv2

filename = "image.jpg"
img = ...

cv2.imwrite(filename, img)
os.rename(filename, os.path.splitext(filename)[0])

Hope this helps!

+4
source

, JPEG .JPG PNG .PNG, .

, , :

import cv2

# Load image
im = cv2.imread('image.jpg')

, , im.

:

success, buffer = cv2.imencode(".jpg",im)
buffer.tofile('ExtensionlessFile') 
0

fileName , .

-2

All Articles