Skip to content
Related Articles

Related Articles

Improve Article

How to distort image using Block_Distortion module in Python ?

  • Last Updated : 04 Jul, 2021
Geek Week

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


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.


distort_image(image, splits=2000)

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

Image Used:


from skimage import img_as_ubyte
from 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. 


animate_image(splits=2000, frames=100)

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 persistance of each frame in gif. ( in m/s ) defaults to 100.


from 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 : 

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course

My Personal Notes arrow_drop_up
Recommended Articles
Page :