ImageShrink


Normally when you shrink or stretch a picture on just one axis, the picture quickly becomes distorted, but what if there were a way to do shrink the column while appearing to keep the aspect ratio intact?

In my Algorithms class we discussed a means of using Dynamic programming to selectively remove pixels from a picture in order to shrink or stretch a picture without appearing to change the aspect ratio. This concept is called Seam Carving.

Basically, the program will analyze the pixels of a given picture and find the derivative of that pixel in relationship to the pixels that surround it. Once the energies of the pixels are calculated, a path is found connecting the top of the picture and the bottom that has the least energy of any path. This path is then removed from the picture. This works surprisingly well, check out the pictures. I have left the white space intact in the pictures as the amount of information which was removed from the original.

I wrote the program in Python, using the PIL library to edit the picture as an array. To use the script you will have to have them both installed.

The code is far from perfect or hugely optimized. I wrote this because I had never worked with image manipulation and the idea seemed pretty cool, there are other tools out there I believe which can do this more efficiently. I initially wrote in what I thought would be some major optimizations but many of them were not successful because generally when you move one pixel it can affect a very large portion of the image matrix.

I do NOT recommend running this on large pictures unless you have a)a very powerful computer or b) lots of time.

Script SOURCE here.

To use the program:

  • run the script in python
  • Enter the picture file to be shrunk ( easiest if located in same directory ).
  • Enter the shrinking factor. 2 would remove 1/2, 4 would remove 1/4, 10 would remove 1/10. I recommend removing no more than about 20 or maybe 25%, but it is up to you.
  • Let it run. The file will be named the same as the input file but with an out at the beginning. Test.jpg -> outTest.jpg

Examples from my pictures: