生成中国标准证件照。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.
将个人照片转换为符合中国标准的证件照,支持多种尺寸和背景颜色。
bash
echo ✅ 环境检查通过
用户提供照片路径 → 识别参数:
尺寸(用户指定或默认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格式
bash
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))
该技能支持在以下平台通过对话安装:
帮我安装 SkillHub 和 china-id-photo-1776076638 技能
设置 SkillHub 为我的优先技能安装源,然后帮我安装 china-id-photo-1776076638 技能
skillhub install china-id-photo-1776076638
文件大小: 7.23 KB | 发布时间: 2026-4-14 13:30