发布时间:2021-07-27 23:55:06编辑:run阅读(3733)
不同的python库都可以用于基本的图像操作,几乎所有库都以numpy ndarray存储图像
使用numpy数组切片成圆形图
import matplotlib.image as mpimg
import matplotlib.pylab as plt
import numpy as np
# 使用numpy数组切片和掩模在人物图像上创建圆形掩模
# 指定默认字体
plt.rcParams['font.sans-serif'] = ['KaiTi']
# 解决保存图像是负号'-'显示为方块的问题
plt.rcParams['axes.unicode_minus'] = False
lena = mpimg.imread(r'D:\image_processing\jpgs\c.jpg')
test = mpimg.imread(r'D:\image_processing\jpgs\c.jpg')
lx, ly, _ = lena.shape
x, y = np.ogrid[0:lx, 0:ly]
mask = (x - lx / 2) ** 2 + (y - ly / 2) ** 2 > lx * ly / 4
lena[mask, :] = 0
plt.figure(figsize=(10,10))
plt.subplot(221), plt.imshow(test), plt.title('原图', size=20),plt.axis('off')
plt.subplot(222), plt.imshow(lena), plt.title('圆形图', size=20),plt.axis('off')
plt.show()
简单的图像合并,将背景图和人物图合并成一张新图
from PIL import Image
import matplotlib.pylab as plt
# 指定默认字体
plt.rcParams['font.sans-serif'] = ['KaiTi']
# 解决保存图像是负号'-'显示为方块的问题
plt.rcParams['axes.unicode_minus'] = False
im1 = Image.open(r'D:\image_processing\jpgs\c.jpg')
im2 = Image.open(r'D:\image_processing\jpgs\aa.jpg')
w1 = im1.width # 图片的宽
h1 = im1.height # 图片的高
im3 = Image.new('RGB', (w1, h1), 'black')
n = 0
for y in range(h1):
for x in range(w1):
if y % 2 == 0:
if n % 2 == 0:
im3.putpixel((x, y), im1.load()[x, y])
else:
im3.putpixel((x, y), im2.load()[x, y])
n += 1
else:
if n % 2 == 0:
im3.putpixel((x, y), im2.load()[x, y])
else:
im3.putpixel((x, y), im1.load()[x, y])
n += 1
plt.figure(figsize=(10,10))
plt.subplot(221), plt.imshow(im1), plt.title('头像图', size=20),plt.axis('off')
plt.subplot(222), plt.imshow(im2), plt.title('背景图', size=20),plt.axis('off')
plt.subplot(223), plt.imshow(im3), plt.title('合成图', size=20),plt.axis('off')
plt.show()
下一篇: python3-PIL图像实战1
51308
50760
41356
38165
32639
29535
28381
23254
23222
21548
1620°
2356°
1958°
1903°
2232°
1940°
2631°
4414°
4249°
3020°