使用python在母图基础上随意粘贴子图且不覆盖母图标注坐标

直接上代码,亲测有效

1.裁剪图片

根据已有的json文件进行坐标的确定与裁剪,使用到的包是pillow,没有这个第三方包的可以使用pip命令装一个

import json
import os
from PIL import Image


images_path = "F:/data/images/"
json_path = "F:/data/JSON/"
save_path = "C:/Users/Administrator/Desktop/test/save/"

images_file = os.listdir(images_path)
#print(json_file)
for file  in images_file:
    i = 1
    imagepath = images_path + file
    image = Image.open(imagepath)
    name = os.path.splitext(file)
    jsonfile = json_path + name[0] +'.json'
    with open(jsonfile,'r',encoding='utf-8') as f:
        json_data = json.load(f)
    shape = json_data["shapes"]
    for key in shape:
        points = key["points"]
        # print(points)
        x = points[0][0]
        y = points[0][1]
        w = points[1][0]
        h = points[1][1]
        W = w -x
        H = h -y
        rate = H / W
        area = W*H
        print('searching!!')
        #设置筛选需要的图片
        if(area<16000 and 1.0<rate<1.1):
            print(area,rate)
            i +=1
            newname = save_path + name[0] + str(i) + '.jpg' 
            #裁剪图片函数
            image_crop = image.crop((x, y,  w, h))
            #保存图片
            image_crop.save(newname)

2.粘贴子图到母图上

import os
from PIL import Image
import random
import json



def compute_iou(box1,box2):
    #计算两个iou的框
    for a in range(len(box1_list)):
        iou_h = min(box1[a][3],box2[0][3])-max(box1[a][1],box2[0][1])
        iou_w = min(box1[a][2],box2[0][2])-max(box1[a][0],box2[0][0]) 
        if iou_h<0 or iou_w<0:
            union = 0
        else:
            union = iou_h * iou_w
            return  union
    #box2.clear()
    return union

old_images_path = 'C:/Users/Administrator/Desktop/Try/old_images/'
json_path = 'C:/Users/Administrator/Desktop/Try/json/'
save_path = 'C:/Users/Administrator/Desktop/Try/change_images/'
imgs_path = 'C:/Users/Administrator/Desktop/Try/save/'

old_images = os.listdir(old_images_path)
cut_images = os.listdir(imgs_path)
#将列表打乱,重新排序
random.shuffle(cut_images)
#print(random.shuffle(cut_images))   

for image in old_images:
    i = 0
    box1_list = []
    box2_list = []
    image_size_list = []
    cut_images_size_list =[]
    #读取母图
    images = Image.open(old_images_path + image)
    #获取母图尺寸大小
    image_size = images.size
    image_size_list.append(image_size)
    #print(image_size_list)
    #解析json文件获取box1位置坐标
    filename = os.path.splitext(image)
    json_name = filename[0] + '.json'
    #保存图片路径
    save_name = save_path + filename[0] + '_change' + '.jpg'
    
    with open(json_path + json_name,'r',encoding='utf-8') as f:
        json_data = json.load(f)
        #取json中的位置坐标
        shapes = json_data['shapes']
        for name in shapes: 
            points = name['points']
            x1 = int(points[0][0])
            y1 = int(points[0][1])
            x2 = int(points[1][0])
            y2 = int(points[1][1])
            box1_list.append([x1,y1,x2,y2])
    for file in cut_images:
        #读取截取图片
        imgs = Image.open(imgs_path + file)
        #获取裁剪图片尺寸
        img = imgs.size
        cut_images_size_list.append(img)
        print(imgs)
        if(i<3):  #粘贴图的数量设定
            #计算宽和高的取值范围
            xrange_num = image_size_list[0][0] - cut_images_size_list[0][0]
            yrange_num = image_size_list[0][1] - cut_images_size_list[0][1]
            x = random.randint(0,xrange_num)
            y = random.randint(0,yrange_num)
            w = x+cut_images_size_list[0][0]
            h = y+cut_images_size_list[0][1]
            box2_list.append([x,y,w,h])            
            while(True):
                iou = compute_iou(box1_list,box2_list)
                if(iou>0):
                    box2_list.remove([x,y,w,h])
                    x = random.randint(0,xrange_num)
                    y = random.randint(0,yrange_num)
                    w = x+cut_images_size_list[0][0]
                    h = y+cut_images_size_list[0][1]
                    box2_list.append([x,y,w,h])  
                    continue
                else:
                    box1_list.append([x,y,w,h])
                    box2_list.remove([x,y,w,h])
                    cut_images_size_list.pop(0)
                    break
            #粘贴图片
            images.paste(imgs,(x,y))
            #显示图片
            #images.show()
            #保存图片
            images.save(save_name)
            #移除粘贴过的图片
            cut_images.pop(i)
            i += 1
            continue   

代码写的会比较臃肿但逻辑是正确的,如有简介的代码还请告知,谢谢!!!

转载请说明出处!!!!!!

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值