# -*- coding: utf-8 -*-
"""
@Time : 2022/5/6 16:06
@File :divided_sub_txt.py
@IDE :PyCharm
@Motto:no bug!
分割图像
1.将txt文件中的标签和坐标改成对于图像的xy坐标
2.裁切图像,并另存为对应的文件名
存在问题:
1. 空txt
2. 不知道为什么会报错的一些图片
"""
import os
from PIL import Image
import cv2
# 获取图片宽高
def get_image_width_high(imgname):
image = cv2.imread(imgname)
image_width, image_high = image.shape[0], image.shape[1]
return image_width, image_high
# 坐标转化
def convert_line(line, image_width, image_high):
array = line.split()
# opencv中的坐标是高 宽
x_label_min = (float(array[1]) - float(array[3]) / 2) * image_high
x_label_max = (float(array[1]) + float(array[3]) / 2) * image_high
y_label_min = (float(array[2]) - float(array[4]) / 2) * image_width
y_label_max = (float(array[2]) + float(array[4]) / 2) * image_width
bbox = (int(x_label_min), int(y_label_min), int(x_label_max), int(y_label_max))
return bbox
imgdir = r'xxx'
txtdir = r'xxx'
savedir = r'xxx'
imgs = os.listdir(imgdir)
txts = os.listdir(txtdir)
for img in imgs:
# img = '16915183.jpg'
img1 = Image.open(os.path.join(imgdir, img))
txt = img[:-4] + '.txt'
f = open(os.path.join(txtdir, txt), 'r')
lines = f.readlines()
f.close()
new_lines = []
image_width, image_high = get_image_width_high(os.path.join(imgdir, img))
if len(lines) > 1:
for line in lines:
new_lines.append(line[:-1])
for i in range(len(lines)):
crop_box = convert_line(new_lines[i], image_width, image_high)
img2 = img1.crop(crop_box)
imgname = img[:-4] + '_' + str(i) + '.jpg'
try:
img2.save(os.path.join(savedir, imgname))
except:
print(img,'error')
elif len(lines) == 1:
try:
line = lines[0]
if "\n" in line:
line = line[:-1]
else:
pass
crop_box = convert_line(line, image_width, image_high)
# print(crop_box)
img2 = img1.crop(crop_box)
imgname = img
img2.save(os.path.join(savedir, imgname))
except:
print(img)
print(crop_box)
else:
print(img,'txt文件为空')
如果图片报错的话,或者只想有一张图片的话
import os
from PIL import Image
import cv2
# 获取图片宽高
def get_image_width_high(imgname):
image = cv2.imread(imgname)
image_width, image_high = image.shape[0], image.shape[1]
return image_width, image_high
# 坐标转化
def convert_line(line, image_width, image_high):
array = line.split()
# opencv中的坐标是高 宽
x_label_min = (float(array[1]) - float(array[3]) / 2) * image_high
x_label_max = (float(array[1]) + float(array[3]) / 2) * image_high
y_label_min = (float(array[2]) - float(array[4]) / 2) * image_width
y_label_max = (float(array[2]) + float(array[4]) / 2) * image_width
bbox = (int(x_label_min), int(y_label_min), int(x_label_max), int(y_label_max))
return bbox
img = '16915183.jpg'
img1 = Image.open(os.path.join(imgdir, img))
txt = img[:-4] + '.txt'
f = open(os.path.join(txtdir, txt), 'r')
lines = f.readlines()
f.close()
new_lines = []
image_width, image_high = get_image_width_high(os.path.join(imgdir, img))
if len(lines) > 1:
for line in lines:
new_lines.append(line[:-2])
for i in range(len(lines)):
crop_box = convert_line(new_lines[i], image_width, image_high)
img2 = img1.crop(crop_box)
imgname = img[:-4] + '_' + str(i) + '.jpg'
img2.save(os.path.join(savedir, imgname))
elif len(lines) == 1:
try:
line = lines[0]
if "\n" in line:
line = line[:-1]
else:
pass
crop_box = convert_line(line, image_width, image_high)
# print(crop_box)
img2 = img1.crop(crop_box)
imgname = img
img2.save(os.path.join(savedir, imgname))
except:
print(img)
print(crop_box)
else:
print(img, 'txt文件为空')