labelme标注的图片剪切成2000长度

本文介绍了如何利用Python编程,对通过labelme工具标注的图片进行处理,将其裁剪成长度为2000像素的片段,以适应特定的需求。主要涉及到图像处理和文件操作的相关技术。
摘要由CSDN通过智能技术生成
import json
import glob
import cv2
import os
import shutil
from PIL import Image

# load_data_dir = 'E:/data/json/'  # 原始json位置
# p1 = 'E:/data/move_png/'   # 原始png位置
# p3 = 'E:/data/save_json/'   # 保存json的位置
# copy_path = 'E:/data/save_json/1.json'  # copyjson后保存的位置,1.json临时名称
# p5 = 'E:/data/save_png/'   # png保存位置

load_data_dir = 'C:/Users/wzp/Desktop/j/'  # 原始json位置
p1 = 'C:/Users/wzp/Desktop/p/'   # 原始png位置
p3 = 'C:/Users/wzp/Desktop/js/'   #保存json的位置
copy_path = 'C:/Users/wzp/Desktop/js/1.json'  #copyjson后保存的位置,1.json临时名称
p5 = 'C:/Users/wzp/Desktop/ps/'   # png保存位置


for jf in glob.glob(load_data_dir + '*.json'):  # # 遍历文件夹中的json  glob.glob返回路径
    with open(jf, "r") as f:  # 'r'读  打开文件读,并关闭;jf是json文件;json文件的对象在{}中,数组在【】中
        json_str = f.read()  # f.read()读取文件,返回给json_str
    your_dict = json.loads(json_str)    # json格式转python格式(字符串解码成字典),
    your_dict["imageData"] = None       # python中None即是null
    '''获取文件名,jf是json文件,.以json结尾,【,-5】是减掉(.json)
        your_dict["imagePath"] = os.path.basename(jf)[:-5] + '.png'  '''
    '''找json对应的png,求宽和高'''
    path1 = p1
    path2 = os.path.basename(jf)[:-5] + '.png'
    img_path = os.path.join(path1,path2)   #png的路径
    img = Image.open(img_path)
    imgSize = img.size  # 大小/尺寸
    W = img.width  # 图片的宽
    H = img.height  # 图片的高
    # path_3 = 'C://Users/wzp/Desktop/yuan/'
    # path_4 = os.path.basename(jf)[:-5] + i + '.json'
    # json_path = os.path.join(path3,path4)
    '''初步判断分成几份-N'''
    if H <= 2000:
        N = 1
    elif H > 2000 and H % 2000 == 0:
        N = H // 2000
    else:
        N = H // 2000 + 1
    '''拆分json'''
    shapes = your_dict["shapes"]  # label保存在shapes属性里面
    label = 0
    a = shapes[label]["points"][0][0]
    b = shapes[label]["points"][0][1]
    c = shapes[label]["points"][1][0]
    d = shapes[label]["points"][1][1]
    if a < c and b < d:
        x0 = a
        y0 = b
        x1 = c
        y1 = d
        i = 1
        while i <= N:
            if y0 < 2000*i :
                shutil.copyfile(jf,copy_path)
                old_path = copy_path
                path3 = p3
                path4 = os.path.basename(jf)[:-5] + str(i) + '.json'
                new_json_name = os.path.basename(os.path.join(path3,path4))
                os.rename(old_path, os.path.join(path3,path4))
                shapes = your_dict["shapes"]
                your_dict["imageData"] = None  # python中None即是null
                your_dict["imagePath"] = os.path.basename(os.path.join(path3,path4))  # 获取文件名,jf是json文件,.以json结尾,【,-5】减5个字符
                your_dict["imageHeight"] = 2000
                your_dict["imageWidth"] = W
                shapes[label]["points"][0][1] = y0 - 2000 * (i-1)
                if y1 >= 2000 * i:
                    shapes[label]["points"][1][1] = 2000
                else:
                    shapes[label]["points"][1][1] = y1 - 2000*(i-1)
                save_dir = os.path.join(path3,path4)
                with open(save_dir, "w", encoding="UTF-8") as f1:  # 打开文件写入
                    json.dump(your_dict, f1, ensure_ascii=False, indent=2)
                png = cv2.imread(img_path)
                cut_png = png[2000*(i-1):2000*i,0:W]
                path5 = p5
                path = os.path.basename(jf)[:-5] + str(i) + '.png'
                png_path = os.path.join(path5,path)
                cv2.imwrite(png_path,cut_png)
                i = i+1
                while i <= N :
                    if y1 > 2000*(i-1):
                        shutil.copyfile(jf, copy_path)
                        old_path = copy_path
                        path3 = p3
                        path4 = os.path.basename(jf)[:-5] + str(i) + '.json'
                        new_json_name = os.path.basename(os.path.join(path3, path4))
                        os.rename(old_path, os.path.join(path3, path4))
                        shapes = your_dict["shapes"]
                        your_dict["imageData"] = None  # python中None即是null
                        your_dict["imagePath"] = os.path.basename(os.path.join(path3, path4))  # 获取文件名,jf是json文件,.以json结尾,【,-5】减5个字符
                        your_dict["imageHeight"] = 2000
                        your_dict["imageWidth"] = W
                        shapes[label]["points"][0][1] = 0,
                        if y1 >= 2000*i:
                        # if y1 >= 2000 * (i+1):
                            # your_dict["imageHeight"] = 2000
                            shapes[label]["points"][1
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值