Save an image in OpenCV without using "imwrite" function

Is there any other way to save an image in opencv with python without “imwrite” function?

where is the problem with it ? please explain

1 Like

I am new to opencv. I wanted to know if there is any method to save an image at a location without using “imwrite” function directly

why dont you want to use it ?

1 Like

Bro, instead of asking me these many questions, you could have given the answer

i’m not yor bro, and you just keep repeating yourself

what you are asking is unusual and frankly pointless. OpenCV has a function to write pictures. use it. if you don’t want to use it, you should explain why you want something else.

if you don’t understand why that is, the fault lies with you.

As I know OpenCV has only imwrite()

cv2.imwrite('output-imwrite.jpg', img)

You can also use imdecode() with standard write() but it will do the same as imwrite()

result, data = cv2.imencode('.jpg', img)

fh = open('output-imencode.jpg', 'wb')

For other method you have to install external module.

You can write it with imageio - but remeber to conver image from BGR(A) to RGB(A)

import imageio

img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

imageio.imwrite('output-imageio.jpg', img)

You can also conver to PILLOW and write it. It also need to convert BGR(A) to RGB(A)

from PIL import Image

img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

img = Image.fromarray(img)'output-pillow.jpg')

But if you have some problem with original imwrite() then (as said crackwitz) better describe your problem because you may have the same problem with other modules - and you may have XY Problem.

1 Like

Thanks for the information. I have one more query, I saw people in the internet calling the output of imencode function as buffer. I know that buffer is a memory allocated for something. But here from imencode function, we are getting stream of bytes. So how is it related to buffer?

Reference link : python - Interpreting cv2.imencode result - Stack Overflow