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('不合法的行列切割参数!') src = input('请输入图片文件路径:')
if os.path.isfile(src):
dstpath = input('请输入图片输出目录(不输入路径则表示使用源图片所在目录):')
if (dstpath == '') or os.path.exists(dstpath):
row = int(input('请输入切割行数:'))
col = int(input('请输入切割列数:'))
if row > 0 and col > 0:
splitimage(src, row, col, dstpath)
else:
print('无效的行列切割参数!')
else:
print('图片输出目录 %s 不存在!' % dstpath)
else:
print('图片文件 %s 不存在!' % src)

  然后执行。

  恩,确实很简单。Python 的库实在很强大,也很好用。我居然有些不想用 Delphi 写小工具了:)

最新文章

  1. iOS使用Charles(青花瓷)抓包并篡改返回数据图文详解
  2. BSA基础数据维护
  3. Linux CentOS 编绎安装Python 3.5
  4. 重置kafka的offset
  5. MDNavBarView下拉导航菜单(仿美团导航下拉菜单)
  6. 通过AOP 实现异常统一管理
  7. Java中的类加载器以及Tomcat的类加载机制
  8. Android UI -- 的基础知识。
  9. prototype/constructor/__proto__之prototype
  10. C++开源小贱鸡(simsimi api)
  11. Spring连接MySQL、Oracle和SQL Server的数据库运动连接属性
  12. JQuery AJAX Demo
  13. Mybatis基金会: 经常问的问题FAQ
  14. JAVA Static方法与单例模式的理解
  15. IOS学习之路二十四(custom 漂亮的UIColor)
  16. 使用mongodb作为Quartz.Net下的JobStore实现底层的持久化机制
  17. 设计模式总结篇系列:适配器模式(Adapter)
  18. word交叉引用公式编号时和连公式一起引用
  19. UVA 11582 Colossal Fibonacci Numbers(数学)
  20. PyInstaller打包python脚本的一些心得

热门文章

  1. WPF-按钮美化
  2. 51nod 1134最长递增子序列
  3. 使用HttpClient携带证书报错_Certificate for &lt;IP&gt; doesn't match any of the subject alternative names:[域名]
  4. java中什么包不需要导入
  5. 419 Battleships in a Board 甲板上的战舰
  6. 转】[MySQL优化]为MySQL数据文件ibdata1瘦身
  7. net start iisadmin报错:系统找不到指定的文件
  8. 【Mybatis】环境搭建
  9. poj3685 Matrix
  10. ES之值类型以及堆和栈