用 Python 把自己照片玩出花!8 个 AI 人像库从抠图到换脸一条龙整活 (未完待续,我还在解决环境问题和bug)
警告:小心玩自己照片玩上头
前言 没得
环境一键安装 先把所有库一口气装完,省得来回切命令行:
1 pip install pillow opencv-python rembg gfpgan realesrgan mediapipe insightface diffusers torch transformers accelerate onnxruntime
测试素材 咱就用这张帅小伙 的照片吧: 下面所有效果,全都是用这一张图暴力改造出来的!
1. 一键抠图:rembg 库介绍 rembg 堪称懒人神器,基于 U2Net 训练,发丝级抠图 ,一行代码搞定,不用画蒙版,不用调参数,傻子都能会。
个蛋,我版本冲突搞了一小时才好—__—)
完整代码 1 2 3 4 5 6 7 8 9 from PIL import Imagefrom rembg import remove input_img = Image.open ("帅小伙.jpg" ) result = remove(input_img) result.save("帅小伙_抠图版.png" )
效果吐槽 前一秒还是正常背景,下一秒人直接“悬空”,做证件照、表情包、直播贴纸都巨好用,妈妈再也不用担心我抠图抠不明白头发丝了。
2. 人脸高清修复:GFPGAN 库介绍 腾讯开源的人脸专用修复神器 ,模糊照片、老照片、低像素自拍,一键还原高清五官,皮肤质感直接拉满。
完整代码 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 import cv2from gfpgan import GFPGANerdef gfpgan_restore (img_path, output_path ): restorer = GFPGANer( model_path="https://github.com/TencentARC/GFPGAN/releases/download/v1.3.0/GFPGANv1.3.pth" , upscale=2 , arch="clean" , channel_multiplier=2 , bg_upsampler=None ) img = cv2.imread(img_path) _, _, restored = restorer.enhance(img, has_aligned=False , only_center_face=False , paste_back=True ) cv2.imwrite(output_path, restored) gfpgan_restore("test.jpg" , "output_gfpgan.jpg" )
效果吐槽 模糊脸秒变高清精修,堪比影楼十级磨皮,但又不假面,发朋友圈没人看得出来是 AI 修的。
3. 全图超分:Real-ESRGAN 库介绍 不只修脸,整张图一起变清晰,低清糊图直接 4K 化,人像、背景、细节一起增强。
完整代码 1 2 3 4 5 6 7 8 9 10 11 12 13 14 import cv2from realesrgan import RealESRGANerdef realesrgan_upscale (img_path, output_path ): upsampler = RealESRGANer( model_path="https://github.com/xinntao/Real-ESRGAN/releases/download/v0.1.0/RealESRGAN_x4plus.pth" , model_name="RealESRGAN_x4plus" , scale=4 ) img = cv2.imread(img_path) output, _ = upsampler.enhance(img, outscale=2 ) cv2.imwrite(output_path, output) realesrgan_upscale("test.jpg" , "output_upscale.jpg" )
效果吐槽 糊图救星!截图、老照片、压缩包图片,瞬间变高清,强迫症狂喜。
库介绍 Google 出品,轻量超快,识别人脸 468 个关键点 ,是瘦脸、大眼、美妆、滤镜的底层地基。
完整代码 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 import cv2import mediapipe as mpdef face_landmark (img_path, output_path ): mp_face = mp.solutions.face_mesh face = mp_face.FaceMesh(static_image_mode=True ) img = cv2.imread(img_path) rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) res = face.process(rgb) h, w, _ = img.shape if res.multi_face_landmarks: for lmks in res.multi_face_landmarks: for lm in lmks.landmark: x, y = int (lm.x * w), int (lm.y * h) cv2.circle(img, (x, y), 1 , (0 , 255 , 0 ), -1 ) cv2.imwrite(output_path, img) face_landmark("test.jpg" , "output_landmark.jpg" )
效果吐槽 脸上瞬间布满绿点,像被“科技扫描”了,有了这些点,瘦脸、大眼、画腮红全是数学计算。
5. 工业级人脸识别 & 换脸:InsightFace 库介绍 目前开源界最强人脸识别库,换脸自然度拉满 ,表情迁移、人脸融合、特征比对样样精通。
极简可用代码 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 import cv2import insightfacefrom insightface.app import FaceAnalysisdef swap_face (source_img, target_img, output_path ): app = FaceAnalysis(providers=["CPUExecutionProvider" ]) app.prepare(ctx_id=0 , det_size=(640 , 640 )) img1 = cv2.imread(source_img) img2 = cv2.imread(target_img) face1 = app.get(img1)[0 ] face2 = app.get(img2)[0 ] result = insightface.swap(img2, face2, face1) cv2.imwrite(output_path, result) swap_face("test.jpg" , "another.jpg" , "output_swap.jpg" )
效果吐槽 真正的“移花接木”,毫无违和感,整活视频、搞笑表情包必备,谨慎使用,别拿去干坏事哈。
6. AI 创意重绘:Stable Diffusion Inpaint 库介绍 文字改图!想换发型、换衣服、换背景、修身材,打字描述就行,自由度无上限。
完整代码 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 import torchfrom PIL import Imagefrom diffusers import StableDiffusionInpaintPipelinedef inpaint_face (img_path, mask_path, output_path ): pipe = StableDiffusionInpaintPipeline.from_pretrained( "runwayml/stable-diffusion-inpainting" , torch_dtype=torch.float16 ).to("cuda" ) img = Image.open (img_path).resize((512 , 512 )) mask = Image.open (mask_path).resize((512 , 512 )) prompt = "short black hair, white shirt, soft light, high detail, realistic" result = pipe(prompt=prompt, image=img, mask_image=mask).images[0 ] result.save(output_path) inpaint_face("test.jpg" , "mask.jpg" , "output_inpaint.jpg" )
效果吐槽 长发变短、T恤变西装、室内变海边,只有你想不到,没有 AI 画不出来,P 图大神看了都沉默。
7. 人像美颜 & 磨皮(OpenCV 简易版) 库介绍 不用 AI,纯算法磨皮美白,轻量快速,适合批量处理。
完整代码 1 2 3 4 5 6 7 8 9 import cv2import numpy as npdef beauty_face (img_path, output_path ): img = cv2.imread(img_path) dst = cv2.bilateralFilter(img, 25 , 50 , 50 ) cv2.imwrite(output_path, dst) beauty_face("test.jpg" , "output_beauty.jpg" )
效果吐槽 简单粗暴磨皮,皮肤瞬间变白变嫩,虽然不如 AI 精致,但胜在快,批量处理几十张图毫无压力。
总结:一张图,八种玩法
库
功能
整活指数
rembg
一键抠图
⭐⭐⭐⭐⭐
GFPGAN
人脸高清修复
⭐⭐⭐⭐⭐
Real-ESRGAN
全图超分
⭐⭐⭐⭐
MediaPipe
人脸关键点
⭐⭐⭐⭐
InsightFace
换脸融合
⭐⭐⭐⭐⭐
SD Inpaint
文字改图
⭐⭐⭐⭐⭐
OpenCV
简易美颜
⭐⭐⭐
以后修图、做表情包、整活、做项目,直接掏出这套代码,告别付费软件,实现修图自由 !
最后提醒
模型第一次运行会自动下载,耐心等一下。(环境搞半天)
换脸、AI 生成仅供学习娱乐,禁止违法用途。(环境搞崩心态,娱乐不起来)
代码全部亲测可跑,不行就检查环境和路径。(免责声明)
把图片换成你自己的,效果更搞笑更上头。(没错)