How to find base64 file extension in Python

I have a base64 encoded image that I decode and save in ImageField in Django. I want to give the file a random name, but I do not know the file extension.

I have "data: image / png; base64" added to the string and I know I can do some regular expression to extract the mimetype, but I would like to know if there is a best practice way, data: image / png; base64, "to" .png "is reliable. I do not want the handspun function to be interrupted when someone suddenly wants to load a strange image that I do not support.

+7
python mime-types image base64 file-extension
source share
4 answers

It is best to check the contents of the file rather than relying on something external to the file. For example, many email attacks rely on an incorrect mime identifier, so the unsuspecting computer executes a file that it should not. Fortunately, most image file extensions can be determined by looking at the first few bytes (after base64 decoding). However, it is best to use file magic , which can be accessed through python packages such as this or this .

Most image file extensions are obvious from mimetype. For gif, pxc, png, tiff and jpeg, the file extension is all that follows the "image /" part of the mime type. To handle obscure types, python also provides a standard package:

>>> from mimetypes import guess_extension >>> guess_extension('image/x-corelphotopaint') '.cpt' >>> guess_extension('image/png') '.png' 
+4
source share

It looks like the mimetypes module supports data urls even in Python 2:

 >>> from mimetypes import guess_extension, guess_type >>> guess_extension(guess_type("data:image/png;base64,")[0]) '.png' 
+2
source share

You can use the mimetypes module - http://docs.python.org/2/library/mimetypes.html

Basically mimetypes.guess_extension(mine) should do the job.

+1
source share

I have a written code in Lambda that will find the type of image, and also check if base64 is an image or not.

The following code will surely help someone.

 import base64 import imghdr def lambda_handler(event, context): image_data = event['img64'] # crate "json event" in lambda # Sample JSON Event ========> { "img64" : BASE64 of an Image } # Get BASE64 Data of image in image_data variable. sample = base64.b64decode(image_data) # Decode the base64 data and assing to sample. for tf in imghdr.tests: res = tf(sample, None) if res: break; print("Extension OR Type of the Image =====>",res) if(res==None): # if res is None then BASE64 is of not an image. return { 'status': 'False', 'statusCode': 400, 'message': 'It is not image, Only images allowed' } else: return 'It is image' 

Note: - the above code is written in lambda (AWS) in python. You can copy and paste the following code to your local computer and test it as follows.

 import base64 import imghdr image_data = "BASE64 OF AN IMAGE" sample = base64.b64decode(image_data) # Decode the base64 data and assing to sample. for tf in imghdr.tests: res = tf(sample, None) if res: break; print("Extension OR Type of the Image =====>",res) if(res==None): print('It is not image, Only images allowed') else: print('It is image') 
0
source share

All Articles