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实现list反转实例汇总
Nov 11 Python
Python简单计算文件夹大小的方法
Jul 14 Python
python轻松查到删除自己的微信好友
Jan 10 Python
Python实现八大排序算法
Aug 13 Python
python数字图像处理之高级滤波代码详解
Nov 23 Python
python 获取字符串MD5值方法
May 29 Python
代码实例讲解python3的编码问题
Jul 08 Python
Python分析彩票记录并预测中奖号码过程详解
Jul 09 Python
Python自带的IDE在哪里
Jul 01 Python
python爬虫数据保存到mongoDB的实例方法
Jul 28 Python
解决tensorflow模型压缩的问题_踩坑无数,总算搞定
Mar 02 Python
深入解析NumPy中的Broadcasting广播机制
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
YII Framework框架教程之安全方案详解
2016/03/14 PHP
给大家分享几个常用的PHP函数
2017/01/15 PHP
PHP实现执行外部程序的方法详解
2017/08/17 PHP
js 控制下拉菜单刷新的方法
2013/03/03 Javascript
解决js中window.open弹出的是上次的缓存页面问题
2013/12/29 Javascript
JavaScript Promise启示录
2014/08/12 Javascript
jQuery实现的文字hover颜色渐变效果实例
2016/02/20 Javascript
Javascript函数中的arguments.callee用法实例分析
2016/09/16 Javascript
bootstrap警告框使用方法解析
2017/01/13 Javascript
Vue.js列表渲染绑定jQuery插件的正确姿势
2017/06/29 jQuery
JavaScript实现图片无缝滚动效果
2017/07/07 Javascript
Js利用Canvas实现图片压缩功能
2017/09/13 Javascript
jquery使用iscorll实现上拉、下拉加载刷新
2017/10/26 jQuery
微信小程序实现action-sheet弹出底部菜单功能【附源码下载】
2017/12/09 Javascript
搭建element-ui的Vue前端工程操作实例
2018/02/23 Javascript
JS中promise化微信小程序api
2018/04/12 Javascript
详解ajax的data参数错误导致页面崩溃
2018/04/30 Javascript
微信小程序实现的一键连接wifi功能示例
2019/04/24 Javascript
node.js通过Sequelize 连接MySQL的方法
2020/12/28 Javascript
[06:25]第二届DOTA2亚洲邀请赛主赛事第二天比赛集锦.mp4
2017/04/03 DOTA
[55:18]Liquid vs Chaos 2019国际邀请赛小组赛 BO2 第一场 8.15
2019/08/16 DOTA
Python的词法分析与语法分析
2013/05/18 Python
用matplotlib画等高线图详解
2017/12/14 Python
浅析Python3爬虫登录模拟
2018/02/07 Python
Pandas DataFrame数据的更改、插入新增的列和行的方法
2019/06/25 Python
Python使用正则实现计算字符串算式
2019/12/29 Python
python图片剪裁代码(图片按四个点坐标剪裁)
2020/03/10 Python
H5 canvas中width、height和style的宽高区别详解
2018/11/02 HTML / CSS
萌新的HTML5 入门指南
2020/11/06 HTML / CSS
国际旅客访问北美最大的汽车租赁提供商:Alamo Rent A Car
2018/06/13 全球购物
面向对象编程OOP的优点
2013/01/22 面试题
艺术设计专业求职自荐信
2014/05/19 职场文书
王金山在党的群众路线教育实践活动总结大会上的讲话稿
2014/10/25 职场文书
冬季作息时间调整通知
2015/04/24 职场文书
国际最新研究在陨石中发现DNA主要成分 或由陨石带来地球
2022/04/29 数码科技
windows10 家庭版下FTP服务器搭建教程
2022/08/05 Servers