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正则表达式 re模块使用说明
May 19 Python
简单的Python抓taobao图片爬虫
Oct 26 Python
python从入门到精通(DAY 1)
Dec 20 Python
在CentOS上配置Nginx+Gunicorn+Python+Flask环境的教程
Jun 07 Python
Python在图片中添加文字的两种方法
Apr 29 Python
python绘制热力图heatmap
Mar 23 Python
Pandas读取并修改excel的示例代码
Feb 17 Python
浅谈python中get pass用法
Mar 19 Python
python3.7 利用函数os pandas利用excel对文件名进行归类
Sep 29 Python
基于Python获取城市近7天天气预报
Nov 26 Python
Python编写万花尺图案实例
Jan 03 Python
Python还能这么玩之用Python修改了班花的开机密码
Jun 04 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
图象函数中的中文显示
2006/10/09 PHP
smarty获得当前url的方法分享
2014/02/14 PHP
php实现连接access数据库并转txt写入的方法
2017/02/08 PHP
Mozilla 表达式 __noSuchMethod__
2009/04/05 Javascript
JavaScript 实现模态对话框 源代码大全
2009/05/02 Javascript
JavaScript 函数式编程的原理
2009/10/16 Javascript
Tips 带三角可关闭的文字提示
2010/10/06 Javascript
基于JQuery的访问WebService的代码(可访问Java[Xfire])
2010/11/19 Javascript
js 对小数加法精度处理示例说明
2013/12/27 Javascript
javascript 密码框防止用户粘贴和复制的实现代码
2014/02/17 Javascript
再谈Jquery Ajax方法传递到action(补充)
2014/05/12 Javascript
javascript实现网页中涉及的简易运动(改变宽高、透明度、位置)
2015/11/29 Javascript
精通JavaScript的this关键字
2020/05/28 Javascript
js实现当鼠标移到表格上时显示这一格全部内容的代码
2016/06/12 Javascript
Vue.js列表渲染绑定jQuery插件的正确姿势
2017/06/29 jQuery
为什么使用koa2搭建微信第三方公众平台的原因
2018/05/16 Javascript
微信小程序实现留言板功能
2018/11/02 Javascript
js实现左右轮播图
2020/01/09 Javascript
python在windows下实现备份程序实例
2014/07/04 Python
Python安装Imaging报错:The _imaging C module is not installed问题解决方法
2014/08/22 Python
python实现mysql的单引号字符串过滤方法
2015/11/14 Python
Python中判断子串存在的性能比较及分析总结
2019/06/23 Python
在VS2017中用C#调用python脚本的实现
2019/07/31 Python
基于python进行抽样分布描述及实践详解
2019/09/02 Python
Scrapy框架基本命令与settings.py设置
2020/02/06 Python
Python @property及getter setter原理详解
2020/03/31 Python
Python判断字符串是否为空和null方法实例
2020/04/26 Python
在pycharm中关掉ipython console/PyDev操作
2020/06/09 Python
用html5实现语音搜索框的方法
2014/03/18 HTML / CSS
Nili Lotan官网:Nili Lotan同名品牌
2018/01/07 全球购物
编程实现去掉XML的重复结点
2014/05/28 面试题
酒店保洁主管岗位职责
2013/11/28 职场文书
酒店餐厅2014重阳节活动策划方案
2014/09/16 职场文书
2014县委书记四风对照检查材料思想汇报
2014/09/21 职场文书
党员干部学习三严三实心得体会
2016/01/05 职场文书
Android实现图片九宫格
2022/06/28 Java/Android