Skip to content

Documentation: The glitch this library

Chase edited this page Jul 26, 2020 · 8 revisions

Welcome to the glitch-this library docs! Here you will find all the information you need to get glitching, in any python project you like!

If you don't have the library already, get it on pypi

A full example of how you can use the class is in test_script.py

First you need to import the required class - ImageGlitcher

from glitch_this import ImageGlitcher

Now you can instantiate the class

glitcher = ImageGlitcher()

Now all you have to do is call the methods mentioned here and pass in the necessary parameters!

Method- glitch_image(self, src_img, glitch_amount, glitch_change=0.0, cycle=False, color_offset=False, scan_lines=False, gif=False, frames=23, step=1)

Returns an Image object if gif is set to False

Returns a list of PngImageFile objects if gif is set to True

Required Params

  • src_img: Either Full/Relative path to the Image you'd like to glitch (OR) A PIL Image object
  • glitch_amount: A number between 0.1 to 10.0 (including both endpoints), signifying how glitched the output image should be

You can supply a float value as glitch_amount as well! Just remember, floats with a max of 1-2 decimal places will make a difference. There probably won't be any difference between 1.592 glitch_amount and 1.610 glitch_amount but there will be between 1.5 and 1.6.

Optional Params

  • color_offset: Specify whether to use color offset effect (recommended True)

    Defaults to False

  • scan_lines: Specify whether to use scan lines effect, for an old school CRT TV look!

    Defaults to False

  • gif: Specify whether the returned object should be ready to be saved as a GIF

    If True, the function will return a list of PngImageFile objects, ready to be made into a GIF!

    Defaults to False

  • seed: Use a custom seed for the RNG used

    This ensures you generate predictable glitches

    Defaults to None

    Note: If used with gif=True, this will generate exactly the same image for all frames.

  • glitch_change: Increment/Decrement the glitch_amount by given value after every frame glitched

    Should be in range [+-0.0, +-10.0] (inclusive)

    Defaults to 0.0, i.e glitch_amount will stay the same after every frame glitched

    You can use a negative value to decrement it instead!

  • cycle: Specify True if glitch_amount should be looped back to 1 or 10, if it overflows or underflows

    Defaults to False, i.e glitch_amount will stay at 1 or 10, instead of getting looped back

    glitch_amount will get incremented/decremented if glitch_change is included, when it gets decremented to 1, it'll be underflowed to 10 cycle is True. Likewise, when it gets incremented to 10, it'll be overflowed to 1

  • frames: Specify how many glitched images to store in the list of PngImageFile

    Defaults to 23

  • step: Glitch every stepth frame instead of all frames

    Defaults to 1, i.e glitch every frame

    If included this will glitch every stepth frame, keeping the other frames untouched. There will still be exactly the number of frames specified by frames parameter

Code examples!

All these examples have color_offset turned on

For example if you wanted to get a glitched image, of glitch level 2 (with color_offset), for an image file in the same folder named test.png, you'd do-

from glitch_this import ImageGlitcher
glitch_img = glitcher.glitch_image('test.png', 2, color_offset=True)

You could also pass in the Image object itself;

from PIL import Image
from glitch_this import ImageGlitcher
img = Image.open('test.png')
glitch_img = glitcher.glitch_image(img, 2, color_offset=True)

You can now save it using

glitch_img.save('glitched_test.png')

Or you can do anything at all with the returned Image object!

How about GIFs? Let's do one of those!

from glitch_this import ImageGlitcher
glitch_img = glitcher.glitch_image('test.png', 2, color_offset=True, gif=True)

Again, you could also pass in the Image object itself;

from PIL import Image
from glitch_this import ImageGlitcher
img = Image.open('test.png')
glitch_img = glitcher.glitch_image(img, 2, color_offset=True, gif=True)

Same thing right? Well how do we save it? Like this!

DURATION = 200      # Set this to however many centiseconds each frame should be visible for
LOOP = 0            # Set this to how many times the gif should loop
                    # LOOP = 0 means infinite loop
glitch_img[0].save('glitched_test.gif',
                   format='GIF',
                   append_images=glitch_img[1:],
                   save_all=True,
                   duration=DURATION,
                   loop=LOOP)

Method- glitch_gif(self, src_gif, glitch_amount, glitch_change=0.0, cycle=False, color_offset=False, scan_lines=False, step=1)

Returns the following:

  • List of PngImage objects,
  • Average duration (in centiseconds) of each frame in the original GIF,
  • Number of frames in the original GIF

Required Params

  • src_img: Either Full/Relative path to the GIF Image you'd like to glitch (OR) A PIL Image object (must be GIF)
  • glitch_amount: An integer between 0.1 to 10.0 (including both endpoints), signifying how glitched the output image should be

You can supply a float value as glitch_amount as well! Just remember, floats with a max of 1-2 decimal places will make a difference. There probably won't be any difference between 1.592 glitch_amount and 1.610 glitch_amount but there will be between 1.5 and 1.6.

Optional Params

  • color_offset: Specify whether to use color offset effect (recommended True)

    Defaults to False

  • scan_lines: Specify whether to use scan lines effect, for an old school CRT TV look!

    Defaults to False

  • glitch_change: Increment/Decrement the glitch_amount by given value after every frame glitched

    Should be in range [+-0.0, +-10.0] (inclusive)

    Defaults to 0.0, i.e glitch_amount will stay the same after every frame glitched

    You can use a negative value to decrement it instead!

  • seed: Use a custom seed for the RNG used

    This ensures you generate predictable glitches

    Defaults to None

  • cycle: Specify True if glitch_amount should be looped back to 1 or 10, if it overflows or underflows

    Defaults to False, i.e glitch_amount will stay at 1 or 10, instead of getting looped back

    glitch_amount will get incremented/decremented if glitch_change is included, when it gets decremented to 1, it'll be underflowed to 10 cycle is True. Likewise, when it gets incremented to 10, it'll be overflowed to 1

  • step: Glitch every stepth frame instead of all frames

    Defaults to 1, i.e glitch every frame

    If included this will glitch every stepth frame, keeping the other frames untouched. There will still be exactly the number of frames specified by frames parameter

Code examples!

All these examples have color_offset turned on

For example if you wanted to get a glitched gif, of glitch level 2 (with color_offset), for a GIF file in the same folder named test.gif, you'd do-

from glitch_this import ImageGlitcher
glitch_img, src_gif_duration, src_gif_frames = glitcher.glitch_gif('test.gif', 2, color_offset=True)

You could also pass in the Image object itself;

from PIL import Image
from glitch_this import ImageGlitcher
img = Image.open('test.gif')
glitch_img, src_gif_duration, src_gif_frames = glitcher.glitch_gif(img, 2, color_offset=True)

You can now save it using

DURATION = 200      # Set this to however many centiseconds each frame should be visible for
LOOP = 0            # Set this to how many times the gif should loop
                    # LOOP = 0 means infinite loop

# You could also use the `src_gif_duration` returned by `glitch_gif` 
# To keep the GIF exactly the same durationwise

glitch_img[0].save('glitched_test.gif',
                   format='GIF',
                   append_images=glitch_img[1:],
                   save_all=True,
                   duration=DURATION,
                   loop=LOOP)

Or you can do anything at all with the returned Image object!

That's it! Get glitchin'!