Skip to content
Related Articles

Related Articles

How to distort image using Block_Distortion module in Python ?

Improve Article
Save Article
  • Last Updated : 14 Dec, 2021
Improve Article
Save Article

In this article, we will discuss how to perform block distortion on images using Python. We will be using a module called block_distoriton. Let’s see a brief about this module.

Block_distortion module

  • It applies block distortion effects on images.
  • It has the option to produce both still and animated(.gif) versions of an image.
  • Amount of distortion can be controlled

Installation

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

pip install block_distortion

Distorting the still image

distort_image() function is used to distort the images.

Syntax:

distort_image(image, splits=2000)

Parameter:
splits = Number of splits [ distortions ] to be performed, defaults to 2000. More the splits, more smoother is image.

Image Used:

Python3




from skimage import img_as_ubyte
from skimage.io import imread, imsave
from block_distortion import distort_image
  
# read image
input_image = imread('hotel.jpeg')
  
# distort the read image
distorted = distort_image(input_image)
  
# save to required path the converted binary image
imsave("./block-hotel.png", img_as_ubyte(distorted))

Output :

Distorting GIF images

animate_image() method is used to perform required gif distortion. 

Syntax:

animate_image(splits=2000, frames=100)

Parameters:
splits = Number of splits [ distortions ] to be performed, defaults to 2000. More the splits, more smoother is image.
frames = Number of frames to be created for gif image. defaults to 100.

write_frames_to_gif(path=curr, animated_image, duration=100)

path : Location to save file.
duration : duration of persistence of each frame in gif. ( in m/s ) defaults to 100.

Python3




from skimage.io import imread
from block_distortion import animate_image, write_frames_to_gif
  
# read the image
input_image = imread("hotel.jpeg")
  
# convert to .gif after block distortion
frames = animate_image(input_image)
  
# write gif to output path
write_frames_to_gif("./block-anim-hotel.gif", frames, duration=100)

Output : 


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!