How to get a professional and automatic aspect ratio crop for your images

By February 11, 2019 March 26, 2019 DIY Instructions, Software

On your dig­i­tal pic­ture frame, you are like­ly to have pho­tos that were tak­en by var­i­ous peo­ple using dif­fer­ent cam­eras. This typ­i­cal­ly means that your pho­to files will have many incon­sis­tent aspect ratios. Some may be 4:3 (Four-Thirds cam­eras), oth­ers 3:2 (DSLR), and every­thing in between is pos­si­ble with a smart­phone.

The dig­i­tal pic­ture frame only has one for­mat, typ­i­cal­ly 16:10 or 16:9. The effect of a non-for­mat-adjust­ed fam­i­ly & friends pho­to col­lec­tion can be pil­lar or let­ter­box­ing.

The online ser­vice Crop­po­la uses sophis­ti­cat­ed image recog­ni­tion soft­ware to find the best com­po­si­tion and crop in a pho­to auto­mat­i­cal­ly. We talked to the Crop­po­la team in Switzer­land and will show you a Python script which auto­mates the crop­ping process for your images.

A well maintained digital picture frame needs some love

A dig­i­tal pic­ture frame requires only lit­tle main­te­nance, but there is one thing that needs to be done right when you upload to new images: The images need to have the right aspect ratio.

How­ev­er, man­u­al­ly check­ing and crop­ping many hun­dred images or even more is not only very time con­sum­ing and requires the right pho­to soft­ware but it also asks for a bit of a photographer's eye.

As "mak­ing it real­ly sim­ple" is one of the crit­i­cal suc­cess ingre­di­ents of a dig­i­tal pic­ture frame (and need­ed to get the stamp of approval from oth­er house­hold mem­bers), I did a fair amount of research to test-dri­ve a num­ber of online crop­ping tools.

Fol­low­ing many hours of research­ing and test­ing var­i­ous online tools, I came across Crop­po­la, a solu­tion that I can high­ly rec­om­mend to any­one.

If you want your images to be intel­li­gent­ly cropped to the exact aspect ratio of your dig­i­tal pic­ture frame, then Crop­po­la is the way to go.

Francis Ford C(r)oppola

Crop­po­la is the brain­child of Rad­hakr­ish­na Achan­ta, a for­mer Ph.D. stu­dent in the field of Com­put­er Vision at the EPFL Uni­ver­si­ty of Lau­sanne in Switzer­land. Out of appre­ci­a­tion for a movie direc­tor, the prod­uct name was born.

With­in the Image and Visu­al Rep­re­sen­ta­tion Group (IVRG), direct­ed by Pro­fes­sor Sabine Süsstrunk he devel­oped a clever image recog­ni­tion algo­rithm that auto­mat­i­cal­ly rec­og­nizes faces, col­ors and oth­er objects in videos.

In 2012 this know-how was applied to pho­tos, and Achanta's Ph.D. stu­dent col­league Thomas Lochmat­ter cre­at­ed a web­site to make the Crop­po­la tech­nol­o­gy avail­able to the pub­lic.

I talked with Thomas Lochmat­ter, an IT con­sul­tant for dis­trib­uted data sys­tems, about Crop­po­la. Since 2012, Thomas has devel­oped and main­tained the soft­ware and servers of the Crop­po­la project.

It's a kind of magic

Crop­po­la serves up over 100,000 cropped images through its web­site every month. The ser­vice is acces­si­ble either through upload­ing images on the site or through an API.

Although there are pay­ing high-vol­ume crop­ping cor­po­rate cus­tomers, Crop­po­la remains main­ly a research project with mon­e­ti­za­tion only being giv­en sec­ond thoughts. This is why, luck­i­ly, the crop­ping ser­vice is free for users with less than 500 images a month.

Crop­po­la is super fast, and the hit rate of get­ting the right crop is aston­ish­ing­ly high. Artis­tic tastes are a per­son­al mat­ter, so there will always be instances where a per­son may pre­fer a dif­fer­ent crop, but for automat­ing the crop­ping process for a dig­i­tal pic­ture frame, Crop­po­la is more than ide­al.

