Open In App

Adding Glitches to Images using Python

Last Updated : 08 Sep, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

One of the filters currently popular is to add random glitches to the images. These create a random effect that comes out to be a natural glitch. Usually, glitches are introduced by the corruption of frames of images or adding a layer over them. In this article, we will use the two different modules of Python to add glitches to the image.

Using GlitchArt Module

This Python library adds random glitch effects to Images. It does this by distorting or Corrupting JPEG frames of some random bytes of Images, to not harm the File. Internally it uses Pillow library to perform tasks. 

Installation 

To install this module type the below command in the terminal.

pip3 install glitchart

After installation, the glitchart library is imported and jpeg() or png() is invoked depending upon the extension of the image to be glitched. Just run the script and a new glitched file will be created with the name <filename>_glitch in the same path.

Function Used:

png(): Add glitches to png images.

Syntax:

png(photo, seed=random_val, min_amount=0, max_amount=10, inplace=False):

Parameters:

  • photo :  The required .png format photo to add glitch to.
  • seed : Random number, if we wish to add similar amount of glitch to next photo, can have same seed value.
  • min_amount : Minimum amount of glitch required, defaults to 0.
  • max_amount : Maximum amount of glitch required, defaults to 10.
  • inplace : boolean field, if True, changes original photo, not creating new one.

Example:

Input Image:

Python3




import glitchart
 
glitchart.png('gfg.png')


 

 

Output:

 

 

Example 2: Controlling Glitch amount.

 

Python3




import glitchart
 
glitchart.png('gfg.png', max_amount=3)


Output : 

Using glitch-this Module 

Using this, we can perform the task of glitching images using command line by supplying image path and levels and certain image and file management parameters.

Installation 

To install this module type the below command in the terminal

pip install glitch-this

Command Line Parameters Explanation

glitch_this [-h] [–version] [-c] [-s] [-g] [-ig] [-f] [-o Outfile_path] Image_Path Glitch_Level

Parameters:
Image_Path : The image path to perform glitch on.
Glitch_Level : Level of Glitch to be applied, from 0.1 to 10.0 [inclusive].
-h : Getting pull description
-o Outfile_path :Explicitly supply full/relative path to output file.
-g : Output image as gif.
-ig :  Include if input is gif.
-f : If output file has to be overwritten.
-c : If color effect has to be added.
-s. : If sideline effect has to be added.

Example: Controlling Glitch amount.

Example of Glitching using glitch-this

Output : 

Glitched Image Output



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads