Python切图九宫格的实现方法


Posted in Python onOctober 10, 2019

本文介绍了Python切图九宫格的实现方法,分享给大家,具体如下

Python切图九宫格的实现方法

# -*- coding: utf-8 -*-
'''
将一张图片填充为正方形后切为9张图
'''
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('./python'+str(index) + '.png', 'PNG')
    index += 1

if __name__ == '__main__':
  file_path = "python.jpeg"
  image = Image.open(file_path)
  #image.show()
  image = fill_image(image)
  image_list = cut_image(image)
  save_images(image_list)

Python切图九宫格的实现方法 Python切图九宫格的实现方法 Python切图九宫格的实现方法

Python切图九宫格的实现方法 Python切图九宫格的实现方法 Python切图九宫格的实现方法

Python切图九宫格的实现方法 Python切图九宫格的实现方法 Python切图九宫格的实现方法

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

Python 相关文章推荐
python3编写C/S网络程序实例教程
Aug 25 Python
Python基于smtplib实现异步发送邮件服务
May 28 Python
Python利用IPython提高开发效率
Aug 10 Python
Java及python正则表达式详解
Dec 27 Python
python2.7 json 转换日期的处理的示例
Mar 07 Python
Python基于递归和非递归算法求两个数最大公约数、最小公倍数示例
May 21 Python
使用Python来开发微信功能
Jun 13 Python
Django Aggregation聚合使用方法解析
Aug 01 Python
Python绘制三角函数图(sin\cos\tan)并标注特定范围的例子
Dec 04 Python
Python魔术方法专题
Jun 19 Python
Python web框架(django,flask)实现mysql数据库读写分离的示例
Nov 18 Python
详解matplotlib绘图样式(style)初探
Feb 03 Python
python 3.7.4 安装 opencv的教程
Oct 10 #Python
Django实现auth模块下的登录注册与注销功能
Oct 10 #Python
导入tensorflow时报错:cannot import name 'abs'的解决
Oct 10 #Python
关于pycharm中pip版本10.0无法使用的解决办法
Oct 10 #Python
超实用的 30 段 Python 案例
Oct 10 #Python
使用浏览器访问python写的服务器程序
Oct 10 #Python
详解Python time库的使用
Oct 10 #Python
You might like
php打印一个边长为N的实心和空心菱型的方法
2015/03/02 PHP
[原创]PHP实现字节数Byte转换为KB、MB、GB、TB的方法
2017/08/31 PHP
在Javascript中为String对象添加trim,ltrim,rtrim方法
2006/09/22 Javascript
JavaScript打字小游戏代码
2011/12/26 Javascript
可选择和输入的下拉列表框示例
2013/11/05 Javascript
页面内容排序插件jSort使用方法
2015/10/10 Javascript
Jquery插件仿百度搜索关键字自动匹配功能
2016/05/11 Javascript
Bootstrap三种表单布局的使用方法
2016/06/21 Javascript
Angularjs 制作购物车功能实例代码
2016/09/14 Javascript
最常见的左侧分类菜单栏jQuery实现代码
2016/11/28 Javascript
如何使用Bootstrap 按钮实例详解
2017/03/29 Javascript
JScript实现地址选择功能
2017/08/15 Javascript
使用jQuery实现简单的tab框实例
2017/08/22 jQuery
基于 Vue.js 之 iView UI 框架非工程化实践记录(推荐)
2017/11/21 Javascript
SpringBoot+Vue前后端分离,使用SpringSecurity完美处理权限问题的解决方法
2018/01/09 Javascript
详解Vue.js自定义tipOnce指令用法实例
2018/12/19 Javascript
JavaScript实时更新当前的时间的示例代码
2020/07/15 Javascript
教你如何在Django 1.6中正确使用 Signal
2014/06/22 Python
python实现从字符串中找出字符1的位置以及个数的方法
2014/08/25 Python
Python使用Matplotlib实现雨点图动画效果的方法
2017/12/23 Python
解决Python2.7读写文件中的中文乱码问题
2018/04/12 Python
python如何生成各种随机分布图
2018/08/27 Python
在pycharm上mongodb配置及可视化设置方法
2018/11/30 Python
pyqt5 tablewidget 利用线程动态刷新数据的方法
2019/06/17 Python
Django  ORM 练习题及答案
2019/07/19 Python
python实现超市商品销售管理系统
2019/10/25 Python
python 5个顶级异步框架推荐
2020/09/09 Python
24个canvas基础知识小结
2014/12/17 HTML / CSS
世界上第一个水枕头:Mediflow
2018/12/06 全球购物
TCP/IP的分层模型
2013/10/27 面试题
学校安全教育制度
2014/01/31 职场文书
晨会主持词
2014/03/17 职场文书
大队委竞选演讲稿
2014/04/28 职场文书
乡镇领导班子批评与自我批评材料
2014/09/23 职场文书
微信小程序实现拍照和相册选取图片
2021/05/09 Javascript
2021好看的国漫排行榜前十名 《完美世界》上榜,《元龙》排名第一
2022/03/18 国漫