Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert base64 string to image? [duplicate]

Tags:

python

base64

I'm converting an image to base64 string and sending it from android device to the server. Now, I need to change that string back to an image and save it in the database.

Any help?

like image 747
omarsafwany Avatar asked Apr 25 '13 12:04

omarsafwany


People also ask

How do I convert an image to Base64?

Using following code to convert it into an image file: function base64_to_jpeg( $base64_string, $output_file ) { $ifp = fopen( $output_file, "wb" ); fwrite( $ifp, base64_decode( $base64_string) ); fclose( $ifp ); return( $output_file ); } $image = base64_to_jpeg( $my_base64_string, 'tmp. jpg' );

Does Base64 end in ==?

The equals sign "=" represents a padding, usually seen at the end of a Base64 encoded sequence. The size in bytes is divisible by three (bits divisible by 24): All bits are encoded normally.

What is CG == in Base64?

Cg== is the base64 encode of the new line character in the latest position. So if you want to encode ABC you will get QUJD , however if you include a "return character" after ABC you will get QUJDCg== .

Can you Base64 encode an image?

With elmah. io's free image to Base64 encoder, it's easy to copy and paste markup or style for exactly your codebase. Simply drag and drop, upload, or provide an image URL in the controls above and the encoder will quickly generate a Base64 encoded version of that image.


1 Answers

Try this:

import base64 imgdata = base64.b64decode(imgstring) filename = 'some_image.jpg'  # I assume you have a way of picking unique filenames with open(filename, 'wb') as f:     f.write(imgdata) # f gets closed when you exit the with statement # Now save the value of filename to your database 
like image 50
rmunn Avatar answered Sep 21 '22 00:09

rmunn