OpenCV - specify format while writing image to file (cv2.imwrite)
I don't understand your motivation for doing this, but if you want to write a JPEG to disk without the .JPG
extension, or a PNG file without the .PNG
extension, you could simply do the encoding to a memory buffer and then write that to disk.
So, if I load an image like this:
import cv2
# Load image
im = cv2.imread('image.jpg')
I should now be in the same position as you, with an image in my variable im
.
I can now encode the image to a memory buffer, and write that memory buffer to disk without extension:
success, buffer = cv2.imencode(".jpg",im)
buffer.tofile('ExtensionlessFile')
I think it's not possible to specify the compression of an image while saving it without extension. I would recommend to save it with extension and then use 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!