通过PYTHON来实现图像分割详解


Posted in Python onJune 26, 2019

程序思路:

此次程序主要是利用PIL(Python Image Libraty)这库,来进行图片的处理。

PIL是一个功能非常强大的python图像处理标准库,但由于PIL只支持python2.7。如今很多python程序员都使用python3.x,所以PIL在之前的基础上分离出来一个分支,另外创建一个Pillow库,以便支持python3.x, 本程序在使用之前确保已经安装了Pillow库。

程序首先把你要分隔的图像读取到一个变量中,然后我们定义了一个 fill_image() 方法,用来填充图像让原本大小不一的图像,重新变为一个长宽相同的正方形图像,方便之后处理。

通过 fill_image() 方法,我们就会得到新的一张正方形图像。随后我们在利用 cut_image() 方法,把图像分隔成 9 张,因为微信朋友圈最多发 9张图片。

from PIL import Image
import sys
#先将 input image 填充为正方形
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) #因为朋友圈一行放3张图。
  box_list = []
  # (left, upper, right, lower)
  for i in range(0,3):
    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) + '.png', 'PNG')
    index += 1
if __name__ == '__main__':
  file_path = "2.jpg" #图片保存的地址
  image = Image.open(file_path)
  #image.show()
  image_new = fill_image(image)
  image_list = cut_image(image_new)
  save_images(image_list)

如果有兴趣的话可以看一看Pillow库的使用

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

Python 相关文章推荐
python在windows命令行下输出彩色文字的方法
Mar 19 Python
Python基于pygame实现的弹力球效果(附源码)
Nov 11 Python
python+requests+unittest API接口测试实例(详解)
Jun 10 Python
Python 模拟员工信息数据库操作的实例
Oct 23 Python
我们为什么要减少Python中循环的使用
Jul 10 Python
python脚本执行CMD命令并返回结果的例子
Aug 14 Python
python kafka 多线程消费者&手动提交实例
Dec 21 Python
Pytorch 数据加载与数据预处理方式
Dec 31 Python
pycharm中leetcode插件使用图文详解
Dec 07 Python
python实现语音常用度量方法的代码详解
May 25 Python
基于Python和openCV实现图像的全景拼接详细步骤
Oct 05 Python
python百行代码实现汉服圈图片爬取
Nov 23 Python
Flask模板引擎之Jinja2语法介绍
Jun 26 #Python
如何使用Python实现自动化水军评论
Jun 26 #Python
详解用pyecharts Geo实现动态数据热力图城市找不到问题解决
Jun 26 #Python
Python 数据可视化pyecharts的使用详解
Jun 26 #Python
python 实现交换两个列表元素的位置示例
Jun 26 #Python
python之信息加密题目详解
Jun 26 #Python
实例详解Python模块decimal
Jun 26 #Python
You might like
用PHP实现小型站点广告管理(修正版)
2006/10/09 PHP
mysql limit查询优化分析
2008/11/12 PHP
php zend 相对路径问题
2009/01/12 PHP
php fsockopen中多线程问题的解决办法[翻译]
2011/11/09 PHP
php中的boolean(布尔)类型详解
2013/10/28 PHP
YII路径的用法总结
2014/07/09 PHP
PHP中register_shutdown_function函数的基础介绍与用法详解
2017/11/28 PHP
将CKfinder整合进CKEditor3.0的新方法
2010/01/10 Javascript
javascript replace()正则替换实现代码
2010/02/26 Javascript
用js实现输入提示(自动完成)的实例代码
2013/06/14 Javascript
jquery用offset()方法获得元素的xy坐标
2014/09/06 Javascript
jQuery中:input选择器用法实例
2015/01/03 Javascript
jQuery简单几行代码实现tab切换
2015/03/10 Javascript
微信小程序开发(一) 微信登录流程详解
2017/01/11 Javascript
详解vue中v-model和v-bind绑定数据的异同
2020/08/10 Javascript
vue配置多代理服务接口地址操作
2020/09/08 Javascript
Python和GO语言实现的消息摘要算法示例
2015/03/10 Python
Python实现单词拼写检查
2015/04/25 Python
Python实现扫描局域网活动ip(扫描在线电脑)
2015/04/28 Python
Python获取某一天是星期几的方法示例
2017/01/17 Python
Python实现矩阵转置的方法分析
2017/11/24 Python
Python实现调度算法代码详解
2017/12/01 Python
python版本五子棋的实现代码
2018/12/11 Python
基于Python在MacOS上安装robotframework-ride
2018/12/28 Python
djang常用查询SQL语句的使用代码
2019/02/15 Python
python二分法查找算法实现方法【递归与非递归】
2019/12/06 Python
利用CSS3实现自定义滚动条代码分享
2016/08/18 HTML / CSS
canvas实现扭蛋机动画效果的示例代码
2018/10/17 HTML / CSS
国际书籍零售商:Wordery
2017/11/01 全球购物
新西兰优惠网站:Treat Me
2019/07/04 全球购物
大学生关于奋斗的演讲稿
2014/01/09 职场文书
迎新晚会主持词
2014/03/24 职场文书
Python 多线程之threading 模块的使用
2021/04/14 Python
Python机器学习应用之基于线性判别模型的分类篇详解
2022/01/18 Python
MYSQL如何查看进程和kill进程
2022/03/13 MySQL
SQL Server 忘记密码以及重新添加新账号
2022/04/26 SQL Server