通过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处理cookie详解
Feb 07 Python
Python中使用md5sum检查目录中相同文件代码分享
Feb 02 Python
Python实现字符串格式化的方法小结
Feb 20 Python
Python中类的初始化特殊方法
Dec 01 Python
Python读取mat文件,并保存为pickle格式的方法
Oct 23 Python
python pands实现execl转csv 并修改csv指定列的方法
Dec 12 Python
Django框架首页和登录页分离操作示例
May 28 Python
python切片的步进、添加、连接简单操作示例
Jul 11 Python
Python 实现使用空值进行赋值 None
Mar 12 Python
Python filter过滤器原理及实例应用
Aug 18 Python
pytorch学习教程之自定义数据集
Nov 10 Python
解决PyCharm无法使用lxml库的问题(图解)
Dec 22 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中批量替换文件名的实现代码
2011/07/20 PHP
php中经典方法实现判断多维数组是否为空
2011/10/23 PHP
thinkphp实现发送邮件密码找回功能实例
2014/12/01 PHP
php实现根据词频生成tag云的方法
2015/04/17 PHP
php使用QueryList轻松采集js动态渲染页面方法
2018/09/11 PHP
PHP静态方法和静态属性及常量属性的区别与介绍
2019/03/22 PHP
js 屏蔽鼠标右键脚本附破解方法
2009/12/03 Javascript
使用jQuery validate 验证注册表单实例演示
2013/03/25 Javascript
用Javascript来生成ftp脚本的小例子
2013/07/03 Javascript
jquery中object对象循环遍历的方法
2015/12/18 Javascript
jquery.validate提示错误信息位置方法
2016/01/22 Javascript
有关easyui-layout中的收缩层无法显示标题的解决办法
2016/05/10 Javascript
基于BootStrap Metronic开发框架经验小结【一】框架总览及菜单模块的处理
2016/05/12 Javascript
详解vue-router 路由元信息
2017/09/13 Javascript
详解webpack-dev-server使用http-proxy解决跨域问题
2018/01/13 Javascript
使用vue完成微信公众号网页小记(推荐)
2019/04/28 Javascript
vue-video-player实现实时视频播放方式(监控设备-rtmp流)
2020/08/10 Javascript
python 多线程应用介绍
2012/12/19 Python
python提取页面内url列表的方法
2015/05/25 Python
约瑟夫问题的Python和C++求解方法
2015/08/20 Python
python 列表,数组,矩阵两两转换tolist()的实例
2018/04/04 Python
在pycharm上mongodb配置及可视化设置方法
2018/11/30 Python
Python Django基础二之URL路由系统
2019/07/18 Python
Python调用Windows命令打印文件
2020/02/07 Python
解决pytorch 模型复制的一些问题
2021/03/03 Python
简约控的天堂:The Undone
2016/12/21 全球购物
Carter’s OshKosh加拿大:购买婴幼儿服装和童装
2018/11/27 全球购物
理肤泉英国官网:La Roche-Posay英国
2019/01/14 全球购物
女性时尚在线:IVRose
2019/02/23 全球购物
电子信息专业学生自荐信
2013/11/09 职场文书
广告设计专业自荐信范文
2013/11/14 职场文书
《每逢佳节倍思亲》教后反思
2014/04/19 职场文书
一份恶作剧的检讨书
2014/09/13 职场文书
公务员四风问题对照检查材料整改措施
2014/09/26 职场文书
《检阅》教学反思
2016/02/22 职场文书
python3实现常见的排序算法(示例代码)
2021/07/04 Python