Undo the Edits

Playing with Image-Editing Code

At AS-Level, I took Photography as a subject in my Sixth Form. It was the first time I really learnt about different features of Adobe Photoshop. In a classroom again, I found myself thinking about Photoshop. It was in one of my Machine Learning classes as part of my Master’s degree. The lesson covered algorithms to edit images. Initially, I found the lesson difficult. After taking some of my lecturer’s time afterwards to ask a few questions, I remember facetiously blurting out “I now appreciate Photoshop more!”.

After successfully creating image-editing code as assignments for that class, I decided to revisit some of my code after my degree. I wanted to see if I could undo edits I made in Photoshop with that code.

1. Inverted Images

My assignment’s tasks mainly called methods from a single helper-methods file. To invert an image, I needed to add a new method to that file. A hurdle I encountered was how the images of my assignments consisted of black and white images hence initial code I wrote did not cater for RGB images. With this in mind, my invert method was as follows:

My main script then included the many different methods from this file ready to be applied to an imported image:

(Using some of my brother’s photography,) I tested the invert method:

Successfully obtaining the original image via the invert method

2.a. Halftone Images — Preparation

Another effect in Photoshop Express was to mimic a halftone process. I chose this as the next effect to revert because it allowed me to test existing methods in my helper-methods file. My existing methods covered:

  • Applying a specified convolution filter
  • Applying a mean filter
  • Applying a median filter

(Again, RGB images were initially not considered so there were some tweaks to my original code for this.)

Applying a filter meant applying a set of values to an image. Technically speaking, an image would be represented as a 2D array. A filter would be a smaller 2D array to be overlapped and moved around the larger 2D array (the image). Matrix-calculations would be included to change the image’s 2D array values using the filter’s 2D array values. This would then produce a new 2D array: a filtered image.

A specified filter therefore meant the filter’s values were supplied. This is compared to the mean and median filters where the mean and median values (respectively) of the image itself were used as the filter.

Returning to my main script, some specified filters I included were as follows:

  • Laplacian filter (also known as an edge-detection filter)
  • Sharpen filter
  • Gaussian 3x3 blur
  • Gaussian 5x5 blur

To read more about this process and the specified filters, see here.

2.b. Halftone Images — Execution

This effect became more difficult to revert compared to the inverted effect. This was because of the dots created as part of the halftone process (unlike inverted images where no shapes were introduced, just pixel values were manipulated). From the filters previously listed, the Gaussian blur, mean and median filters were mainly tested.

I initially tested a Gaussian 3x3 blur on a 50% colour halftone image. There was no noticeable difference. The ~2000x1300 sized image meant my code to apply a filter took a few minutes. This coupled with the relatively small filter (3x3 compared to ~2000x1300) meant I changed the original image to ~500x300 to test more filters in a shorter amount of time:

(Please note blurry images will appear in succession of one another)

After testing the Gaussian filters, I then tested the mean and median filters:

After being the most satisfied with the Mean 5x5 filter, I then applied this filter to the image in its original size at halftone 50% and 25%:

After the many filters, I concluded the Mean 5x5 filter reverted the halftone effect the best. It would not produce a near-perfect result similar to reverting an inverted image. However, I believed I noticed the dots (introduced by the halftone effect) the least with this filter.

3. Pixelated Images

A final effect I wanted to test was pixelation. To get some test images, I noted whilst introducing pixelation, features and shapes were lost when the pixel value was not that high. Therefore small pixel values of 6 and 10 were used.

After the success of the Mean 5x5 filter, I finished off my experiment by testing the pixelated images with this filter:

Closing Remarks

Attempting to produce original images from edited images using code was a fun task. Despite not producing perfect results, it is clear more time can be used to further explore what filters might work best with other effects. For example, there might be a different filter to use for the pixelated images.

Also, I now give more thought to the labour that has gone into filters via apps…

Thanks for reading!

Photo Credits

PS. Bloopers

Coder