python实现图片九宫格分割


Posted in Python onMarch 07, 2021

大家都知道在微信朋友圈或者微博以及QQ动态中,有很多“强迫症患者”发图片都爱发9张,而有些图是一张图片分成的九宫图,对于这种操作,大家知道是怎么做到的吗?

本文就是用Python做的一个九宫格图片生成器,是一个打包好的exe文件,用户无需部署安装Python的开发环境,在本地就可以运行此程序,以此快速生成九宫格图片。

下面是程序的所有代码,这是一个Python GUI程序,代码不多,也很容易理解:

# -*- coding: UTF-8 -*-
# 将一张图片分成九张,九宫格
import tkinter as tk
from PIL import Image 
import sys 
 
 
#先将 input image 填充为正方形 
def fill_image(image): 
 width, height = image.size 
 #选取长和宽中较大值作为新图片的 
 new_image_length = width if width > height else height 
 #生成新图片[白底] 
 new_image = Image.new(image.mode, (new_image_length, new_image_length), color='white') #注意这个函数! 
 #将之前的图粘贴在新图上,居中 
 if width > height:#原图宽大于高,则填充图片的竖直维度 #(x,y)二元组表示粘贴上图相对下图的起始位置,是个坐标点。 
 new_image.paste(image, (0, int((new_image_length - height) / 2))) 
 else: 
 new_image.paste(image, (int((new_image_length - width) / 2),0)) 
 return new_image 
 
# 分割图片 
def cut_image(image):
 width, height = image.size
 item_width = int(width / 3) #因为朋友圈一行放3张图。
 box_list = []
 # (left, upper, right, lower)
 for i in range(0,3):
 for j in range(0,3):
 #print((i*item_width,j*item_width,(i+1)*item_width,(j+1)*item_width))
 box = (j*item_width,i*item_width,(j+1)*item_width,(i+1)*item_width)
 box_list.append(box)
 image_list = [image.crop(box) for box in box_list]
 return image_list
 
#保存图片 
def save_images(image_list): 
 index = 1 
 for image in image_list: 
 image.save(str(index) + '.png', 'PNG') 
 index += 1 
 
 
# 点击按钮,实现图片分割
def cTofClicked():
 file_path=str(entryCd.get()) # 获取要进行分割的图片路径
 image = Image.open(file_path) 
 #image.show() 
 image = fill_image(image) 
 image_list = cut_image(image) 
 save_images(image_list) 
 labelcTof.config(text="九宫格图片已生,请在程序所在目录查看!")
 
# 窗体
top=tk.Tk()
top.title('九宫格图片生成器')
labelcTof=tk.Label(top,text="请输入要进行转换的图片路径:",height=4,\
 width=40,fg="blue") 
labelcTof.pack()
entryCd=tk.Entry(top,text='0') # 文本框,获取图片路径
entryCd.pack()
label_tip=tk.Label(top,text="请检查图片路径是否输入正确!",height=2,\
 width=40,fg="gray") 
label_tip.pack()
btnCal=tk.Button(top,text="点击生成九宫格图片",fg="red",bg="yellow",command=cTofClicked) # 点击回调函数
btnCal.pack()
 
top.mainloop() # 执行主循环

打包好的exe程序下载地址:python实现图片九宫格分割

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
使用Python制作获取网站目录的图形化程序
May 04 Python
Python使用PIL模块生成随机验证码
Nov 21 Python
python爬虫爬取淘宝商品信息(selenum+phontomjs)
Feb 24 Python
python实现超简单的视频对象提取功能
Jun 04 Python
Python使用POP3和SMTP协议收发邮件的示例代码
Apr 16 Python
python获取微信企业号打卡数据并生成windows计划任务
Apr 30 Python
Python Django 命名空间模式的实现
Aug 09 Python
python+jinja2实现接口数据批量生成工具
Aug 28 Python
postman传递当前时间戳实例详解
Sep 14 Python
python面向对象之类属性和类方法案例分析
Dec 30 Python
如何安装并在pycharm使用selenium的方法
Apr 30 Python
用Python写一个简易版弹球游戏
Apr 13 Python
django最快程序开发流程详解
Jul 19 #Python
python打印9宫格、25宫格等奇数格 满足横竖斜相加和相等
Jul 19 #Python
Django REST framework 视图和路由详解
Jul 19 #Python
Django使用模板后无法找到静态资源文件问题解决
Jul 19 #Python
Django模板Templates使用方法详解
Jul 19 #Python
python GUI图形化编程wxpython的使用
Jul 19 #Python
Django 外键的使用方法详解
Jul 19 #Python
You might like
php输出1000以内质数(素数)示例
2014/02/16 PHP
ThinkPHP基于PHPExcel导入Excel文件的方法
2014/10/15 PHP
ThinkPHP3.1.x修改成功与失败跳转页面的方法
2017/09/29 PHP
extJs 文本框后面加上说明文字+下拉列表选中值后触发事件
2009/11/27 Javascript
jquery 双色表格实现代码
2009/12/08 Javascript
20款效果非常棒的 jQuery 插件小结分享
2011/11/18 Javascript
javascript常用的方法整理
2015/08/20 Javascript
Prototype框架详解
2015/11/25 Javascript
纯javascript版日历控件
2016/11/24 Javascript
Vue过滤器的用法和自定义过滤器使用
2017/02/08 Javascript
原生JS实现N级菜单的代码
2017/05/21 Javascript
vue component组件使用方法详解
2017/07/14 Javascript
js数组实现权重概率分配
2017/09/12 Javascript
vue系列之requireJs中引入vue-router的方法
2018/07/18 Javascript
JS 正则表达式验证密码、邮箱格式的实例代码
2018/10/28 Javascript
微信小程序日历组件使用方法详解
2018/12/29 Javascript
浅谈小程序 setData学问多
2019/02/20 Javascript
JavaScript中关于base64的一些事
2019/05/06 Javascript
[26:50]2018完美盛典DOTA2表演赛
2018/12/17 DOTA
Mac OS X10.9安装的Python2.7升级Python3.3步骤详解
2013/12/04 Python
python实现在控制台输入密码不显示的方法
2015/07/02 Python
python 时间信息“2018-02-04 18:23:35“ 解析成字典形式的结果代码详解
2018/04/19 Python
Python装饰器模式定义与用法分析
2018/08/06 Python
Python3 解决读取中文文件txt编码的问题
2019/12/20 Python
执行Python程序时模块报错问题
2020/03/26 Python
python多线程实现同时执行两个while循环的操作
2020/05/02 Python
使用SQLAlchemy操作数据库表过程解析
2020/06/10 Python
Python列表推导式实现代码实例
2020/09/09 Python
python3中calendar返回某一时间点实例讲解
2020/11/18 Python
celery在python爬虫中定时操作实例讲解
2020/11/27 Python
Made in Design德国:设计师家具、灯具和装饰
2019/10/31 全球购物
美术教师自我鉴定
2014/02/12 职场文书
秋游活动策划方案
2014/02/16 职场文书
《欢乐的泼水节》教学反思
2014/04/22 职场文书
公司庆典欢迎词
2015/01/26 职场文书
2016年三严三实党课学习心得体会
2016/01/06 职场文书