目录
方式二:使用 dlib 和 opencv-python 进行面部特征合成
Python实例题
题目
Python合成女神图片
方式一:使用 Pillow 进行简单图片合成
此方法能将两张女神图片按一定位置和透明度进行合成。
from PIL import Image
def composite_images(image_path1, image_path2, output_path):
# 打开两张图片
img1 = Image.open(image_path1)
img2 = Image.open(image_path2)
# 调整图片大小(如果需要)
img2 = img2.resize(img1.size)
# 合成图片,这里使用 50% 的透明度
composite = Image.blend(img1, img2, 0.5)
# 保存合成后的图片
composite.save(output_path)
# 示例使用
image_path1 = 'goddess1.jpg'
image_path2 = 'goddess2.jpg'
output_path = 'composite_goddess.jpg'
composite_images(image_path1, image_path2, output_path)
代码解释
- 导入库:导入
Pillow
库中的Image
模块。 - 打开图片:使用
Image.open()
方法打开两张女神图片。 - 调整大小:若两张图片尺寸不同,可使用
resize()
方法将它们调整为相同大小。 - 合成图片:使用
Image.blend()
方法按指定透明度合成两张图片。 - 保存图片:使用
save()
方法保存合成后的图片。
方式二:使用 dlib 和 opencv-python 进行面部特征合成
此方法可以提取多张女神图片的面部特征,然后合成一张包含这些特征的新图片。
import cv2
import dlib
import numpy as np
# 加载人脸检测器和特征点检测器
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")
def get_landmarks(image):
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
faces = detector(gray)
if len(faces) > 0:
landmarks = predictor(gray, faces[0])
landmarks_points = []
for n in range(0, 68):
x = landmarks.part(n).x
y = landmarks.part(n).y
landmarks_points.append((x, y))
return np.array(landmarks_points)
return None
def affine_transform(image, source_points, dest_points):
M = cv2.getAffineTransform(source_points[:3], dest_points[:3])
warped_image = cv2.warpAffine(image, M, (image.shape[1], image.shape[0]))
return warped_image
def face_morphing(image1, image2, alpha):
landmarks1 = get_landmarks(image1)
landmarks2 = get_landmarks(image2)
if landmarks1 is not None and landmarks2 is not None:
# 计算平均特征点
mean_landmarks = (1 - alpha) * landmarks1 + alpha * landmarks2
# 进行仿射变换
warped_image1 = affine_transform(image1, landmarks1, mean_landmarks)
warped_image2 = affine_transform(image2, landmarks2, mean_landmarks)
# 融合图片
morphed_image = (1 - alpha) * warped_image1 + alpha * warped_image2
return morphed_image.astype(np.uint8)
return None
# 示例使用
image1 = cv2.imread('goddess1.jpg')
image2 = cv2.imread('goddess2.jpg')
alpha = 0.5
morphed_image = face_morphing(image1, image2, alpha)
if morphed_image is not None:
cv2.imwrite('morphed_goddess.jpg', morphed_image)
代码解释
- 加载检测器:使用
dlib
加载人脸检测器和特征点检测器。 - 获取面部特征点:定义
get_landmarks()
函数,用于检测图片中的人脸并提取 68 个面部特征点。 - 仿射变换:定义
affine_transform()
函数,用于对图片进行仿射变换,使两张图片的面部特征点对齐。 - 面部融合:定义
face_morphing()
函数,计算平均特征点,进行仿射变换并融合图片。 - 保存合成图片:使用
cv2.imwrite()
方法保存合成后的图片。
运行思路
方式一
- 安装
Pillow
库:
pip install pillow
- 准备两张女神图片,将代码中的
image_path1
和image_path2
替换为实际图片路径。 - 运行代码,合成后的图片将保存为
composite_goddess.jpg
。
方式二
- 安装
dlib
和opencv-python
库:
pip install dlib opencv-python
- 下载
shape_predictor_68_face_landmarks.dat
文件,并将代码中的路径替换为实际文件路径。 - 准备两张女神图片,将代码中的
image1
和image2
替换为实际图片路径。 - 运行代码,合成后的图片将保存为
morphed_goddess.jpg
。
注意事项
- 图片质量:图片的质量和面部角度会影响合成效果,尽量选择清晰、正面的图片。
- 版权问题:确保使用的图片具有合法的使用权限。
以下是完整的doubaocanvas
代码块:
生成 simple_composite.py
import cv2
import dlib
import numpy as np
# 加载人脸检测器和特征点检测器
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")
def get_landmarks(image):
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
faces = detector(gray)
if len(faces) > 0:
landmarks = predictor(gray, faces[0])
landmarks_points = []
for n in range(0, 68):
x = landmarks.part(n).x
y = landmarks.part(n).y
landmarks_points.append((x, y))
return np.array(landmarks_points)
return None
def affine_transform(image, source_points, dest_points):
M = cv2.getAffineTransform(source_points[:3], dest_points[:3])
warped_image = cv2.warpAffine(image, M, (image.shape[1], image.shape[0]))
return warped_image
def face_morphing(image1, image2, alpha):
landmarks1 = get_landmarks(image1)
landmarks2 = get_landmarks(image2)
if landmarks1 is not None and landmarks2 is not None:
# 计算平均特征点
mean_landmarks = (1 - alpha) * landmarks1 + alpha * landmarks2
# 进行仿射变换
warped_image1 = affine_transform(image1, landmarks1, mean_landmarks)
warped_image2 = affine_transform(image2, landmarks2, mean_landmarks)
# 融合图片
morphed_image = (1 - alpha) * warped_image1 + alpha * warped_image2
return morphed_image.astype(np.uint8)
return None
# 示例使用
image1 = cv2.imread('goddess1.jpg')
image2 = cv2.imread('goddess2.jpg')
alpha = 0.5
morphed_image = face_morphing(image1, image2, alpha)
if morphed_image is not None:
cv2.imwrite('morphed_goddess.jpg', morphed_image)
生成 face_morphing.py
from PIL import Image
def composite_images(image_path1, image_path2, output_path):
# 打开两张图片
img1 = Image.open(image_path1)
img2 = Image.open(image_path2)
# 调整图片大小(如果需要)
img2 = img2.resize(img1.size)
# 合成图片,这里使用 50% 的透明度
composite = Image.blend(img1, img2, 0.5)
# 保存合成后的图片
composite.save(output_path)
# 示例使用
image_path1 = 'goddess1.jpg'
image_path2 = 'goddess2.jpg'
output_path = 'composite_goddess.jpg'
composite_images(image_path1, image_path2, output_path)