1. 폴더 생성 및 데이터 이동
import os
import shutil
# 폴더 생성
os.makedirs("C:/0109_project/data_move_test/images")
os.makedirs("C:/0109_project/data_move_test/labels")
# 옮길 파일명
img_filename = 'image.jpg'
label_filename = 'label.txt'
# image 원본 및 옮겨질 경로
src1 = 'Origin_image/path'
dst1 = 'moved_image/path'
# label 원본 및 옮겨질 경로
src2 = 'Origin_label/path'
dst2 = 'moved_label/path'
# 이동복사
shutil.copy(os.path.join(src1, img_filename), os.path.join(dst1, img_filename))
shutil.copy(os.path.join(src2, label_filename), os.path.join(dst2, label_filename))
2. 텍스트 파일 읽기, 쓰기
txt_root = '텍스트 파일 경로'
# 텍스트 파일 읽기
f = open(txt_root, 'r')
f.read # read, readlines
f.close() # 파일 닫기
# 텍스트 파일 쓰기
f = open(txt_root, 'w')
f.write("example")
f.close()
3. 이미지 출력
import PIL
from PIL import Image, ImageDraw
# Load the image
image = Image.open('이미지 경로')
txt_root = '텍스트 파일 경로'
# Create an instance of ImageDraw
draw = ImageDraw.Draw(image)
f = open(txt_root, 'r') # file 열기/생성하기 / r: 읽기용, w: 쓰기용
data = f.readline()
f.close() # 파일 닫기
data = data.split()
data = list(map(float, data))
x1 = data[1]*255 - (data[3]*255 / 2)
x2 = data[1]*255 + (data[3]*255 / 2)
y1 = data[2]*255 - (data[4]*255 / 2)
y2 = data[2]*255 + (data[4]*255 / 2)
x, h, w, y = x1, data[4]*255, data[3]*255, y1
draw = ImageDraw.Draw(image)
draw.line([(x, y+30), (x+60, y+30), (x+60, y+90), (x, y+90), (x, y+30)], fill="red", width=2)
image.show()
image.save('저장 경로')