How to get image information in Python

Cloudmersive
2 min readMay 5, 2020

--

When you are done with this tutorial lesson, your app will have the ability to extract a nice range of information from image files. These images can be in any of over 100 different formats. All this can be accomplished in mere minutes by using an API.

Installation of our client can be carried out with ease by using pip install.

pip install cloudmersive-convert-api-client

For our task, we need to use convert_image_get_image_info. This first requires activating an API instance, through which we may call said function.

from __future__ import print_functionimport timeimport cloudmersive_convert_api_clientfrom cloudmersive_convert_api_client.rest import ApiExceptionfrom pprint import pprint# Configure API key authorization: Apikeyconfiguration = cloudmersive_convert_api_client.Configuration()configuration.api_key['Apikey'] = 'YOUR_API_KEY'# Uncomment below to setup prefix (e.g. Bearer) for API key, if needed# configuration.api_key_prefix['Apikey'] = 'Bearer'# create an instance of the API classapi_instance = cloudmersive_convert_api_client.ConvertImageApi(cloudmersive_convert_api_client.ApiClient(configuration))input_file = '/path/to/file' # file | Input file to perform the operation on.try:# Get information about an imageapi_response = api_instance.convert_image_get_image_info(input_file)pprint(api_response)except ApiException as e:print("Exception when calling ConvertImageApi->convert_image_get_image_info: %s\n" % e)

And now send in your request to the server with your file path and you will get your response a few seconds later. This response will contain all the information about your image that you could possibly want, including format, EXIF data, compression, DPI and more. Here are the response fields so you can take a look at the full list:

{
"Successful": true,
"ColorSpace": "string",
"ColorType": "string",
"Width": 0,
"Height": 0,
"CompressionLevel": 0,
"ImageHashSignature": "string",
"HasTransparency": true,
"MimeType": "string",
"ImageFormat": "string",
"DPIUnit": "string",
"DPI": 0,
"ColorCount": 0,
"BitDepth": 0,
"Comment": "string",
"ExifProfileName": "string",
"ExifValues": [
{
"Tag": "string",
"DataType": "string",
"DataValue": "string"
}
]
}

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.

No responses yet