Croppola logo

Getting Started Technical Support

Examples

Flexible Cropping Fixed-ratio Cropping Fixed-shape Cropping Command Line Thumbnail Creation Python

Croppola Documentation – Examples

Python

Croppola.com can be accessed using the requests library for Python. The two following scripts may serve as starting point to integrate croppola into your Python project.

Cropping a single picture

The following Python script uploads a picture (tea.jpg) to croppola.com for cropping, and stores the resulting crop:

import os, requests

# Open the original picture
data = open('tea.jpg', 'rb')

# Make the request
url = 'http://croppola.com/croppola/image.jpg?aspectRatio=1.0&minimumHeight=80%&algorithm=croppola'
res = requests.post(url, data=data)

# Save the cropped picture
if res.status_code == 200:
    f = open('tea-cropped.jpg', 'wb')
    f.write(res.content)
    print('Done!');
else:
    print('Error ' + res.status_code + '!')

To change the aspect ratio, or other parameters, simply modify the URL.

Batch cropping all pictures within a folder

Similarly, the following script processes all pictures within the original folder, and stores the crops in the cropped folder:

import os, requests

# Prepare the folders
originalFolder = 'original'
croppedFolder = 'cropped'

if not os.path.exists(croppedFolder):
    os.makedirs(croppedFolder)

# This is the URL
url = 'http://croppola.com/croppola/image.jpg?aspectRatio=16:9&maximumHeight=90%&algorithm=croppola'

# Process all pictures
for pictureFile in os.listdir(originalFolder):
    print(pictureFile)
    data = open(originalFolder + '/' + pictureFile, 'rb')
    res = requests.post(url, data=data, headers={'User-Agent' : 'py'})
    data.close();
    if res.status_code == 200:
        f = open(croppedFolder + '/' + pictureFile, 'wb')
        f.write(res.content)
    else:
        print('Error ' + res.status_code)

Note that croppola.com is available free of charge for a volume of up to 100 photos per day, 500 photos per months, or 2400 photos per year. Above that, please request an API key.