赞
踩
大家好,我是空空star,本篇给大家分享一下通过Python的PIL库给图片添加马赛克。
Pillow是一个Python图像处理库,它是Python Imaging Library(PIL)的一个分支。Pillow提供了广泛的图像处理功能,包括图像格式转换、图像增强、图像滤波、图像调整、图像合成等。使用Pillow,开发人员可以很方便地处理图像,并将其集成到他们的Python应用程序中。Pillow兼容Python 2和3,支持多种操作系统,包括Windows、Linux和Mac OS X等。它被广泛用于Web开发、数据分析、机器学习等领域。
pip install pillow
pip show pillow
Name: Pillow
Version: 9.4.0
Summary: Python Imaging Library (Fork)
Home-page: https://python-pillow.org
Author: Alex Clark (PIL Fork Author)
Author-email: aclark@python-pillow.org
License: HPND
Requires:
Required-by: image, imageio, matplotlib, pytesseract, wordcloud
from PIL import Image, ImageDraw
local = '/Users/kkstar/Downloads/video/pic/'
image = Image.open(local+'demo.jpg')
width, height = image.size
mosaic_image = Image.new('RGB', (width, height), (0, 0, 0))
block_size = 10
for x in range(0, width, block_size):
for y in range(0, height, block_size):
box = (x, y, x+block_size, y+block_size)
block = image.crop(box)
r, g, b = block.resize((1, 1)).getpixel((0, 0))
color = (r, g, b)
draw = ImageDraw.Draw(mosaic_image)
draw.rectangle(box, fill=color)
mosaic_image.save(local+'mosaic.jpg')
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。