本文分享一段利用Python编写的证件照自动排版代码,能够根据用户选择自动调整照片的大小和比例,并自动排版至6寸页面,混合3种类型常用证件照(上面6张1寸,左下2张2寸,右下2张小2寸)

代码中所用到的第三方库包括:Pillow(用于图片处理)

FLASK在线演示:

最终效果图展示:

转载请标明出处

测试图:

此人像为AI生成,不涉及侵犯肖像权

目录下准备一个名字为证件照的图片(png or jpg)即可运行

import os
from PIL import Image, ImageDraw, ImageEnhance, ImageFilter
def resize_photo(photo,choice):if choice == 1:resized_photo = photo.resize((250,350))enhancer = ImageEnhance.Sharpness(resized_photo)resized_photo = enhancer.enhance(2)return resized_photoif choice == 2:resized_photo = photo.resize((350, 500))enhancer = ImageEnhance.Sharpness(resized_photo)resized_photo = enhancer.enhance(2)return resized_photoif choice == 3:resized_photo = photo.resize((330, 480))enhancer = ImageEnhance.Sharpness(resized_photo)resized_photo = enhancer.enhance(2)return resized_photo
def cut_photo(photo,choice):width = photo.size[0] height = photo.size[1]rate = height / widthif choice == 1:if rate < (350/250):x = (width - int(height / 350 * 250)) / 2y = 0cutted_photo = photo.crop((x, y, x + (int(height / 350 * 250)), y + height))else:x = 0y = (height - int(width / 250 * 350)) / 2cutted_photo = photo.crop((x, y, x + width, y + (int(width / 250 * 350))))return cutted_photoif choice == 2:if rate < (500/350):x = (width - int(height / 500* 350)) / 2y = 0cutted_photo = photo.crop((x, y, x + (int(height / 500* 350)), y + height))else:x = 0y = (height - int(width / 350 * 500)) / 2cutted_photo = photo.crop((x, y, x + width, y + (int(width / 350 * 500))))return cutted_photoif choice == 3:if rate < (480/330):x = (width - int(height / 480* 330)) / 2y = 0cutted_photo = photo.crop((x, y, x + (int(height / 480* 330)), y + height))else:x = 0y = (height - int(width / 330 * 480)) / 2cutted_photo = photo.crop((x, y, x + width, y + (int(width / 330 * 480))))return cutted_photo
try:photo = Image.open('证件照.jpg')
except:photo = Image.open('证件照.png')
photo_1 = resize_photo(cut_photo(photo, 1), 1)
width_px, height_px = photo_1.size
txt = str(width_px) + '-' + str(height_px)
print("一寸比列:" + txt)
photo_2 = resize_photo(cut_photo(photo, 2), 2)
width_px, height_px = photo_2.size
txt = str(width_px) + '-' + str(height_px)
print("二寸比列:" + txt)
photo_3 = resize_photo(cut_photo(photo, 3), 3)
width_px, height_px = photo_3.size
txt = str(width_px) + '-' + str(height_px)
print("小二寸比列:" + txt)
# 6寸背景
print_bg = background = Image.new('RGB', (1520, 1020), 'white')
# 1寸证件照布局
print_bg.paste(photo_1, (0, 100))
print_bg.paste(photo_1, (254, 100))
print_bg.paste(photo_1, (508, 100))
print_bg.paste(photo_1, (762, 100))
print_bg.paste(photo_1, (1016, 100))
print_bg.paste(photo_1, (1270, 100))
# 2寸证件照布局
print_bg.paste(photo_2, (40, 500))
print_bg.paste(photo_2, (430, 500))
# 小2寸证件照布局
print_bg.paste(photo_3, (800, 500))
print_bg.paste(photo_3, (1170, 500))
# 证件图框线
draw = ImageDraw.Draw(print_bg)
draw.rectangle((0, 100, 248, 450), outline='black', width=1)
draw.rectangle((254, 100, 502, 450), outline='black', width=1)
draw.rectangle((508, 100, 756, 450), outline='black', width=1)
draw.rectangle((762, 100, 1010, 450), outline='black', width=1)
draw.rectangle((1016, 100, 1264, 450), outline='black', width=1)
draw.rectangle((1270, 100, 1518, 450), outline='black', width=1)
draw.rectangle((40, 500, 390, 1000), outline='black', width=1)
draw.rectangle((430, 500, 780, 1000), outline='black', width=1)
draw.rectangle((800, 500, 1133, 980), outline='black', width=1)
draw.rectangle((1170, 500, 1500, 980), outline='black', width=1)
path = os.getcwd() + "/6寸混合证件照(画质优化).jpeg"
enhancer = ImageEnhance.Sharpness(print_bg)
print_bg = enhancer.enhance(2)
print_bg.save(path)
print_bg.show()
print("完毕!")

