通过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脚本
Apr 05 Python
Python处理PDF及生成多层PDF实例代码
Apr 24 Python
Python使用requests发送POST请求实例代码
Jan 25 Python
centos6.8安装python3.7无法import _ssl的解决方法
Sep 17 Python
让代码变得更易维护的7个Python库
Oct 09 Python
python2与python3的print及字符串格式化小结
Nov 30 Python
pyqt5 使用cv2 显示图片,摄像头的实例
Jun 27 Python
Python facenet进行人脸识别测试过程解析
Aug 16 Python
Python实现朴素贝叶斯的学习与分类过程解析
Aug 24 Python
python中的 zip函数详解及用法举例
Feb 16 Python
Django-rest-framework中过滤器的定制实例
Apr 01 Python
python pyecharts 实现一个文件绘制多张图
May 13 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页面防重复提交方法总结
2013/11/25 PHP
php上传图片到指定位置路径保存到数据库的具体实现
2013/12/30 PHP
laravel 执行迁移回滚示例
2019/10/23 PHP
一个很酷的拖动层的js类,兼容IE及Firefox
2009/06/23 Javascript
jquery1.4后 jqDrag 拖动 不可用
2010/02/06 Javascript
js setTimeout 常见问题小结
2013/08/13 Javascript
对之前写的jquery分页做下升级
2014/06/19 Javascript
Backbone.js中的集合详解
2015/01/14 Javascript
实现placeholder效果的方案汇总
2015/06/11 Javascript
通过XMLHttpRequest和jQuery实现ajax的几种方式
2015/08/28 Javascript
js判断当前页面在移动设备还是在PC端中打开
2016/01/06 Javascript
详解javascript实现自定义事件
2016/01/19 Javascript
JavaScript实现简单Tip提示框效果
2016/04/20 Javascript
Angular 路由route实例代码
2016/07/12 Javascript
js 颜色选择插件
2017/01/23 Javascript
详解用vue-cli来搭建vue项目和webpack
2017/04/20 Javascript
vuejs2.0子组件改变父组件的数据实例
2017/05/10 Javascript
NodeJS 实现手机短信验证模块阿里大于功能
2017/06/19 NodeJs
JavaScript实现离开页面前提示功能【附jQuery实现方法】
2017/09/26 jQuery
web3.js增加eth.getRawTransactionByHash(txhash)方法步骤
2018/03/15 Javascript
js canvas画布实现高斯模糊效果
2018/11/27 Javascript
js 计算图片内点个数的示例代码
2019/04/04 Javascript
jquery实现烟花效果(面向对象)
2020/03/10 jQuery
jQuery实现视频展示效果
2020/05/30 jQuery
在Gnumeric下使用Python脚本操作表格的教程
2015/04/14 Python
简单介绍Python中的round()方法
2015/05/15 Python
python3实现抓取网页资源的 N 种方法
2017/05/02 Python
Python基于socket模块实现UDP通信功能示例
2018/04/10 Python
Python使用pymongo库操作MongoDB数据库的方法实例
2019/02/22 Python
用Python中的turtle模块画图两只小羊方法
2019/04/09 Python
在django中自定义字段Field详解
2019/12/03 Python
css3 border-radius属性详解
2017/07/05 HTML / CSS
法学专业求职信范文
2015/03/19 职场文书
2015年七夕情人节活动方案
2015/05/06 职场文书
收入证明怎么写
2015/06/12 职场文书
大学生奶茶店创业计划书
2019/06/25 职场文书