Photo Histograms Everywhere!

Aug 31
Posted on August 31, 2008 0:56 in Photography, Problems & Solutions, Projects

Most of you probably know that I am a huge fan of photography, my Flickr account being the prove of it. But besides uploading my own photographs I also love to give other photographers feedback on their photography.

One of my common things I like to do when judging a photo is to have a look at the photo’s histogram. Unfortunately to do so, you need to download the photo, load it into your favorite photo application and then look at the graph. This gets a bit tedious if you want to do it quite often.

So I decided to make my own solution that will work in any browser. I have some experience with bookmarklets, and as they are highly cross-browser they looked like the way to go. Unfortunately most browser do not support the next generation CSS/Javascript that will allow for the histogram data to be read out in the browser only, so I had to extend the concept with some server side code. It all worked out though, and after less than a day coding I now have a working system.

Server Side Code

The server side part is a PHP script that parses the photo’s pixels and generates a nice graph using the Google Chart API. The service returns a javascript function when run in the bookmarklet but it can also be called as a service to generate a image or a html block. Here is an example of an image and it’s equivalent histogram created via the server side script.


Geeks in the Country - Cliffs

 

Geeks in the Country - Cliffs

The url of the photo is 

http://farm4.static.flickr.com/3122/2806124294_08cc5efc74.jpg

And the url of the corresponding histogram is

http://histogram.cristianobetta.com/?type=image&rgb=true&image=

http://farm4.static.flickr.com/3122/2806124294_08cc5efc74.jpg

As you can see, the histogram script takes 4 parameters. The image parameter defines the image to be parsed. This can be either a jpg, png, or gif file. The rgb parameter let’s you choose between a grayscale or rgb histogram. A grayscale histogram for this image would look like this.

Geeks in the Country - Cliffs

Finally the type parameter let’s you choose between an image (like the examples above), html object (image pre-wrapped in a img html object) or a piece of Javascript (js, callable as getSrc()). 

Since September 3rd 2008 the service also take a width parameter. For more info see the end of this post.

The Bookmarklet 

The web service gets more powerful when we add a nice bookmarklet. I wanted to be able to load the histogram for any image on the web with just a few clicks. So, I decided to make a bookmarklet that loads a simple GUI, prompts you to click the image you want to analyze, and then shows you the histogram. The result was a bit more code than I hoped for but more solid than I expected. I think the easiest way to show you how it works is with a video.



Download

So enough talking, I guess you want to try it yourself. Drag this bookmarklet into your bookmarks bar and try it out. 

If you really like the service, I would appreciate if you would install your own version of the server side script on your own server, simply because it will save me bandwidth feel free to use the webservice and bookmarklet as much as you want. You can also decide to download the webservice to your own host if you want to.  When installed on your own server, change the host variable in the bookmarklet to point to your own server and all should be fine.

The source of both the server side script and the bookmarklet are available to anyone who wants to play with them and released under GPL2.

Download as Zip

Improvements

There are still a few things that need to be worked out. I invite anyone to have a look at the code and help me out. Some of the main improvements currently are:

  • Caching – Currently nothing is cached. A little bit of caching would make things way less bandwidth demanding. This has been implemented since September 3rd, 2008. Javascript, image, and HTML output is now by default cached for 24 hours.
  • Custom size – Currently the server side script returns a fixed size image, but it would be arbitrary to allow the bookmarklet to send the parameters defining the size. I added a simple height parameter that is optional. Images will always have a aspect ratio of 2:1 as this makes histograms look better, plus it makes it easier to oblige to Google Chart API’s demand of staying under 300.000 pixels. Therefore the max height you can specify is 385 pixels (making the image 385×770, or 296.450 pixels).
  • Correct Histogram Calculation – I don’t think that I’m far off at the moment, but I might have scaled some of the histograms just slightly off from what it is supposed to look like. I need to read up on how histograms are generated in pro-tools.

32 Responses to “Photo Histograms Everywhere!”

  1. Gerard says:

    Hmmm strange: this works: http://histogram.cristianobetta.com/?type=image&image=http://www.wfcfotobeeld.com/data/523/a900DSC22267-5BW.JPG

    This does not work: http://www.wfcfotobeeld.com/histogram4.php?type=image&image=http://www.wfcfotobeeld.com/data/523/a900DSC22267-5BW.JPG

    Difference, from your site you call I guess directly the index.php (I can’t use that becaus index.php is the index of our site), so I renamed it to histogram4.php, now it does not work, a bug?

    The difference is this: http://www.wfcfotobeeld.com/histogram4.php?
    Strange error (and I hope you understand Dutch because you a Dutch/Italian: De afbeelding “http://www.wfcfotobeeld.com/histogram4.php?type=image&image=http://www.wfcfotobeeld.com/data/523/a900DSC22267-5BW.JPG” kan niet worden weergegeven, omdat hij fouten bevat. ??????

    In English: the image …….. cannot be viewed becasue it has errors?? Very strange, the image is on the site and can be viewed, so I guess the problems lies somewhere in de index.php

  2. Gerard says:

    Both supplied codes do not work.
    Ok I still continueing in English so other people can benefit from this.

    Hmmm the server, this is what I coudl find:
    Apache version 2.2.13 (Unix) does this help
    PHP version 5.2.9
    Operating system Linux

    Is this sufficient?

  3. Hmm, that seems to be a good enough setup. This script uses CURL though, do you have CURl access on your account?

  4. Gerard says:

    Cristiano, you lost me, CURL?

    I have allot of acces, but I’m not sure what CURL is

  5. Can you ssh into your machine? And if so, can you use the command “curl” on the command line?

  6. Gerard says:

    Yes I can enter SSH, however when I try I need to make a key of some sort.

    I have a screen which says: Generating a Public Key:

    RSA vs DSA: RSA and DSA are encryption algorithms used to encrypt your key. DSA is faster for Key Generation and Signing and RSA is faster for Verification.

    Then I need to give a key name, a password is already supplied, but I do not know what this password can be because I never supplied a password.

    RSA or DSA etc etc. Hmmm I have no clue of what I should do here so I think I better do nothing at all

  7. Gerard, this is going a bit beyond the scope of this blog post. Send me a mail and I’ll try and help you out

  8. Gerard says:

    Yes that’s better, and we could continue in Dutch

Leave a Reply