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 不同对象比较大小示例探讨
Aug 21 Python
python基础教程之面向对象的一些概念
Aug 29 Python
Python安装使用命令行交互模块pexpect的基础教程
May 12 Python
python flask 多对多表查询功能
Jun 25 Python
Python实现学生成绩管理系统
Apr 05 Python
Python模拟自动存取款机的查询、存取款、修改密码等操作
Sep 02 Python
pycharm运行出现ImportError:No module named的解决方法
Oct 13 Python
python实现转盘效果 python实现轮盘抽奖游戏
Jan 22 Python
Python numpy数组转置与轴变换
Nov 15 Python
基于Python+QT的gui程序开发实现
Jul 03 Python
基于Python的图像阈值化分割(迭代法)
Nov 20 Python
使用python画出逻辑斯蒂映射(logistic map)中的分叉图案例
Dec 11 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
PHPLog php 程序调试追踪工具
2009/09/09 PHP
php实现的IMEI限制的短信验证码发送类
2015/05/05 PHP
phalcon model在插入或更新时会自动验证非空字段的解决办法
2016/12/29 PHP
解决php extension 加载顺序问题
2019/08/16 PHP
Thinkphp5框架中引入Markdown编辑器操作示例
2020/06/03 PHP
Jquery iframe内部出滚动条
2010/02/11 Javascript
jQuery旋转插件—rotate支持(ie/Firefox/SafariOpera/Chrome)
2013/01/16 Javascript
jquery中map函数与each函数的区别实例介绍
2014/06/23 Javascript
javascript学习笔记(七)Ajax和Http状态码
2014/10/08 Javascript
JQuery中$.each 和$(selector).each()的区别详解
2015/03/13 Javascript
由浅入深讲解Javascript继承机制与simple-inheritance源码分析
2015/12/13 Javascript
使用Angular缓存父页面数据的方法
2017/01/03 Javascript
JS获取字符对应的ASCII码实例
2017/09/10 Javascript
jQuery+CSS实现的table表格行列转置功能示例
2018/01/08 jQuery
vue router动态路由下让每个子路由都是独立组件的解决方案
2018/04/24 Javascript
koa2使用ejs和nunjucks作为模板引擎的使用
2018/11/27 Javascript
js实现网页随机验证码
2020/10/19 Javascript
vue项目中使用rem,在入口文件添加内容操作
2020/11/11 Javascript
python中mechanize库的简单使用示例
2014/01/10 Python
深入讨论Python函数的参数的默认值所引发的问题的原因
2015/03/30 Python
详解Python编程中基本的数学计算使用
2016/02/04 Python
Django 缓存配置Redis使用详解
2019/07/23 Python
centos7之Python3.74安装教程
2019/08/15 Python
详解基于Scrapy的IP代理池搭建
2020/09/29 Python
html5+css3之CSS中的布局与Header的实现
2014/11/21 HTML / CSS
html5 canvas 画图教程案例分析
2012/11/23 HTML / CSS
2014信息公开实施方案
2014/02/22 职场文书
技校毕业生自荐信
2014/06/03 职场文书
高中学生会竞选演讲稿
2014/08/25 职场文书
2014年单位法制宣传日活动总结
2014/11/01 职场文书
2014年办公室个人工作总结
2014/11/12 职场文书
护理工作个人总结
2015/03/03 职场文书
紧急通知
2015/04/17 职场文书
html+css合并表格边框的示例代码
2021/03/31 HTML / CSS
Python制作一个随机抽奖小工具的实现
2021/07/07 Python
Linux磁盘管理方法介绍
2022/06/01 Servers