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 相关文章推荐
sqlalchemy对象转dict的示例
Apr 22 Python
Python实现从url中提取域名的几种方法
Sep 26 Python
Python中文件操作简明介绍
Apr 13 Python
python奇偶行分开存储实现代码
Mar 19 Python
Python3.5 创建文件的简单实例
Apr 26 Python
python3读取csv和xlsx文件的实例
Jun 22 Python
python3注册全局热键的实现
Mar 22 Python
django Model层常用验证器及自定义验证器详解
Jul 15 Python
PyCharm最新激活码(2020/10/27全网最新)
Oct 27 Python
python爬取天气数据的实例详解
Nov 20 Python
Python命令行参数定义及需要注意的地方
Nov 30 Python
Python离线安装openpyxl模块的步骤
Mar 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
PHP检测接口Traversable用法详解
2017/12/29 PHP
php使用curl获取header检测开启GZip压缩的方法
2018/08/15 PHP
document.all还是document.getElementsByName?
2006/07/21 Javascript
由prototype_1.3.1进入javascript殿堂-类的初探
2006/11/06 Javascript
关于Jqzoom的使用心得 jquery放大镜效果插件
2010/04/12 Javascript
基于jquery的大众点评,分类导航实现代码
2011/08/23 Javascript
JavaScript创建一个欢迎cookie弹出窗实现代码
2013/03/15 Javascript
jQuery实现向下滑出的平滑下拉菜单效果
2015/08/21 Javascript
jQuery控制div实现随滚动条滚动效果
2016/06/07 Javascript
JS前端笔试题分析
2016/12/19 Javascript
jQuery读取XML文件的方法示例
2017/02/03 Javascript
详解基于Angular4+ server render(服务端渲染)开发教程
2017/08/28 Javascript
Angularjs使用过滤器完成排序功能
2017/09/20 Javascript
Three.js利用性能插件stats实现性能监听的方法
2017/09/25 Javascript
使用RN Animated做一个“添加购物车”动画的方法
2018/09/12 Javascript
vxe-table vue table 表格组件功能
2019/05/26 Javascript
react PropTypes校验传递的值操作示例
2020/04/28 Javascript
完美解决通过IP地址访问VUE项目的问题
2020/07/18 Javascript
Python中关键字is与==的区别简述
2014/07/31 Python
浅谈Python中用datetime包进行对时间的一些操作
2016/06/23 Python
浅谈python jieba分词模块的基本用法
2017/11/09 Python
python模仿网页版微信发送消息功能
2018/02/24 Python
python 将md5转为16字节的方法
2018/05/29 Python
pycharm重置设置,恢复默认设置的方法
2018/10/22 Python
Python 离线工作环境搭建的方法步骤
2019/07/29 Python
Django shell调试models输出的SQL语句方法
2019/08/29 Python
django中使用事务及接入支付宝支付功能
2019/09/15 Python
Python 使用双重循环打印图形菱形操作
2020/08/09 Python
Python中random模块常用方法的使用教程
2020/10/04 Python
宝拉珍选英国官网:Paula’s Choice英国
2019/05/29 全球购物
消防安全责任书
2014/04/14 职场文书
个人先进事迹总结
2015/02/26 职场文书
2015学校师德师风工作总结
2015/04/22 职场文书
2016年大学生寒假社会实践心得体会
2015/10/09 职场文书
歌咏比赛口号大全
2015/12/25 职场文书
幼儿园大班教师评语
2019/06/21 职场文书