python制作朋友圈九宫格图片


Posted in Python onNovember 03, 2019

本文实例为大家分享了python朋友圈九宫格图片的具体制作代码,供大家参考,具体内容如下

将一张图片,切分成九宫格的样式:

原图:

python制作朋友圈九宫格图片

# -*- coding: UTF-8 -*-
from PIL import Image
import sys
import os
 
__author__ = 'kandy'
 
#当前文件所在文件夹
DIR_NAME = os.path.dirname( os.path.abspath(__file__) )
 
#填充新的image
def fill_image(image):
 width, height = image.size
 print('width:{%d}, height:{%d}' % (width, height))
 
 _length = width
 if height > width:
  _length = height
 
 new_image = Image.new(image.mode, (_length, _length), color='white')
 
 if width > height:
  new_image.paste(image, (0, int((_length - height) / 2)))
 else:
  new_image.paste(image, (int((_length - width) / 2), 0))
 return new_image
 
#裁剪image
def cut_image(image):
 width, height = image.size
 _width = int(width / 3)
 print('_width:{%d}' % _width)
 
 box_list = []
 
 # (left, top, right, bottom)
 for i in range(0, 3):
  for j in range(0, 3):
   print('i:{%d}, j:{%d}' % (i, j))
   box = (j*_width, i*_width, (j+1)*_width, (i+1)*_width)
   box_list.append(box)
   image_list = [image.crop(box) for box in box_list]
 return image_list
 
#将image列表的里面的图片保存
def save_images(image_list): 
 index = 1 
 #创建result文件夹
 res_dir = os.path.join(DIR_NAME, 'result')
 if not os.path.exists(res_dir):
  os.mkdir(res_dir)
 
 for image in image_list:
  new_name = os.path.join(res_dir, str(index) + '.png')
  image.save(new_name, 'PNG') 
  index += 1 
 print('图片保存完毕!')
 
 
if __name__ == '__main__': 
 file_path = os.path.join(DIR_NAME, '123.jpg')
 image = Image.open(file_path)
 #image.show()
 image = fill_image(image)
 #
 image_list = cut_image(image)
 #
 save_images(image_list)
 print('程序结束!')

切图后,拿去发朋友圈吧:

python制作朋友圈九宫格图片

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

Python 相关文章推荐
跟老齐学Python之眼花缭乱的运算符
Sep 14 Python
python比较2个xml内容的方法
May 11 Python
Python图片裁剪实例代码(如头像裁剪)
Jun 21 Python
python3写爬取B站视频弹幕功能
Dec 22 Python
selenium + python 获取table数据的示例讲解
Oct 13 Python
pytorch permute维度转换方法
Dec 14 Python
pytorch 模拟关系拟合——回归实例
Jan 14 Python
django在开发中取消外键约束的实现
May 20 Python
解决Python paramiko 模块远程执行ssh 命令 nohup 不生效的问题
Jul 14 Python
Python 制作查询商品历史价格的小工具
Oct 20 Python
python第三方网页解析器 lxml 扩展库与 xpath 的使用方法
Apr 06 Python
Python数组变形的几种实现方法
May 30 Python
python使用yield压平嵌套字典的超简单方法
Nov 02 #Python
基于python实现从尾到头打印链表
Nov 02 #Python
pandas 空数据处理方法详解
Nov 02 #Python
python pyinstaller打包exe报错的解决方法
Nov 02 #Python
python自动生成model文件过程详解
Nov 02 #Python
python__name__原理及用法详解
Nov 02 #Python
简单了解python中的f.b.u.r函数
Nov 02 #Python
You might like
关于二级目录拖拽排序的实现(源码示例下载)
2013/04/26 PHP
对PHP新手的一些建议(PHP学习经验总结)
2014/08/20 PHP
php获取远程文件大小
2015/10/20 PHP
MAC下通过改apache配置文件切换php多版本的方法
2017/04/26 PHP
php实现和c#一致的DES加密解密实例
2017/07/24 PHP
浅析PHP echo 和 print 语句
2020/06/30 PHP
jQuery中:checkbox选择器用法实例
2015/01/03 Javascript
jquery实现点击弹出可放大居中及关闭的对话框(附demo源码下载)
2016/05/10 Javascript
AngularJs实现分页功能不带省略号的代码
2016/05/30 Javascript
bootstrap的3级菜单样式,支持母版页保留打开状态实现方法
2016/11/10 Javascript
微信小程序 数组(增,删,改,查)等操作实例详解
2017/01/05 Javascript
微信小程序中form 表单提交和取值实例详解
2017/04/20 Javascript
jQuery实现select下拉框获取当前选中文本、值、索引
2017/05/08 jQuery
Vue+Element使用富文本编辑器的示例代码
2017/08/14 Javascript
BootStrap表单验证中的非Submit类型按钮点击时触发验证的坑
2019/09/05 Javascript
layui问题之渲染数据表格时,仅出现10条数据的解决方法
2019/09/12 Javascript
VueCli生产环境打包部署跨域失败的解决
2020/11/13 Javascript
python实现人人网登录示例分享
2014/01/19 Python
Python深入学习之上下文管理器
2014/08/31 Python
Django管理员账号和密码忘记的完美解决方法
2018/12/06 Python
pytorch中的自定义反向传播,求导实例
2020/01/06 Python
Python阶乘求和的代码详解
2020/02/14 Python
python3 deque 双向队列创建与使用方法分析
2020/03/24 Python
python json.dumps中文乱码问题解决
2020/04/01 Python
Python日志:自定义输出字段 json格式输出方式
2020/04/27 Python
对Python 字典元素进行删除的方法
2020/07/31 Python
Python collections模块的使用方法
2020/10/09 Python
中介业务员岗位职责
2014/04/09 职场文书
人力资源管理专业毕业生自荐书
2014/05/25 职场文书
食品安全宣传标语
2014/06/07 职场文书
反腐倡廉标语
2014/06/24 职场文书
企业消防安全责任书
2014/07/23 职场文书
调研汇报材料范文
2014/08/17 职场文书
2014年财务科工作总结
2014/11/11 职场文书
企业爱心捐款倡议书
2015/04/27 职场文书
酒店温馨提示语
2015/07/14 职场文书