FLASK版本:

预览(请上传小质量照片)

python:

from flask import Flask, render_template, request, redirect, url_for, send_file, abort
from werkzeug.utils import secure_filename
from PIL import Image, ImageDraw, ImageEnhance, ImageFilter
import re
import osapp = Flask(__name__)
app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024  # 限制上传文件的大小为 16MB# 设置上传文件保存的目录
app.config['UPLOAD_FOLDER'] = os.path.join(app.root_path, 'uploads')
# 如果目录不存在则创建
if not os.path.exists(app.config['UPLOAD_FOLDER']):os.makedirs(app.config['UPLOAD_FOLDER'])# 设置处理后的图片保存的目录
app.config['RESULT_FOLDER'] = os.path.join(app.root_path, 'result')
# 如果目录不存在则创建
if not os.path.exists(app.config['RESULT_FOLDER']):os.makedirs(app.config['RESULT_FOLDER'])@app.route('/')
def home():return render_template('home.html')@app.route('/process_image', methods=['POST'])
def process_image():# 获取用户上传的文件并保存到本地file = request.files['file']filename = secure_filename(file.filename)file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)file.save(file_path)# 打开上传的图片photo = Image.open(file_path)# 处理图片photo_1 = resize_photo(cut_photo(photo, 1), 1)photo_2 = resize_photo(cut_photo(photo, 2), 2)photo_3 = resize_photo(cut_photo(photo, 3), 3)bg = generate_print_bg(photo_1, photo_2, photo_3)enhancer = ImageEnhance.Sharpness(bg)bg = enhancer.enhance(2)# 将处理后的图片保存到本地output_filename = 'output.jpg'output_path = os.path.join(app.config['RESULT_FOLDER'], output_filename)bg.save(output_path)# 返回处理后的照片并提供下载链接return render_template('result.html', filename=output_filename)@app.route('/show_image/<filename>')
def show_image(filename):# 定义要显示的图片文件路径和下载链接photo_url = url_for('show_image', filename=filename)download_url = url_for('download', filename=filename)# 将图片路径和下载链接传入模板进行渲染return render_template('result.html', photo_url=photo_url, download_url=download_url)@app.route('/download/<filename>')
def download(filename):path = os.path.join(app.config['RESULT_FOLDER'], filename)return send_file(path, as_attachment=True)def resize_photo(photo,choice):# 处理图片尺寸if choice == 1:resized_photo = photo.resize((250,350))enhancer = ImageEnhance.Sharpness(resized_photo)resized_photo = enhancer.enhance(2)return resized_photoif choice == 2:resized_photo = photo.resize((350, 500))enhancer = ImageEnhance.Sharpness(resized_photo)resized_photo = enhancer.enhance(2)return resized_photoif choice == 3:resized_photo = photo.resize((330, 480))enhancer = ImageEnhance.Sharpness(resized_photo)resized_photo = enhancer.enhance(2)return resized_photodef cut_photo(photo, choice):# 剪裁图片width = photo.size[0]height = photo.size[1]rate = height / widthif choice == 1:if rate < (350 / 250):x = (width - int(height / 350 * 250)) / 2y = 0cutted_photo = photo.crop((x, y, x + (int(height / 350 * 250)), y + height))else:x = 0y = (height - int(width / 250 * 350)) / 2cutted_photo = photo.crop((x, y, x + width, y + (int(width / 250 * 350))))return cutted_photoif choice == 2:if rate < (500 / 350):x = (width - int(height / 500 * 350)) / 2y = 0cutted_photo = photo.crop((x, y, x + (int(height / 500 * 350)), y + height))else:x = 0y = (height - int(width / 350 * 500)) / 2cutted_photo = photo.crop((x, y, x + width, y + (int(width / 350 * 500))))return cutted_photoif choice == 3:if rate < (480 / 330):x = (width - int(height / 480 * 330)) / 2y = 0cutted_photo = photo.crop((x, y, x + (int(height / 480 * 330)), y + height))else:x = 0y = (height - int(width / 330 * 480)) / 2cutted_photo = photo.crop((x, y, x + width, y + (int(width / 330 * 480))))return cutted_photodef generate_print_bg(photo_1, photo_2, photo_3):# 生成6寸混合证件照print_bg = Image.new('RGB', (1520, 1020), 'white')# 插入1寸证件照print_bg.paste(photo_1, (0, 100))print_bg.paste(photo_1, (254, 100))print_bg.paste(photo_1, (508, 100))print_bg.paste(photo_1, (762, 100))print_bg.paste(photo_1, (1016, 100))print_bg.paste(photo_1, (1270, 100))# 插入2寸证件照print_bg.paste(photo_2, (40, 500))print_bg.paste(photo_2, (430, 500))# 插入小2寸证件照print_bg.paste(photo_3, (800, 500))print_bg.paste(photo_3, (1170, 500))# 证件图框线draw = ImageDraw.Draw(print_bg)draw.rectangle((0, 100, 248, 450), outline='black', width=1)draw.rectangle((254, 100, 502, 450), outline='black', width=1)draw.rectangle((508, 100, 756, 450), outline='black', width=1)draw.rectangle((762, 100, 1010, 450), outline='black', width=1)draw.rectangle((1016, 100, 1264, 450), outline='black', width=1)draw.rectangle((1270, 100, 1518, 450), outline='black', width=1)draw.rectangle((40, 500, 390, 1000), outline='black', width=1)draw.rectangle((430, 500, 780, 1000), outline='black', width=1)draw.rectangle((800, 500, 1133, 980), outline='black', width=1)draw.rectangle((1170, 500, 1500, 980), outline='black', width=1)return print_bg
if __name__ == '__main__':app.run(debug=True)

