Ask Your Question

what exactly does the imencode do, wanted to use it for image upload onto server? [closed]

asked 2018-04-03 02:15:43 -0600

Santhosh1 gravatar image

updated 2018-04-11 06:08:48 -0600

I am converting an image using imencode. I'm currently using opencv 3.4 couldn't find the official documentation for 3.4 but did find one for 3.0. Reading and Writing Images

It's given here that the output is retval and buf its said that The function compresses the image and stores it in the memory buffer that is resized to fit the result.

What is the type of compression/encoding that is followed here in OpenCV?

ex: cv2.imencode(".jpg",image)

I can just transfer this buf to a server through an API, which I know will be in compressed format.

How will I decode the received buf without knowing how it was encoded?

edit retag flag offensive reopen merge delete

Closed for the following reason question is off-topic or not relevant by Santhosh1
close date 2018-04-03 08:27:51.780770


what if you lookup cv2.imdecode() ?

it also should be : cv2.imencode(".jpg",image) (extension there is a string)

berak gravatar imageberak ( 2018-04-03 02:28:22 -0600 )edit

Can't find any info about cv2.imdecode also, only The function reads an image from the specified buffer in the memory no explanation of how it is actually carried out

Santhosh1 gravatar imageSanthosh1 ( 2018-04-03 02:36:54 -0600 )edit

imencode and file signatures and it is not an opencv problem

LBerger gravatar imageLBerger ( 2018-04-03 03:19:40 -0600 )edit

@LBerger Can you explain? I don't get you. Please share your thoughts and possible solution.

Santhosh1 gravatar imageSanthosh1 ( 2018-04-03 04:41:30 -0600 )edit

@Santosh1, solutions for which problem ?

imencode() will choose a codec based on the extension string

imdecode() will look at the buffer's file magic, to choose a decoder

berak gravatar imageberak ( 2018-04-03 04:43:55 -0600 )edit

@berak I have an image file with dimension4160 x 2340, when I do cv2.imencode(".jpg",image). I have an numpy.ndarray of shape 2756259 , 1. So based on your explanation a jpg codec is used to compress original image and stored in ndarray. I want to save this compressed .jpg file that is in form of ndarray at particular location on cloud?

Santhosh1 gravatar imageSanthosh1 ( 2018-04-03 05:03:31 -0600 )edit

yes, and ? (where is the problem ?)

berak gravatar imageberak ( 2018-04-03 05:04:49 -0600 )edit

In VM I could have just used cv2.imwrite to save the file used custom API to upload the image file on to the cloud. I'm not using VM I'm using cloud function, which is serverless no VM. I need to store this compressed .jpg on cloud. How do I do it? With just an .jpg compressed variable I currently have? I'm confused

Santhosh1 gravatar imageSanthosh1 ( 2018-04-03 05:11:04 -0600 )edit

if you want to store it somewhere, you obvioously need some datastore, but all of that is entirely off-topic here.

berak gravatar imageberak ( 2018-04-03 05:22:16 -0600 )edit

@berak Yes you are correct, its a database thing not related to the topic. I'm not asking about that. I just shared it to for you to know my final goal.I would like to know till where can OpenCV help me reach there?

Santhosh1 gravatar imageSanthosh1 ( 2018-04-03 05:29:32 -0600 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2018-04-03 08:27:21 -0600

Santhosh1 gravatar image

updated 2018-04-11 06:12:04 -0600

No matter, how the imencode does it's part. It will always require imdecode to extract the image back.

This end result seems to be off-topic

For my case I needed to save the processed image so wanted to know how the image was encoded in imencode but I actually didn't find a specific answer.

This could be a work around to my problem, I'll be trying it after this post. May be will be helpful for anyone with similar problem.


Found a simple solution, use a Tempfile to store the image using cv2.imwrite,then upload this Tempfile

Here's the code

with NamedTemporaryFile() as temp:
            # Extract name to the temp file
            iName = "".join([str(, ".jpg"])
            # save image to temp file
            cv2.imwrite(iName, duplicate_image)
edit flag offensive delete link more

Question Tools

1 follower


Asked: 2018-04-03 02:15:43 -0600

Seen: 14,616 times

Last updated: Apr 11 '18