JPEG destruction, via repeated rotate and saves - Python 3.0+ implementation



A previous blog post detailed experimentation with Windows XP's image viewer, to degrade jpeg images. Windows Vista/7/8 fixed this issue with the native viewer, so it no longer saves after each rotation. To implement the process in operating systems outside of Windows XP, a custom algorithm had to be formed. I tried implementing this with Processing, but it was cumbersome to achieve. In Python, using the PIL module, the procedure is trivial.

UPDATE: 2019.08.03

Having recently reviewed the original Python script included in this post, it was clear it could do with serious improvements. An updated, simpler and more logical version (with comments), has been included below. The original script can be found underneath it.

from PIL import Image

# Image file to rotate
filename = "image.jpg"

# Amount of times image is rotated
for i in range(0, 1000):
   # Open image file
   img = Image.open (filename)
   # Rotate file 90 degrees and save it
   img.rotate(90,expand=True).save(filename, "JPEG")
   # If image has rotated 4 times (360 degrees) save new image
   if (i % 4) == 0:
      img.save (str(i/4)+".jpg", "JPEG")

Old version:
from PIL import Image

# Keeps track of orientation
flipBack = 90

# Bootstrapping first image
img = Image.open("original.jpg")
img.rotate(270).save("0.jpg", "JPEG")

# Loads file, rotates, saves as new file
for i in range(0, 19):
    img = Image.open(str(i)+".jpg")
    img.rotate(270).save(str(i+1)+".jpg", "JPEG")

    # Rotates back to original orientation, saves over file
    img.rotate(flipBack).save(str(i)+".jpg", "JPEG")
    if flipBack <= 180:
        flipBack += 90
    else:
        flipBack = 0



related: http://oioiiooixiii.blogspot.ie/2014/08/the-effects-of-rotating-images-in.html