home.html:

<!doctype html>
<html lang="zh"><head><title>6存照片 - 混合排版1寸、2寸、小2寸证件照 </title><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1"><style>body {font-family: Arial, sans-serif;color: #333;margin: 0;padding: 0;}h1 {text-align: center;margin-top: 30px;}form {display: flex;flex-direction: column;align-items: center;margin-top: 50px;}input[type="file"] {margin-bottom: 20px;}button[type="submit"] {background-color: #007aff;color: #fff;border: none;padding: 10px 20px;border-radius: 5px;cursor: pointer;font-size: 16px;}footer {background-color: #eee;text-align: center;padding: 10px 0;margin-top: 50px;}footer a {color: #007aff;text-decoration: none;}</style></head><body><h1> 6存照片 - 混合排版1寸、2寸、小2寸证件照 </h1><form action="/process_image" method="post" enctype="multipart/form-data"><input type="file" name="file" required><button type="submit">处理图片</button></form><footer>Powered by 派圣. 点击 <a href="https://blog.csdn.net/aimersong69">这里</a> 了解更多信息。</footer></body>
</html>

result.html:

<!DOCTYPE html>
<html lang="zh"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>下载页面</title><style>body {font-family: Arial, sans-serif;color: #333;margin: 0;padding: 0;}h1 {text-align: center;margin-top: 30px;}img {display: block;margin: 50px auto;max-width: 80%;height: auto;box-shadow: 0 5px 10px rgba(0, 0, 0, 0.2);}a {display: block;text-align: center;margin: 20px auto;background-color: #007aff;color: #fff;border: none;padding: 10px 20px;border-radius: 5px;text-decoration: none;font-size: 16px;width: fit-content;cursor: pointer;}</style></head><body><h1> 请下载... </h1><img src="{{ url_for('show_image', filename=filename) }}" alt="Processed image"><a href="{{ url_for('download', filename=filename) }}"> Download </a></body>
</html>

python:6寸照片纸混合排版1寸、2寸、小2寸证件照相关推荐

  1. 在photoshop中,从1寸到24寸照片的大小是多少?

    1寸的相片的有效尺寸为宽2.7厘米,高3.6厘米,以分辨率为300像素/英寸换算,像素值应该是300*450,英寸为1:1.4. 2寸的照片的有效尺寸宽为3.6厘米,高为5.4厘米,以分辨率为300像 ...

  2. photoshop中如何在6寸相纸上打印1寸照片10张2X5模式(自动填充模式)

                                      如何在6寸相纸上打印1寸照片                                                     ...

  3. 计算机照片打印设置方法,详解设置打印机纸张添加7寸照片尺寸在win7电脑中的操作的步骤...

    我们在很多的设计中打印照片的时候很多的小伙伴都是喜欢不停的效果的,那打印纸的时候怎么设置比较特殊的打印的尺寸的呢,对于7寸照片是怎么设置的呢,有小伙伴提问这个问题今天小编就来跟大家分享一下详解设置打印 ...

  4. 案例教程:一步步教你ps制作二寸照片

    下面是ps制作二寸照片具体操作步骤: 1.在photoshop教程中打开需要人像,因为大小尺寸不符合规定,所以用裁剪工具进行裁剪. 在工具箱中找到裁剪工具,或者按下C键,然后在上面的属性栏进行如下设置 ...

  5. 1寸照片如何修改底色?证件照换背景教程

    许多报名平台上传一寸证件照时都对背景颜色有具体要求,那么我们就需要证件照换底色来应对,本文将介绍一个快速证件照换背景(https://www.yasuotu.com/coloreplace)的方法,可 ...

  6. photoshop7.0 排版一寸照片、2寸照片

    说明:必须先照一张一寸电子照片,否则是无法做成1.本例同样采用photoshop CS5制作,其它版本通用,这里采用上一教程"PS照片处理教程-制作一寸照片并排版"的处理效果图进行 ...

  7. 【Python】多图形混合排版,如何在Matplotlib/Seaborn中实现?

    数据展示时,在同一页面上混合排版多个图形是一种常见的用法.本次分享一个Python轮子patchworklib: 通过|./轻松实现图形排列: 比matplotlib.seaborn等自带子图功能更加 ...

  8. 用Photoshop制作2寸照片方法

    用Photoshop制作2寸照片方法 作者: 点击数:17735 更新时间:10-03-23     问: 平时用的小1吋.小2吋照片是多大的?在电脑上如何选择?需要电子版的照片,可是用数码相机拍的太 ...

  9. 6寸照片的尺寸是多少_各类证件照标准尺寸大全

    1英寸证件照尺寸: 25mm×35mm (或者2.54X3.62cm)在5寸相纸(12.7X8.9厘米)中排8张 2英寸证件照尺寸: 35mm×49mm 在5寸相纸(12.7X8.9厘米)中排4张 3 ...

最新文章

  1. 微软虚拟化解决方案课件
  2. codevs——1019 集合论与图论
  3. java面向对象程序设计董小圆_2017-2018-2 20165325 实验三《Java面向对象程序设计》实验报告...
  4. 1.19 final修饰符详解
  5. nginx https 访问http_Nginx之Http模块系列之访问控制模块
  6. 窗体 局部变量转换为全局_从嵌入式编程中感悟「栈」为何方神圣?
  7. ERROR ITMS-90022,90023,问题已解决
  8. 罗永浩要造智能音箱;苹果承认bug;微软特制AI曝光 | 极客头条
  9. nbear分页 效率低_为什么大家都说“SELECT *”效率低?
  10. 亚马逊AI惹众怒:一个没有意识的程序,竟然自己学会了“重男轻女”
  11. Windows提权 cmd 开启 3389
  12. 用java写一个双色球的彩票程序(源码)
  13. 递归算法教学设计java,递归算法数字游戏教学软件的设计|java递归算法经典实例...
  14. 屏幕录像专家2018注册机怎么用?
  15. Openwrt常用软件模块之CWMP
  16. react 翻书效果_react实现页面切换动画效果
  17. CQF笔记M1L4随机分析和伊藤引理
  18. java-家庭作业1
  19. js 网页做一个简单的计算器
  20. pycharm新建python项目等问题

热门文章

  1. 使用beyond compare4作为Git的比较工具
  2. 2021年中国凹面平板探测器市场趋势报告、技术动态创新及2027年市场预测
  3. XILINX开发板KCU105使用aurora协议---上
  4. 深入Preact源码分析(4.20更新)
  5. java mail 抄送多用户,JavaMail 发送邮件,收件人为多人,抄送多人。其中包含收件人邮箱错误时的处理...
  6. 常见分布式事务解决方案
  7. 接口快速复制到 Postman 接口快速修改参数调试
  8. css中侧边导航栏怎么隐藏,CSS3手机侧边导航栏滑动隐藏特效
  9. 基于LAPM架构mysql数据库搭建wordpress个人博客
  10. CPU 的物理核与逻辑核