如何在Python使用 Block_Distortion 模块扭曲图像?
在本文中,我们将讨论如何使用Python对图像执行块失真。我们将使用一个名为 block_distoriton 的模块。让我们来看看这个模块的简介。
块失真模块
- 它对图像应用块失真效果。
- 它可以选择生成图像的静态和动画 (.gif) 版本。
- 可以控制失真量
安装
要安装此模块,请在终端中键入以下命令。
pip install block_distortion
扭曲静止图像
distort_image()函数用于扭曲图像。
句法:
distort_image(image, splits=2000)
Parameter:
splits = Number of splits [ distortions ] to be performed, defaults to 2000. More the splits, more smoother is image.
使用的图像:
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))
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)
输出 :
扭曲 GIF 图像
animate_image() 方法用于执行所需的 gif 失真。
句法:
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 persistance of each frame in gif. ( in m/s ) defaults to 100.
蟒蛇3
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)
输出 :