Python实例题:Python合成女神图片

目录

Python实例题

题目

方式一:使用 Pillow 进行简单图片合成

代码解释

方式二:使用 dlib 和 opencv-python 进行面部特征合成

代码解释

运行思路

方式一

方式二

注意事项

以下是完整的doubaocanvas代码块:

生成 simple_composite.py

生成 face_morphing.py

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_path1image_path2替换为实际图片路径。
  • 运行代码,合成后的图片将保存为composite_goddess.jpg

方式二

  • 安装dlibopencv-python库:
pip install dlib opencv-python
  • 下载shape_predictor_68_face_landmarks.dat文件,并将代码中的路径替换为实际文件路径。
  • 准备两张女神图片,将代码中的image1image2替换为实际图片路径。
  • 运行代码,合成后的图片将保存为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)
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值