python切割图片的示例
作者:jujua 时间:2021-01-10 22:09:12
这个小程序可以自己设定行数和列数进行图片切割
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)
运行效果
来源:https://www.cnblogs.com/jyxbk/p/7805042.html
标签:python,切割,图片
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python利用paramiko连接远程服务器执行命令的方法
2021-07-19 01:07:34
微信小程序创建自定义全局函数以及其调用方法详解
2023-08-24 20:43:22
![](https://img.aspxhome.com/file/2023/5/56255_0s.png)
python包合集shutil示例代码详解
2022-03-28 12:04:27
![](https://img.aspxhome.com/file/2023/5/115755_0s.png)
解决python3 整数数组转bytes的效率问题
2023-08-09 19:39:41
Python基于Hypothesis测试库生成测试数据
2021-10-21 20:27:22
Python Opencv使用ann神经网络识别手写数字功能
2023-11-03 02:44:52
![](https://img.aspxhome.com/file/2023/8/63268_0s.png)
Python控制鼠标键盘代码实例
2021-07-28 11:17:30
详解python学习笔记之解释器
2023-09-15 15:28:26
scrapy-splash简单使用详解
2023-06-02 15:22:37
![](https://img.aspxhome.com/file/2023/1/103161_0s.jpg)
基于YUV 数据格式详解及python实现方式
2021-12-03 07:28:59
![](https://img.aspxhome.com/file/2023/9/93859_0s.jpg)
IE6局部调用PNG32合并图片
2009-03-11 21:24:00
![](https://img.aspxhome.com/file/UploadPic/20093/11/01-12s.jpg)
分享13个非常有用的Python代码片段
2022-02-17 00:00:37
python实现彩票系统
2021-04-12 21:16:42
python 利用jieba.analyse进行 关键词提取
2021-03-16 22:11:26
![](https://img.aspxhome.com/file/2023/5/78575_0s.png)
django admin实现动态多选框表单的示例代码
2021-12-25 11:31:29
![](https://img.aspxhome.com/file/2023/6/97606_0s.png)
python在Windows下安装setuptools(easy_install工具)步骤详解
2022-10-18 19:40:23
![](https://img.aspxhome.com/file/2023/6/107466_0s.jpg)
Python学生成绩管理系统简洁版
2023-08-23 01:33:08
一个jquery日期选取插件源码
2009-12-23 19:15:00
![](https://img.aspxhome.com/file/UploadPic/20101/11/2009126213743-92s.png)
使用python计算三角形的斜边例子
2023-01-25 01:33:19
![](https://img.aspxhome.com/file/2023/6/95396_0s.jpg)
Python对XML文件实现增删改查操作
2023-11-19 20:42:03