728x90
반응형
아래 코드는 input 이미지가 jpg형식일 때 적용 가능한 코드
from PIL import Image, ImageDraw, ImageFont
# Open the image file
image = Image.open('./input_image.jpg')
# Create a drawing context for the image
draw = ImageDraw.Draw(image, "RGBA")
# Define the polygon points
polygon_points = [(50, 50), (200, 50), (200, 200), (50, 200)]
#polygon_points = [50, 50, 200, 50, 200, 200, 50, 200]
# Define the fill color and transparency for the polygon
fill_color = (255, 0, 0, 50) # RGBA format: (red, green, blue, alpha)
# Draw the polygon on the image
draw.polygon(polygon_points, fill=fill_color)
# Define the font and size for the text
font = ImageFont.truetype("FONT_NAME.ttf", 30)
# Define the text and color
text = "YES"
# Get the size of the text
text_size = draw.textsize(text, font)
text_x = (polygon_points[0][0] + polygon_points[2][0] - text_size[0]) // 2
text_y = (polygon_points[0][1] + polygon_points[2][1] - text_size[1]) // 2
# Draw the text on the image
draw.text((text_x, text_y), text, font=font, fill=(255,255,255))
# Save the modified image to a file
image.save('./output_image.jpg')
+) png일때는 다음과 같이 표현되어서, 원본이미지와 동일한 크기의 빈 layer를 하나 더 추가해 준 후, 그려줘야 우리가 원하는 결과물을 얻을 수 있을 것 같다.
728x90
반응형
'머신러닝 > Computer Vision' 카테고리의 다른 글
[PascalVOC] Generate Pascal VOC(polygon) xml 생성(Labelme Json to voc xml) (0) | 2023.03.13 |
---|---|
[Labelme2COCO] (0) | 2023.02.23 |
[cv2] Python image crop (0) | 2023.01.19 |
[Python]Pascal-Voc XML format to Yolov5(v7) txt format/PascalVOC to Yolo txt (0) | 2023.01.12 |
[PIL / colorthief / extcolors] 이미지 내 주요 색 추출 (Extracting dominant colors in image python) (0) | 2022.11.14 |