How to convert a multiple document pages into PNG Thumbnails in Python

Cloudmersive
2 min readJun 13, 2020

--

Thumbnails are generally pretty great, adding a lot to your user’s experience. However, their automated creation can come at a cost in development time if you are not careful. The process involves, splitting your document into individual pages, rendering those pages over a background, creating PNGs from those renders, and finally resizing all of those. Nobody’s got time for all that. So let’s look at an API-fueled alternative solution instead.

Install this Cloudmersive API client as your first step.

pip install cloudmersive-convert-api-client

Then proceed to calling our thumbnail function, which can be customized with image dimensions and number of pages.

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.ConvertDocumentApi(cloudmersive_convert_api_client.ApiClient(configuration))input_file = '/path/to/file' # file | Input file to perform the operation on.pages = 56 # int | Optional; Specify how many pages of the document will be converted to thumbnails. Default is 1 page. (optional)max_width = 56 # int | Optional; Maximum width of the output thumbnail - final image will be as large as possible while less than or equal to this width. Default is 128. (optional)max_height = 56 # int | Optional; Maximum height of the output thumbnail - final image will be as large as possible while less than or equal to this width. Default is 128. (optional)extension = 'extension_example' # str | Optional; Specify the file extension of the inputFile. This will improve the response time in most cases. Also allows unsupported files without extensions to still return a corresponding generic icon. (optional)try:# Convert File to Thumbnail Image Objectapi_response = api_instance.convert_document_autodetect_to_thumbnails_advanced(input_file, pages=pages, max_width=max_width, max_height=max_height, extension=extension)pprint(api_response)except ApiException as e:print("Exception when calling ConvertDocumentApi->convert_document_autodetect_to_thumbnails_advanced: %s\n" % e)

And that’s it! Best shortcut ever.

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

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

No responses yet