python实现将文件夹内的每张图片批量分割成多张


Posted in Python onJuly 22, 2019

一、说在前面

       需求:有一张长为960,宽为96的图片,需要将其分割成10张96*96的图片并存放在另外一个文件夹下,通过手工分割耗时且不规范,选择python写一个简单的程序完成。

二、源码

# -*- coding: utf-8 -*-
"""
Created on Thu Aug 23 18:19:09 2018
@author: Administrator
"""
 
import os
from PIL import Image
 
# 切割图片
def splitimage(src, rownum, colnum, dstpath):
 img = Image.open(src)
 w, h = img.size
 if rownum <= h and colnum <= w:
 print('Original image info: %sx%s, %s, %s' % (w, h, img.format, img.mode))
 print('图片切割')
 
 s = os.path.split(src)
 if dstpath == '':
  dstpath = s[0]
 fn = s[1].split('.')
 basename = fn[0]
 ext = fn[-1]
 
 num = 0
 rowheight = h // rownum
 colwidth = w // colnum
 for r in range(rownum):
  for c in range(colnum):
  box = (c * colwidth, r * rowheight, (c + 1) * colwidth, (r + 1) * rowheight)
  img.crop(box).save(os.path.join(dstpath, basename + '_' + str(num) + '.' + ext), ext)
  num = num + 1
 
 print('共生成 %s 张小图片。' % num)
 else:
 print('error')
 
# 创建文件夹
def mkdir(path):
 # 去除首位空格
 path = path.strip()
 # 去除尾部 \ 符号
 path = path.rstrip("\\")
 
 # 判断路径是否存在
 # 存在 True
 # 不存在 False
 isExists = os.path.exists(path)
 
 # 判断结果
 if not isExists:
 os.makedirs(path)
 print (path+' 创建成功')
 return True
 else:
 print (path + ' 目录已存在')
 return False
 
 
folder = r'C:/Users/Administrator/Desktop/testresults' # 存放图片的文件夹
path = os.listdir(folder)
# print(path)
 
for each_bmp in path: # 批量操作
 first_name, second_name = os.path.splitext(each_bmp)
 each_bmp = os.path.join(folder, each_bmp)
 src = each_bmp
 print(src)
 print(first_name)
 # 定义要创建的目录
 mkpath = "C:/Users/Administrator/Desktop/results/"+ first_name
 # 调用函数
 mkdir(mkpath)
 if os.path.isfile(src):
  dstpath = mkpath
  if (dstpath == '') or os.path.exists(dstpath):
  row = int(1) # 切割行数
  col = int(10) # 切割列数
  if row > 0 and col > 0:
   splitimage(src, row, col, dstpath)
  else:
   print('无效的')
  else:
  print('图片保存目录 %s 不存在!' % dstpath)
 else:
  print('图片文件 %s 不存在!' % src)

三、写在后面

这里定义了切割行数和列数:

python实现将文件夹内的每张图片批量分割成多张

如果需要将图片更改尺寸,可以简单的使用PIL库中的resize()函数,代码如下:

from PIL import Image
 
for i in range(1,100):
 img = Image.open("C:/Users/Administrator/Desktop/test_results/"+str(i)+".png")
 img = img.convert("L")
 img = img.resize((960,96))
 
 img.save("C:/Users/Administrator/Desktop/test_results/"+str(i)+".png", "PNG")

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

Python 相关文章推荐
使用Python绘制图表大全总结
Feb 11 Python
python之消除前缀重命名的方法
Oct 21 Python
python垃圾回收机制(GC)原理解析
Dec 30 Python
Python面向对象之继承原理与用法案例分析
Dec 31 Python
python读取与处理netcdf数据方式
Feb 14 Python
浅谈python print(xx, flush = True) 全网最清晰的解释
Feb 21 Python
jupyter notebook oepncv 显示一张图像的实现
Apr 24 Python
在pycharm中debug 实时查看数据操作(交互式)
Jun 09 Python
Python爬取梨视频的示例
Jan 29 Python
DjangoRestFramework 使用 simpleJWT 登陆认证完整记录
Jun 22 Python
Pygame Draw绘图函数的具体使用
Nov 17 Python
Python OpenCV实现图像模板匹配详解
Apr 07 Python
使用APScheduler3.0.1 实现定时任务的方法
Jul 22 #Python
Python定时任务APScheduler的实例实例详解
Jul 22 #Python
基于多进程中APScheduler重复运行的解决方法
Jul 22 #Python
django云端留言板实例详解
Jul 22 #Python
python实现图片中文字分割效果
Jul 22 #Python
django用户登录验证的完整示例代码
Jul 21 #Python
Python Threading 线程/互斥锁/死锁/GIL锁
Jul 21 #Python
You might like
实例讲解php将字符串输出到HTML
2019/01/27 PHP
php+lottery.js实现九宫格抽奖功能
2019/07/21 PHP
php设计模式之备忘模式分析【星际争霸游戏案例】
2020/03/24 PHP
单击某一段文字改写文本颜色
2014/06/06 Javascript
jQuery制作简洁的图片轮播效果
2015/04/03 Javascript
详解AngularJS中的表达式使用
2015/06/16 Javascript
自定义刻度jQuery进度条及插件
2015/09/02 Javascript
js选择器全面解析
2016/06/27 Javascript
jQuery用FormData实现文件上传的方法
2016/11/21 Javascript
jQuery+ajax实现局部刷新的两种方法
2017/06/08 jQuery
JavaScript表单即时验证 验证不成功不能提交
2017/08/31 Javascript
jQuery 实现左右两侧菜单添加、移除功能
2018/01/02 jQuery
vue 动态表单开发方法案例详解
2019/12/02 Javascript
JS 设计模式之:工厂模式定义与实现方法浅析
2020/05/06 Javascript
Vue实现跑马灯效果
2020/05/25 Javascript
浅析Python中的for 循环
2016/06/09 Python
Python中异常重试的解决方案详解
2017/05/05 Python
Python下调用Linux的Shell命令的方法
2018/06/12 Python
python dataframe 输出结果整行显示的方法
2018/06/14 Python
使用sklearn进行对数据标准化、归一化以及将数据还原的方法
2018/07/11 Python
解决pandas .to_excel不覆盖已有sheet的问题
2018/12/10 Python
python mac下安装虚拟环境的图文教程
2019/04/12 Python
Pandas时间序列重采样(resample)方法中closed、label的作用详解
2019/12/10 Python
Html5百叶窗效果的示例代码
2017/12/11 HTML / CSS
Tory Burch德国官网:美国时尚生活品牌
2018/01/03 全球购物
英国最大的LED专业零售商:Led Hut
2018/03/16 全球购物
C语言变量的命名规则都有哪些
2013/12/27 面试题
工作过失检讨书
2014/02/23 职场文书
安全标语大全
2014/06/10 职场文书
网络文明传播志愿者活动方案
2014/08/20 职场文书
公司员工宿舍管理制度
2015/08/03 职场文书
安全生产奖惩制度
2015/08/06 职场文书
八年级作文之友谊
2019/12/02 职场文书
如何用python反转图片,视频
2021/04/24 Python
tensorflow+k-means聚类简单实现猫狗图像分类的方法
2021/04/28 Python
使用Cargo工具高效创建Rust项目
2022/08/14 Javascript