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 相关文章推荐
使用Python的Treq on Twisted来进行HTTP压力测试
Apr 16 Python
Python二分查找详解
Sep 13 Python
python九九乘法表的实例
Sep 26 Python
Python之Class&Object用法详解
Dec 25 Python
python获取依赖包和安装依赖包教程
Feb 13 Python
Pycharm中import torch报错的快速解决方法
Mar 05 Python
Django高并发负载均衡实现原理详解
Apr 04 Python
Pytorch转tflite方式
May 25 Python
Python类super()及私有属性原理解析
Jun 15 Python
浅谈Python 参数与变量
Jun 20 Python
python利用递归方法实现求集合的幂集
Sep 07 Python
用Python监控你的朋友都在浏览哪些网站?
May 27 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 mkdir()定义和用法
2009/01/14 PHP
遍历echsop的region表形成缓存的程序实例代码
2016/11/01 PHP
PHP有序表查找之二分查找(折半查找)算法示例
2018/02/09 PHP
纯CSS打造的导航菜单(附jquery版)
2010/08/07 Javascript
jquery 元素控制(追加元素/追加内容)介绍及应用
2013/04/21 Javascript
Javascript 浮点运算的问题分析与解决方法
2013/08/27 Javascript
枚举的实现求得1-1000所有出现1的数字并计算出现1的个数
2013/09/10 Javascript
JS获取文本框,下拉框,单选框的值的简单实例
2014/02/26 Javascript
jQuery实现带分组数据的Table表头排序实例分析
2015/11/24 Javascript
基于javascript实现图片懒加载
2016/01/05 Javascript
BootStrap 图标icon符号图标glyphicons不正常显示的快速解决办法
2016/12/08 Javascript
微信端开发--登录小程序步骤
2017/01/11 Javascript
小程序点赞收藏功能的实现代码示例
2018/09/07 Javascript
详解jQuery获取特殊属性的值以及设置内容
2018/11/14 jQuery
基于node+vue实现简单的WebSocket聊天功能
2020/02/01 Javascript
vue绑定数字类型 value为数字的实例
2020/08/31 Javascript
解决Antd Table表头加Icon和气泡提示的坑
2020/11/17 Javascript
python的exec、eval使用分析
2017/12/11 Python
Python3.4实现远程控制电脑开关机
2018/02/22 Python
详解Python下Flask-ApScheduler快速指南
2018/11/04 Python
python numpy 按行归一化的实例
2019/01/21 Python
python装饰器简介---这一篇也许就够了(推荐)
2019/04/01 Python
解决Python设置函数调用超时,进程卡住的问题
2019/08/08 Python
python正则表达式实例代码
2020/03/03 Python
属性与 @property 方法让你的python更高效
2020/09/21 Python
python字典通过值反查键的实现(简洁写法)
2020/09/30 Python
预订旅游活动、景点和旅游:GetYourGuide
2019/09/29 全球购物
新加坡一家在线男士皮具品牌:Faire Leather Co.
2019/12/01 全球购物
下列程序在32位linux或unix中的结果是什么
2014/03/25 面试题
问卷调查计划书
2014/01/10 职场文书
领班岗位职责范文
2014/02/06 职场文书
党的群众路线教育实践活动个人对照检查材料(乡镇)
2014/11/05 职场文书
工作检讨书怎么写
2015/01/23 职场文书
学生党支部工作总结2015
2015/05/26 职场文书
银行资信证明
2015/06/17 职场文书
数据设计之权限的实现
2022/08/05 MySQL