栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python 批量裁剪tiff图

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python 批量裁剪tiff图

文件夹如图所示:

 程序:

import os
import cv2
'''
裁剪tiff图为(224,224)
'''
source_dir = 'D:deskdmAckyBaseNetdata_setFUSARship-3channel-tiff'
target_dir = 'D:deskdmAckyBaseNetdata_setFUSARship-3-tiff'

# 如果目标目录不存在的话,进行目录的新建
if not os.path.exists(target_dir):
    os.makedirs(target_dir)


T = os.listdir(source_dir)

for t in T:
    files = os.listdir(os.path.join(source_dir, t))
    for class_file in files:
        image_files = os.listdir(os.path.join(source_dir, t, class_file))
        for image in image_files:
            image_name = os.path.join(source_dir, t, class_file, image)
            image_src = os.path.join(target_dir, t, class_file, image)
            img = cv2.imread(image_name)
            image = img[144:368, 144:368]
            print(image.shape)
            if not os.path.exists(os.path.join(target_dir, t, class_file,)):
                os.makedirs(os.path.join(target_dir, t, class_file,))
            cv2.imwrite(image_src, image)  # 图片存储
            print("finish" + " " + image_name)

单张图片的测试代码:

import os
import cv2

img = cv2.imread("D:deskdmAckyBaseNetdata_setFUSARship-3channel-tiff\traincargoshipShip_C01S07N0001.tiff")
print(img.shape)
cv2.imshow("裁剪后前", img)
# 中心裁剪
# 左上角:144=(512-224)/2  右下角:368=512-144
image = img[144:368, 144:368]
print(image.shape)
cv2.imshow("裁剪后", image)

cv2.waitKey()
cv2.destroyAllWindows()

单张图片测试结果图:

批量裁剪结果:

 

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/980534.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号