Convert between various image formats in Python using the Pdfcrowd API v2. The API is easy to use and the integration takes only a couple of lines of code.
pip install pdfcrowd
Learn more about other install options.
Authentication is needed in order to use the Pdfcrowd API. The credentials
used for accessing the API are your Pdfcrowd username and the API key.
Sign up for a Free Trial
import pdfcrowd import sys try: # create the API client instance client = pdfcrowd.ImageToImageClient('your_username', 'your_apikey') # configure the conversion client.setOutputFormat('jpg') # run the conversion and write the result to a file client.convertFileToFile('/path/to/logo.png', 'logo.jpg') except pdfcrowd.Error as why: # report the error sys.stderr.write('Pdfcrowd Error: {}\n'.format(why)) # handle the exception here or rethrow and handle it at a higher level raise
import pdfcrowd import sys try: # create the API client instance client = pdfcrowd.ImageToImageClient('your_username', 'your_apikey') # configure the conversion client.setOutputFormat('jpg') # create output file for conversion result output_file = open('logo.jpg', 'wb') # run the conversion and store the result into an image variable image = client.convertFile('/path/to/logo.png') # write the image into the output file output_file.write(image) # close the output file output_file.close() except pdfcrowd.Error as why: # report the error sys.stderr.write('Pdfcrowd Error: {}\n'.format(why)) # handle the exception here or rethrow and handle it at a higher level raise
import pdfcrowd import sys try: # create the API client instance client = pdfcrowd.ImageToImageClient('your_username', 'your_apikey') # configure the conversion client.setOutputFormat('jpg') # create output stream for conversion result output_stream = open('logo.jpg', 'wb') # run the conversion and write the result into the output stream client.convertFileToStream('/path/to/logo.png', output_stream) # close the output stream output_stream.close() except pdfcrowd.Error as why: # report the error sys.stderr.write('Pdfcrowd Error: {}\n'.format(why)) # handle the exception here or rethrow and handle it at a higher level raise
import pdfcrowd import sys try: # create the API client instance client = pdfcrowd.ImageToImageClient('your_username', 'your_apikey') # configure the conversion client.setOutputFormat('jpg') # run the conversion and write the result to a file client.convertUrlToFile('http://pdfcrowd.com/static/images/logo.png', 'logo.jpg') except pdfcrowd.Error as why: # report the error sys.stderr.write('Pdfcrowd Error: {}\n'.format(why)) # handle the exception here or rethrow and handle it at a higher level raise
import pdfcrowd import sys try: # create the API client instance client = pdfcrowd.ImageToImageClient('your_username', 'your_apikey') # configure the conversion client.setOutputFormat('jpg') # create output file for conversion result output_file = open('logo.jpg', 'wb') # run the conversion and store the result into an image variable image = client.convertUrl('http://pdfcrowd.com/static/images/logo.png') # write the image into the output file output_file.write(image) # close the output file output_file.close() except pdfcrowd.Error as why: # report the error sys.stderr.write('Pdfcrowd Error: {}\n'.format(why)) # handle the exception here or rethrow and handle it at a higher level raise
import pdfcrowd import sys try: # create the API client instance client = pdfcrowd.ImageToImageClient('your_username', 'your_apikey') # configure the conversion client.setOutputFormat('jpg') # create output stream for conversion result output_stream = open('logo.jpg', 'wb') # run the conversion and write the result into the output stream client.convertUrlToStream('http://pdfcrowd.com/static/images/logo.png', output_stream) # close the output stream output_stream.close() except pdfcrowd.Error as why: # report the error sys.stderr.write('Pdfcrowd Error: {}\n'.format(why)) # handle the exception here or rethrow and handle it at a higher level raise
import pdfcrowd import sys try: # create the API client instance client = pdfcrowd.ImageToImageClient('your_username', 'your_apikey') # configure the conversion client.setOutputFormat('jpg') # run the conversion and write the result to a file client.convertRawDataToFile(open('/path/to/logo.png', 'rb').read(), 'logo.jpg') except pdfcrowd.Error as why: # report the error sys.stderr.write('Pdfcrowd Error: {}\n'.format(why)) # handle the exception here or rethrow and handle it at a higher level raise
import pdfcrowd import sys try: # create the API client instance client = pdfcrowd.ImageToImageClient('your_username', 'your_apikey') # configure the conversion client.setOutputFormat('jpg') # create output file for conversion result output_file = open('logo.jpg', 'wb') # run the conversion and store the result into an image variable image = client.convertRawData(open('/path/to/logo.png', 'rb').read()) # write the image into the output file output_file.write(image) # close the output file output_file.close() except pdfcrowd.Error as why: # report the error sys.stderr.write('Pdfcrowd Error: {}\n'.format(why)) # handle the exception here or rethrow and handle it at a higher level raise
import pdfcrowd import sys try: # create the API client instance client = pdfcrowd.ImageToImageClient('your_username', 'your_apikey') # configure the conversion client.setOutputFormat('jpg') # create output stream for conversion result output_stream = open('logo.jpg', 'wb') # run the conversion and write the result into the output stream client.convertRawDataToStream(open('/path/to/logo.png', 'rb').read(), output_stream) # close the output stream output_stream.close() except pdfcrowd.Error as why: # report the error sys.stderr.write('Pdfcrowd Error: {}\n'.format(why)) # handle the exception here or rethrow and handle it at a higher level raise
import pdfcrowd import sys try: # create the API client instance client = pdfcrowd.ImageToImageClient('your_username', 'your_apikey') # configure the conversion client.setOutputFormat('jpg') client.setDebugLog(True) # run the conversion and write the result to a file client.convertFileToFile('/path/to/logo.png', 'logo.jpg') # print URL to the debug log print('Debug log url: {}'.format(client.getDebugLogUrl())) # print the number of available conversion credits in your account print('Remaining credit count: {}'.format(client.getRemainingCreditCount())) # print the number of credits consumed by the conversion print('Consumed credit count: {}'.format(client.getConsumedCreditCount())) # print the unique ID of the conversion print('Job id: {}'.format(client.getJobId())) # print the size of the output in bytes print('Output size: {}'.format(client.getOutputSize())) except pdfcrowd.Error as why: # report the error sys.stderr.write('Pdfcrowd Error: {}\n'.format(why)) # handle the exception here or rethrow and handle it at a higher level raise
try: # call the API except pdfcrowd.Error as why: # print error sys.stderr.write('Pdfcrowd Error: {}\n'.format(why)) # print just error code sys.stderr.write('Pdfcrowd Error Code: {}\n'.format(why.getCode())) # print just error message sys.stderr.write('Pdfcrowd Error Message: {}\n'.format(why.getMessage())) # or handle the error in your way
Conversion from one image format to another image format. All setter methods return ImageToImageClient object unless otherwise specified.
def __init__(self, user_name, api_key)
Parameter | Description | Default |
---|---|---|
user_name |
Your username at Pdfcrowd.
|
|
api_key |
Your API key.
|
def convertUrl(self, url)
Parameter | Description | Default |
---|---|---|
url |
The address of the image to convert.
The supported protocols are http:// and https://.
|
def convertUrlToStream(self, url, out_stream)
Parameter | Description | Default |
---|---|---|
url |
The address of the image to convert.
The supported protocols are http:// and https://.
|
|
out_stream |
The output stream that will contain the conversion output.
|
def convertUrlToFile(self, url, file_path)
Parameter | Description | Default |
---|---|---|
url |
The address of the image to convert.
The supported protocols are http:// and https://.
|
|
file_path |
The output file path.
The string must not be empty.
|
def convertFile(self, file)
Parameter | Description | Default |
---|---|---|
file |
The path to a local file to convert.
The file can be either a single file or an archive (.tar.gz, .tar.bz2, or .zip). The file must exist and not be empty.
|
def convertFileToStream(self, file, out_stream)
Parameter | Description | Default |
---|---|---|
file |
The path to a local file to convert.
The file can be either a single file or an archive (.tar.gz, .tar.bz2, or .zip). The file must exist and not be empty.
|
|
out_stream |
The output stream that will contain the conversion output.
|
def convertFileToFile(self, file, file_path)
Parameter | Description | Default |
---|---|---|
file |
The path to a local file to convert.
The file can be either a single file or an archive (.tar.gz, .tar.bz2, or .zip). The file must exist and not be empty.
|
|
file_path |
The output file path.
The string must not be empty.
|
def convertRawData(self, data)
Parameter | Description | Default |
---|---|---|
data |
The raw content to be converted.
|
def convertRawDataToStream(self, data, out_stream)
Parameter | Description | Default |
---|---|---|
data |
The raw content to be converted.
|
|
out_stream |
The output stream that will contain the conversion output.
|
def convertRawDataToFile(self, data, file_path)
Parameter | Description | Default |
---|---|---|
data |
The raw content to be converted.
|
|
file_path |
The output file path.
The string must not be empty.
|
def setOutputFormat(self, output_format)
Parameter | Description | Default |
---|---|---|
output_format |
Allowed values:
|
png |
Settings used for operations with images.
def setResize(self, resize)
Parameter | Description | Default |
---|---|---|
resize |
The resize percentage or new image dimensions.
|
100% |
setResize("200%")
setResize("1024x768")
def setRotate(self, rotate)
Parameter | Description | Default |
---|---|---|
rotate |
The rotation specified in degrees.
|
0 |
def setDebugLog(self, debug_log)
Parameter | Description | Default |
---|---|---|
debug_log |
Set to True to enable the debug logging.
|
False |
def getDebugLogUrl(self)
def getRemainingCreditCount(self)
def getConsumedCreditCount(self)
def getJobId(self)
def getOutputSize(self)
def setTag(self, tag)
Parameter | Description | Default |
---|---|---|
tag |
A string with the custom tag.
|
def setHttpProxy(self, http_proxy)
Parameter | Description | Default |
---|---|---|
http_proxy |
The value must have format DOMAIN_OR_IP_ADDRESS:PORT.
|
setHttpProxy("myproxy.com:8080")
setHttpProxy("113.25.84.10:33333")
def setHttpsProxy(self, https_proxy)
Parameter | Description | Default |
---|---|---|
https_proxy |
The value must have format DOMAIN_OR_IP_ADDRESS:PORT.
|
setHttpsProxy("myproxy.com:443")
setHttpsProxy("113.25.84.10:44333")
def setUseHttp(self, use_http)
Parameter | Description | Default |
---|---|---|
use_http |
Set to True to use HTTP.
|
False |
def setUserAgent(self, user_agent)
Parameter | Description | Default |
---|---|---|
user_agent |
The user agent string.
|
pdfcrowd_python_client/4.4.2 (http://pdfcrowd.com) |
def setProxy(self, host, port, user_name, password)
Parameter | Description | Default |
---|---|---|
host |
The proxy hostname.
|
|
port |
The proxy port.
|
|
user_name |
The username.
|
|
password |
The password.
|
def setRetryCount(self, retry_count)
Parameter | Description | Default |
---|---|---|
retry_count |
Number of retries wanted.
|
1 |