最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
python切割图片实现代码示例
时间:2022-06-25 01:11:52 编辑:袖梨 来源:一聚教程网
本篇文章小编给大家分享一下python切割图片实现代码示例,文章代码介绍的很详细,小编觉得挺不错的,现在分享给大家供大家参考,有需要的小伙伴们可以来看看。
用opencv处理一下
pillow也可以,但是试过有时候会把图片自动旋转180°,cv没有这个问题
import os
from cv2 import cv2
def split_image(src_path, rownum, colnum, file):
img = cv2.imread(src_path)
# cv2.imwrite(path, img)
size = img.shape[0:2]
w = size[1]
h = size[0]
# print(file, w, h)
# 每行的高度和每列的宽度
row_height = h // rownum
col_width = w // rownum
num = 0
for i in range(rownum):
for j in range(colnum):
# 保存切割好的图片的路径,记得要填上后缀,以及名字要处理一下,可以是
# src_path.split('.')[0] + '_' + str((i+1)*(j+1)) + '.jpg'
save_path = ''
row_start = j * col_width
row_end = (j+1) * col_width
col_start = i * row_height
col_end = (i+1) * row_height
# print(row_start, row_end, col_start, col_end)
# cv2图片: [高, 宽]
child_img = img[col_start:col_end, row_start:row_end]
cv2.imwrite(save_path, child_img)
if __name__ == '__main__':
# 可以遍历文件夹
# file_path = r'我是路径(文件夹路径)'
# for file in file_names:
# src_path 具体图片路径,包含后缀
src_path = ''
row = 4
col = 4
split_image(src_path, row, col, file.split('.')[0])
原图:
切割后:
相关文章
- 如鸢新活动游春集远房亲戚攻略 低配置无核爆通关攻略 08-03
- 燕云十六声免费外观攻略 最难拿到的三个免费套怎么拿 08-03
- 《弧光突袭者》 Arc Raiders “往昔药方”任务攻略 08-03
- 虚无世界宝宝技能搭配攻略(发掘宝宝技能的无限可能) 08-03
- 颠覆认知 曝PS6双机型定价悬殊 多数玩家恐难承受 08-03
- 《盾勇队长技能更换攻略》(优化战斗力,拓展战术选择,走上巅峰之路) 08-03

