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的Bottle框架的一些使用技巧介绍
Apr 08 Python
用Pygal绘制直方图代码示例
Dec 07 Python
python打包压缩、读取指定目录下的指定类型文件
Apr 12 Python
python ipset管理 增删白名单的方法
Jan 14 Python
pandas修改DataFrame列名的实现方法
Feb 22 Python
浅谈python的深浅拷贝以及fromkeys的用法
Mar 08 Python
python操作gitlab API过程解析
Dec 27 Python
解决Jupyter Notebook使用parser.parse_args出现错误问题
Apr 20 Python
python输出数学符号实例
May 11 Python
django 解决model中类写不到数据库中,数据库无此字段的问题
May 20 Python
Python3交互式shell ipython3安装及使用详解
Jul 11 Python
Python开发之QT解决无边框界面拖动卡屏问题(附带源码)
May 27 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模拟post提交数据方法汇总
2016/02/16 PHP
ThinkPHP使用Smarty第三方插件方法小结
2016/03/19 PHP
php版微信公众平台接口参数调试实现判断用户行为的方法
2016/09/23 PHP
ThinkPHP防止重复提交表单的方法实例分析
2018/05/10 PHP
Laravel框架学习笔记之批量更新数据功能
2019/05/30 PHP
php生成静态页面并实现预览功能
2019/06/27 PHP
完美解决JS中汉字显示乱码问题(已解决)
2006/12/27 Javascript
jquery选择器之基本过滤选择器详解
2014/01/27 Javascript
JavaScript实现选择框按比例拖拉缩放的方法
2015/08/04 Javascript
基于Echarts 3.19 制作常用的图形(非静态)
2016/05/19 Javascript
AngularJS使用ng-repeat指令实现下拉框
2016/08/23 Javascript
AngularJS 单元测试(一)详解
2016/09/21 Javascript
Angular实现的简单定时器功能示例
2017/12/28 Javascript
vue以组件或者插件的形式实现throttle或者debounce
2019/05/22 Javascript
Bootstrap实现模态框效果
2019/09/30 Javascript
JQuery事件委托(适用于给动态生成的脚本元素添加事件)
2020/02/01 jQuery
python分割文件的常用方法
2014/11/01 Python
速记Python布尔值
2017/11/09 Python
Python实现正弦信号的时域波形和频谱图示例【基于matplotlib】
2018/05/04 Python
Python闭包执行时值的传递方式实例分析
2018/06/04 Python
Python线上环境使用日志的及配置文件
2019/07/28 Python
Python模块汇总(常用第三方库)
2019/10/07 Python
python中if及if-else如何使用
2020/06/02 Python
python 制作简单的音乐播放器
2020/11/25 Python
python+opencv实现车道线检测
2021/02/19 Python
Waterford加拿大官方网站:世界著名的水晶杯品牌
2016/11/01 全球购物
美国最灵活的移动提供商:Tello
2017/07/18 全球购物
员工拾金不昧表扬信
2014/01/09 职场文书
拖鞋店创业计划书
2014/01/15 职场文书
我的求职择业计划书
2014/04/04 职场文书
机房搬迁方案
2014/05/01 职场文书
超市七夕促销活动方案
2014/08/28 职场文书
一般党员对照检查材料
2014/09/24 职场文书
群众路线教育实践活动总结
2014/10/30 职场文书
超市采购员岗位职责
2015/04/07 职场文书
HTML中的表单元素介绍
2022/02/28 HTML / CSS