It remind­ed me of the face recog­ni­tion fea­ture of pho­to soft­ware like Adobe Light­room or Apple Pho­tos. But Crop­po­la rec­og­nizes a lot more than just faces.

Thomas men­tioned cor­po­rate projects where Crop­po­la was inte­grat­ed into the val­ue chain of their clients automat­ing the process of crop­ping peo­ple pho­tos into fixed shape objects and sav­ing the client a lot of work.

Two things that I would wish for future devel­op­ments: One is that the JPG com­pres­sion qual­i­ty which is cur­rent­ly fixed at 95% can be spec­i­fied or set to 100%. The oth­er one is the preser­va­tion of EXIF data. Unfor­tu­nate­ly, they are removed today after crop­ping which is not great if oth­er automa­tion scripts depend on EXIF data. An exam­ple would be the fil­ter­ing of images based on dates or loca­tions. Thank­ful­ly, Thomas offered that he would look into these issues.

The Python script that will save you many hours

Crop­po­la offers an API that lets you batch upload images that are sub­se­quent­ly ana­lyzed, cropped and resized. This means that you can run a Python script in the back­ground which auto­mat­i­cal­ly trig­gers the upload and crop­ping of new images.

The auto­mat­ic fold­er super­vi­sion to trig­ger the Crop­po­la script will be described in a lat­er post, but the fol­low­ing Python script from Thomas does a great job of look­ing into a fold­er for new images, upload­ing them to Crop­po­la and sav­ing the resized images to a new fold­er. All that's left to do is to copy the new­ly cropped images to your final images fold­er.

In this exam­ple, I have spec­i­fied the crop­ping for a 16:10, 1920 x 1200 px pic­ture frame but you can define the aspect ratio and the final pix­el dimen­sions as you need it.

Paste the fol­low­ing code in a text edi­tor and save it as croppola.py.

#!/usr/bin/env python3

import os, requests, time

# Prepare the folders
originalFolder = 'new-images-inbox'
croppedFolder = 'new-images-cropped'

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

# This is the URL
url = 'http://croppola.com/croppola/image.jpg?aspectRatio=16:10&maximumWidth=100%&scaledWidth=1920&scaledHeight=1200&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)
        time.sleep(5)	# let other people crop
    else:
        print('Error ' + res.status_code)
        break

Adjust the set­tings to fit your dig­i­tal pic­ture frame in this sec­tion:

# This is the URL
url = 'http://croppola.com/croppola/image.jpg?aspectRatio=16:10&maximumWidth=100%&scaledWidth=1920&scaledHeight=1200&algorithm=croppola'

If your aspect ratio is e.g. "16:9" and your mon­i­tor res­o­lu­tion is 1920 x 1080, you can eas­i­ly define it in this line.

There are a lot of indi­vid­ual set­tings that Crop­po­la allows. You can find fur­ther infor­ma­tion on their home­page. But for our pur­pose, this is all you need.

Move the Python file into the "Pic­tures" fold­er of your Rasp­berry Pi.

I assume that you have your SMB file shar­ing con­nec­tion set up. If not, read how to set it up in "How to set up your Rasp­berry Pi for your dig­i­tal pic­ture frame".

To test it, add a few images to the "new-images-inbox" fold­er.

Open up your Ter­mi­nal, and con­nect to your Rasp­berry Pi.
ssh pi@IP-of-your-RaspberryPi

Change to the Pic­tures direc­to­ry
cd Pictures

Then enter
python3 croppola.py
Crop­po­la should now con­vert your files and place the out­put into the "new-images-cropped" fold­er.

Check if the images have the right size as you spec­i­fied.

You're done! No more tedious crop­ping and size con­ver­sion, Crop­po­la will auto­mate this process for you.

A big "Thank You" to the team behind Crop­po­la and espe­cial­ly Thomas Lochmat­ter who pro­vid­ed the Python script and gave me some more back­ground. You guys rock!