使用python批量裁剪图片为目标大小

一、背景

当进行深度学习任务如进行训练时,图片的大小可能导致显存不足,一个方法是对图片直接进行resize,这个会导致图片细节不足;另一个是对图片进行裁剪,裁剪成小的尺寸以送入模型训练,这也有一个缺点,就是会丢失上下文信息。现在想使用python对图片批量裁剪,那么设计了如下一个脚本。

二、方法

进行裁剪时会面临的首要问题是,宽高不足以整除时,如大小为1024*1024的图像,裁剪为300*300的时候,怎么解决? 我的代码是图片在进行横向切分时,在第四片时,剩下124像素大小,这时我将已经被裁剪的部分补充进去,如下图所示:

纵向也是如此,循环代码即可获得最终的切片效果。

三、代码

#coding=gbk
import cv2
import numpy
import scipy.io as scio
import math
import os
maskFile = r'C:\Users\Administrator\Desktop\test_labelme\labelme\examples\semantic_segmentation\DH_voc\SegmentationClassPNG'   # 待裁剪的mask
imgFile = r'C:\Users\Administrator\Desktop\test_labelme\labelme\examples\semantic_segmentation\DH_voc\SegmentationClassPNG'    #待裁剪的图片 两者实际操作相同,如果只裁剪一类图像,如只有mask,则删除对应的裁剪图片的代码即可
###"C:\Users\Administrator\Desktop\1" 为保存结果路径,请修改

list_mask = []
list_img = []
for root, _, fnames in sorted(os.walk(maskFile)):
    for fname in fnames:
        path = os.path.join(root, fname)
        list_mask.append(path)
for root, _, fnames in sorted(os.walk(imgFile)):
    for fname in fnames:
        path = os.path.join(root, fname)
        list_img.append(path)

for i in range(len(list_mask)):

    p,n = os.path.split(list_mask[i])
    pro,ext = os.path.splitext(n)
    print(pro)
    mask = cv2.imread(list_mask[i])
    img = cv2.imread(list_img[i])

    (h, w, c) = mask.shape
    h_n = math.ceil(h / 480.0)  #裁剪的高  480修改为想裁剪的大小
    w_n = math.ceil(w / 480.0)  #裁剪的宽  480修改为想裁剪的大小
    (h, w, c) = img.shape

    for i in range(h_n):
        if i < h_n - 1:
            for j in range(w_n):
                if j < w_n - 1:
                    mask_patch = mask[i * 480:(i + 1) * 480, j * 480:(j + 1) * 480,:]
                    img_pathc = img[i * 480:(i + 1) * 480, j * 480:(j + 1) * 480, :]

                    mask_pathname = os.path.join(r"C:\Users\Administrator\Desktop\1",
                                                  pro[:4]  +'-'+ str(i) + '-' + str(j) + '.png')
                    img_pathname = os.path.join(r"C:\Users\Administrator\Desktop\1",
                                                pro[:4] +'-'+ str(i) + '-' + str(j) + '.png')
                    cv2.imwrite(mask_pathname, mask_patch)
                    #cv2.imwrite(img_pathname, img_pathc)

                else:
                    mask_patch = mask[i * 480:(i + 1) * 480, (w - 480):, :]
                    img_pathc = img[i * 480:(i + 1) * 480, (w - 480):, :]
                    mask_pathname = os.path.join(r"C:\Users\Administrator\Desktop\1",
                                                  pro[:4] +'-'+ str(i) + '-' + str(j) + '.png')
                    img_pathname = os.path.join(r"C:\Users\Administrator\Desktop\1",
                                                pro[:4]  +'-'+ str(i) + '-' + str(j) + '.png')
                    cv2.imwrite(mask_pathname, mask_patch)
                    #cv2.imwrite(img_pathname, img_pathc)

        else:
            for j in range(w_n):
                if j < w_n - 1:
                    mask_patch = mask[(h - 480):, j * 480:(j + 1) * 480, :]
                    img_pathc = img[(h - 480):, j * 480:(j + 1) * 480, :]
                    mask_pathname = os.path.join(r"C:\Users\Administrator\Desktop\1",
                                                  pro[:4]  + '-'  + str(i) + '-' + str(j) + '.png')
                    img_pathname = os.path.join(r"J:\dataset\DH_voc4\JPEGImages",
                                                pro[:4] +'-'+  str(i) + '-' + str(j) + '.png')
                    cv2.imwrite(mask_pathname, mask_patch)
                    #cv2.imwrite(img_pathname, img_pathc)

                else:
                    mask_patch = mask[(h - 480):, (w - 480):, :]
                    img_pathc = img[(h - 480):, (w - 480):, :]
                    mask_pathname = os.path.join(r"C:\Users\Administrator\Desktop\1",
                                                  pro[:4]+'-'+ str(i) + '-' + str(j) + '.png')
                    img_pathname = os.path.join(r"C:\Users\Administrator\Desktop\1",
                                                pro[:4] +'-'+ str(i) + '-' + str(j) + '.png')
                    cv2.imwrite(mask_pathname, mask_patch)
                    #cv2.imwrite(img_pathname, img_pathc)

四、总结

代码中有部分需要修改和优化的地方,请自行优化。如图片的命名,裁剪后拼接的话也需要标记出原始图片的大小才可以进行还原,不赘述。如果有错误,请私信!感谢!!

  • 3
    点赞
  • 33
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Alocus_

如果我的内容帮助到你,打赏我吧

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值