DISCOVER
X

Raspberry Pi Image Cruncher Part 3: Scripts and Automation

August 6, 2014 11:22 pm Leave your thoughts
FacebooktwitterredditpinterestlinkedinmailFacebooktwitterredditpinterestlinkedinmail

cruncher_directoryContinuing on from part 2, here is the full script for the image resizer. Let’s call the script crunch2.py for now, so create a file called crunch2.py under /home/pi/image_cruncher. You can of course use any other directory but we will refer to this one in this article. We will go through the script bits by bits and hopefully I will be able to explain why things are coded as such.

The script itself is quite simple, it’s just a top-down procedural programming and not object oriented at all. However the script does the job crunching images in the Raspberry Pi. Of course, it’s also possible to run this script on another machines too.

Cruncher Script Walkthrough

One of the first thing to do in this script is importing os, time and wand image libraries. We need the os library in order to traverse the directory structures and find the files that we want to resize. Time is also required should we wish to print the execution time or giving the resized file timestamp. In this example we are not using it yet.

We then setup the variables that will be used throughout the script. The baseDirectory is the starting point of the directory traversal later on. Remember how we setup Owncloud using WebDav in the previous part, this is where it comes in handy. We are able to access the owncloud directory just like normal physical directory. Also note the Resize directory, this is where you should be uploading the files from mobile phones to process. It’s possible to have sub-directories within this Resize directory though. TargetDirectory is the name of the subdirectory that will house the resized image. After that we setup the allowedExtensions array variable. It’s important to specify which file extension is to be processed to reduce the amount of files we have to open.

In addition to the previous variables, we also setup the maximumDimension and maximumFilesize these 2 variables are important so that we can skip the files that are too big for the Raspberry Pi to process. Please tweak the numbers yourself, these settings are quite conservative as I’m running the 256MB RAM Raspberry Pi. Simply said, if an image exceeds 45oo pixels or exceeds 7340032 bytes, we will not process the image at all as the Raspberry Pi won’t be powerful enough.

Which brings us to the last bit, holdingImageFilename; if an image file is too big and we don’t process it there should be something to tell us. I have resort to creating a holding image called too_large.gif or something so we know straight away when looking at the folder that a file is not processed.

Continuing on, we define 2 functions for our Raspberry Pi image cruncher/resizer. CreateDir as the name suggests is there to simplify checking if the destination directory exist or not, if not then we create the directory. CreateHolderImage on the other hand is a function that copies the image defined in the holdingImageFilename variable into specified path. The idea is if we try to resize test.png and test.png appears to be un-resizeable, then we substitute it in the resized folder using the holding image instead.

The for loop above basically walks through the directories and loads all the files within the directory. This will automatically traverse the directory structure till the last directory. You should be able to follow the rest of the script since it’s pretty linear and there are a lot of comments on it. However I will explain now a couple validations and tricks that we have to do to prevent the script from crashing altogether.

The code above as noted, delays the continuation of the script for 5 seconds. This is just an estimated number that I took to give the Raspberry Pi enough I/O time before continuing to the next image to process. Not allowing this delay seems to create bottleneck and in many cases (throughout the usage of this script) can cause input output error.

This bit of code above checks the file size in case it’s too big. It resizing means we need to load the image in memory and do the processing. The limited amount of RAM that my Raspberry Pi means we need to limit the filesize to prevent potential errors. Perhaps if you have the Raspberry Pi model B or model B+ then you’ll be able to get away with bigger sizes.

Lastly we also check for the image dimension. Even if the file size is not that big, resizing an image with huge dimension can cause out of memory crash as well. So we limit the image dimensions. Again, the limit is based on my 256mb Raspberry Pi.

Launcher Script

So now that we have gone through the script codes, you should have /home/pi/image_cruncher/crunch2.py. The next thing that we have to do is create a launcher script that can be executed by the cron. Why do we need a launcher when we can just run crunch2.py from cron directly? This is because we need to ensure that owncloud webdav is mounted before we run the script.

The easiest way to do it is by using a bash script. It’s pretty safe to keep trying to mount the directory everytime we execute as mount will simply tell you the directory is already mounted. So create a file called startImageCruncher and insert the codes below. Don’t forget to make the file executable by running chmod 755 startImageCruncher.

In the script above you will notice that we also use flock command to run the cruncher. This is to prevent the crunch2.py process from running more than one instance at a time. If we run more than one resizing process on the Raspberry Pi, things will be very slow and even crashing.

Automating with Cron

Lastly we automate our crunching process! Create a file called cruncher inside the /etc/cron.d/ directory.

Then add the following cron task inside the file. The system will pick up this task schedule automatically. In this example, we set the script to run every 35 minutes.

Closing

This concludes the 3 part article of our Raspberry Pi Image Cruncher. I hope these instructions work well on your Raspberry Pi. As usual, if you have any questions or suggestions, feel free to leave comments, I will try to answer as much as I could. Also share this article to your friends if you like it, who knows it may be a good weekend project :) Have fun.


FacebooktwitterredditpinterestlinkedinmailFacebooktwitterredditpinterestlinkedinmail
Tags: , , ,

Categorised in: ,