用Python实现将一张图片分成9宫格的示例


Posted in Python onJuly 05, 2019

经常看到朋友圈或者空间里有朋友发布照片时,将朋友圈的照片切分为九宫格,参考了一些大神的博客资料,现整理如下;

将图片分拆成九宫格的思路:

读取图片->填充图片为正方形(fill_image函数)->将图片切分为9张(cut_image函数)->保存图片(save_image)->over

代码实现如下:

from PIL import Image
import sys
#将图片填充为正方形
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)
  box_list = []
  # (left, upper, right, lower)
  for i in range(0,3):#两重循环,生成9张图片基于原图的位置
    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) + '.jpg')
    index += 1
 
if __name__ == '__main__':
  file_path = "微信图片_20180809234441.jpg"
  image = Image.open(file_path)
  # image.show()
  image = fill_image(image)
  image_list = cut_image(image)
  save_images(image_list)

效果如下:

用Python实现将一张图片分成9宫格的示例

参考了二胖大神提供的思路,里面的逻辑很有趣:

1.开始相当于是拿一张白底的图片粘贴到了原图上;

2.切图的时候分成9宫格,的循环写的也非常漂亮。

3.代码中出现了很多次for循环的迭代使用:[image.crop(box) for box in box_list],以后自己也要多练习这种写法。

以上这篇用Python实现将一张图片分成9宫格的示例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python time模块用法实例详解
Sep 11 Python
Python写的服务监控程序实例
Jan 31 Python
Python中的XML库4Suite Server的介绍
Apr 14 Python
在Python中处理字符串之isdigit()方法的使用
May 18 Python
用Python实现斐波那契(Fibonacci)函数
Mar 25 Python
Django学习笔记之ORM基础教程
Mar 27 Python
详解Django之admin组件的使用和源码剖析
May 04 Python
python实现差分隐私Laplace机制详解
Nov 25 Python
Python tkinter布局与按钮间距设置方式
Mar 04 Python
Python抓包并解析json爬虫的完整实例代码
Nov 03 Python
Jupyter Notebook 远程访问配置详解
Jan 11 Python
python获取淘宝服务器时间的代码示例
Apr 22 Python
python获取txt文件词向量过程详解
Jul 05 #Python
Python 的字典(Dict)是如何存储的
Jul 05 #Python
关于Python 的简单栅格图像边界提取方法
Jul 05 #Python
Python3+Appium安装使用教程
Jul 05 #Python
Python叠加两幅栅格图像的实现方法
Jul 05 #Python
用vue.js组件模拟v-model指令实例方法
Jul 05 #Python
python买卖股票的最佳时机(基于贪心/蛮力算法)
Jul 05 #Python
You might like
解决dede生成静态页和动态页转换的一些问题,及火车采集入库生成动态的办法
2007/03/29 PHP
PHP Header失效的原因分析及解决方法
2016/11/16 PHP
PHP编程获取音频文件时长的方法【基于getid3类】
2017/04/20 PHP
php接口实现拖拽排序功能
2018/04/23 PHP
Laravel中如何轻松容易的输出完整的SQL语句
2020/07/26 PHP
javascript 面向对象继承
2009/11/26 Javascript
JSON.stringify 语法实例讲解
2012/03/14 Javascript
从jquery的过滤器.filter()方法想到的
2013/09/29 Javascript
使用微信内置浏览器点击下拉框出现页面乱跳转现象(iphone),该怎么办
2016/01/04 Javascript
JQuery ztree带筛选、异步加载实例讲解
2016/02/25 Javascript
ECHO.js 纯javascript轻量级延迟加载的实例代码
2016/05/24 Javascript
JavaScript实现省市县三级级联特效
2017/05/16 Javascript
js移动端图片压缩上传功能
2020/08/18 Javascript
原生JS实现列表子元素顺序反转的方法分析
2018/07/02 Javascript
微信小程序实现页面浮动导航
2019/01/28 Javascript
JS实现的贪吃蛇游戏案例详解
2019/05/01 Javascript
javascript异步处理与Jquery deferred对象用法总结
2019/06/04 jQuery
windows实现npm和cnpm安装步骤
2019/10/24 Javascript
python模拟登陆Tom邮箱示例分享
2014/01/13 Python
Python列表append和+的区别浅析
2015/02/02 Python
python使用pil生成图片验证码的方法
2015/05/08 Python
浅谈Python单向链表的实现
2015/12/24 Python
python实现指定字符串补全空格、前面填充0的方法
2018/11/16 Python
Python Tkinter模块 GUI 可视化实例
2019/11/20 Python
python实现程序重启和系统重启方式
2020/04/16 Python
python代码中怎么换行
2020/06/17 Python
纯css3实现鼠标经过图片显示描述的动画效果
2014/09/01 HTML / CSS
给同事的道歉信
2014/01/11 职场文书
管理专员自荐信
2014/01/26 职场文书
工地材料员岗位职责
2015/04/11 职场文书
2015年青年志愿者协会工作总结
2015/04/27 职场文书
2015年秋季小学开学典礼主持词
2015/07/16 职场文书
幼儿园大班教师随笔
2015/08/14 职场文书
利用Python将list列表写入文件并读取的方法汇总
2022/03/25 Python
MySQL 表锁定 LOCK和UNLOCK TABLES的 SQL语法
2022/04/18 MySQL
MySql统计函数COUNT的具体使用详解
2022/08/14 MySQL