返回顶部
c

china-id-photo中国证件照

生成中国标准证件照。Use when the user needs to create an ID photo, passport photo, visa photo, or any standard photo with specific size and background color. Supports 1-inch, 2-inch, passport, and custom sizes with white/blue/red backgrounds. Outputs JPEG or PNG. Uses OpenCV for face detection.

作者: admin | 来源: ClawHub
源自
ClawHub
版本
V 1.2.1
安全检测
已通过
227
下载量
免费
免费
0
收藏
概述
安装方式
版本历史

china-id-photo

中国证件照生成 China ID Photo

将个人照片转换为符合中国标准的证件照,支持多种尺寸和背景颜色。

功能特点

  • - 🎨 支持白/蓝/红三种标准背景色
  • 📐 支持1寸、2寸、护照等标准尺寸
  • 🖼️ 输出JPEG(默认)或PNG格式
  • 🔒 完全本地处理,不上传任何服务器

触发时机

  • - 帮我做一张证件照
  • 把这张照片转成1寸证件照
  • 我需要蓝色背景的2寸照片
  • 生成护照照片,白色背景
  • 这张照片转成证件照,要红色背景
  • 帮我制作身份证照片

Step 0:环境检查与依赖安装

bash

检查 python3


if ! command -v python3 &> /dev/null; then
echo ❌ 需要 python3
echo macOS: brew install python3
echo Ubuntu: sudo apt install python3
echo Windows: 从 python.org 下载安装
exit 1
fi

检查并安装依赖

echo 📦 检查依赖... pip install rembg pillow opencv-python-headless -q 2>/dev/null || { echo ❌ 依赖安装失败,请手动执行: echo pip install rembg pillow opencv-python-headless exit 1 }

echo ✅ 环境检查通过



Step 1:识别用户需求

用户提供照片路径 → 识别参数:

尺寸(用户指定或默认1寸):
1寸 / 一寸 / 小一寸 → 295×413 像素
2寸 / 二寸 → 413×579 像素
小二寸 / 护照 / passport → 390×567 像素
大一寸 / 签证 / visa → 390×567 像素
身份证 / id card → 358×441 像素
未指定 → 默认1寸(295×413)

背景色(用户指定或默认白色):
白色 / 白底 / white → #FFFFFF
蓝色 / 蓝底 / blue → #438EDB(标准证件照蓝)
红色 / 红底 / red → #FF0000
未指定 → 默认白色

输出格式:
PNG / png / 透明 → PNG格式
未指定 → 默认JPEG格式



Step 2:生成证件照

bash

设置参数(根据用户需求调整)


INPUT_IMAGE=/path/to/user/photo.jpg
SIZE=1 # 尺寸:1/2/passport/id_card
BG_COLOR=white # 背景色:white/blue/red
OUTPUT_FORMAT=jpg # 输出格式:jpg/png

创建输出目录

OUTPUTDIR=${OPENCLAWWORKSPACE:-$PWD}/idphoto$(date +%Y%m%d_%H%M%S) mkdir -p $OUTPUT_DIR

执行Python脚本处理图片

python3 - $INPUTIMAGE $OUTPUTDIR $SIZE $BGCOLOR $OUTPUTFORMAT << PYEOF import sys import os from rembg import remove from PIL import Image, ImageFilter import cv2 import numpy as np

从命令行参数获取配置

input_path = sys.argv[1] output_dir = sys.argv[2] size_type = sys.argv[3] bgcolorname = sys.argv[4] output_format = sys.argv[5]

标准尺寸映射(像素,300DPI)

