HomeFontsAbout

Image glitcher

Updated on 2021-11-13

Although less frequent today, it was common enough in the past for files to become corrupted. This corruption can occur from network errors while transferring the file or due to the file being stored on a faulty storage. Typically, this renders the file unreadable, but for some multimedia formats it creates readable file with interesting visual effects. The results are known as glitch art.

Now, what if we want to create glitches instead of waiting for them to be produced naturally? If we assume the glitches are introduced through random bit flips, then the following python program will get the job done.

from random import randrange
from sys import argv

flips = int(argv[1])
count = int(argv[2])
name, ext = argv[3].split('.')

for i in range(count):
  with open(f'{name}.{ext}', 'rb') as input:
    content = bytearray(input.read())
    for _ in range(flips):
      j, k = randrange(len(content)), randrange(8)
      content[j] ^= 2**k
  with open(f'{name}__{i}.{ext}', 'wb') as output:
    output.write(content)

The values you should use for flips will depend on many things, including the image format and the size of the image. Of course, not all output images will still be valid, but some will. For a 640x426 jpeg image I found 20 random flips worked well. Here are a few examples I thought were pretty cool.

Original photo by Andreas Strandman on Unsplash.


Copyright © 2020-2021 thestuffido.xyz All Rights Reserved