Undo the Edits

Playing with Image-Editing Code

Jenine
5 min readMar 11, 2021

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:

Using Adobe Photoshop Express to invert an image
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)

Applying the 50% colour halftone to a ~500x300 image
Gaussian 3x3 blur applied; Can notice the blur but the circles remained prominent
Gaussian 5x5 blur applied; It only appeared more blurry to me

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

Mean 3x3 filter applied
Mean 5x5 filter applied; Despite appearing the blurriest, I noticed the halftone-dots the least here!
Median 3x3 filter applied; I believed the mean filter was still the front-runner
Median 5x5 filter applied; The presence of dots lessened but details appeared lost

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%:

The image with its original dimensions at halftone 50%
Mean 5x5 filter applied to the 50% halftone image
The image with its original dimensions at halftone 25%
Mean 5x5 filter applied to the 25% halftone image

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.

A pixel value of 20 produced loss of facial features in a 1200x800 image

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

Image with pixel value = 6
Image with pixel value = 6 and Mean 5x5 filter applied; The face in the background appeared sharper but pixelation was still evident for the person in the forefront of the photo
Image with pixel value = 10
Image with pixel value = 10 and Mean 5x5 filter applied; Some smoothening of both faces occurred but pixelation is still prominent

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

Halftone 50%, Gaussian 5x5 followed by the Sharpen filter
Pixel value = 6 and the Sharpen filter applied

--

--