SIZES = { 1: (295, 413), # 1寸 2: (413, 579), # 2寸 passport: (390, 567), # 护照/小二寸 id_card: (358, 441), # 身份证 }

背景色映射(RGB)- 中国证件照标准色值

BG_COLORS = { white: (255, 255, 255), # 白色:身份证、护照、签证 blue: (67, 142, 219), # 蓝色:毕业证、工作证 red: (255, 0, 0), # 红色:结婚证、党员证 }

def detectface(imgarray):
使用OpenCV检测人脸,返回包含头发的头部边界框
face_cascade = cv2.CascadeClassifier(
cv2.data.haarcascades + haarcascadefrontalfacedefault.xml
)

if len(img_array.shape) == 3:
gray = cv2.cvtColor(imgarray, cv2.COLORRGB2GRAY)
else:
gray = img_array

faces = face_cascade.detectMultiScale(
gray,
scaleFactor=1.1,
minNeighbors=5,
minSize=(30, 30)
)

if len(faces) > 0:
# 返回最大的人脸
areas = [w * h for (x, y, w, h) in faces]
max_idx = areas.index(max(areas))
fx, fy, fw, fh = faces[max_idx]

# 扩展边界框以包含头发
# 向上扩展约40%(头发区域)
# 向下扩展约10%(下巴下方)
# 向左右各扩展约10%
expand_top = int(fh * 0.4)
expand_bottom = int(fh * 0.1)
expand_side = int(fw * 0.1)

# 计算扩展后的边界框(确保不超出图片边界)
imgh, imgw = gray.shape
newx = max(0, fx - expandside)
newy = max(0, fy - expandtop)
neww = min(fw + expandside * 2, imgw - newx)
newh = min(fh + expandtop + expandbottom, imgh - new_y)

return (newx, newy, neww, newh)

return None

def refine_mask(mask, radius=2):
优化抠图边缘,去除锯齿
# 转换为numpy数组
mask_np = np.array(mask)

# 轻微模糊边缘
masknp = cv2.GaussianBlur(masknp, (radius2+1, radius2+1), 0)

# 二值化
, masknp = cv2.threshold(masknp, 128, 255, cv2.THRESHBINARY)

return Image.fromarray(mask_np)

def cropfacecentered(img, targetsize, facebbox=None):
根据头部位置进行智能裁剪,确保头顶留白符合证件照标准

标准要求:
- 头顶到照片上边沿:约10%照片高度
- 头部高度:约65%照片高度
- 头部水平居中

targetw, targeth = target_size
targetratio = targetw / target_h
imgw, imgh = img.size

if face_bbox is not None:
fx, fy, fw, fh = face_bbox

# 计算头部中心(水平位置)
head_cx = fx + fw // 2

# 根据中国证件照标准计算裁剪区域
# 头部高度应占照片高度的65%
# 头顶留白占照片高度的10%

# 假设face_bbox已经是扩展后的头部区域
# 头部高度 fh 对应照片高度的 65%
targetareah = int(fh / 0.65)
targetareaw = int(targetareah * target_ratio)

# 确保不超出图片
targetareaw = min(targetareaw, img_w)
targetareah = min(targetareah, img_h)

# 水平居中
cropx = headcx - targetareaw // 2

# 垂直位置:头顶留10%空白
# face_bbox的y坐标是头顶位置
# 头顶到裁剪区域顶部的距离 = 照片高度 * 10%
headtopoffset = int(targetareah * 0.10)
cropy = fy - headtop_offset

# 边界调整
cropx = max(0, min(cropx, imgw - targetarea_w))
cropy = max(0, min(cropy, imgh - targetarea_h))

img = img.crop((cropx, cropy, cropx + targetareaw, cropy + targetareah))

标签

skill ai

通过对话安装

该技能支持在以下平台通过对话安装:

OpenClaw WorkBuddy QClaw Kimi Claude

方式一:安装 SkillHub 和技能

帮我安装 SkillHub 和 china-id-photo-1776076638 技能

方式二:设置 SkillHub 为优先技能安装源

设置 SkillHub 为我的优先技能安装源,然后帮我安装 china-id-photo-1776076638 技能

通过命令行安装

skillhub install china-id-photo-1776076638

下载

⬇ 下载 china-id-photo v1.2.1(免费)

文件大小: 7.23 KB | 发布时间: 2026-4-14 13:30

v1.2.1 最新 2026-4-14 13:30
修复代码bug:img_ratio变量未定义

Archiver·手机版·闲社网·闲社论坛·羊毛社区· 多链控股集团有限公司 · 苏ICP备2025199260号-1

Powered by Discuz! X5.0   © 2024-2025 闲社网·线报更新论坛·羊毛分享社区·http://xianshe.com

p2p_official_large
返回顶部