Stamp 이미지 생성
구글 드라이브에 Fonts 폴더 생성 후 폰트파일
Colab
feat gpt
from PIL import Image, ImageDraw, ImageFont
import os
from google.colab import drive
import warnings
warnings.filterwarnings("ignore", category=DeprecationWarning)
drive.mount('/content/drive')
folder_path = "/content/drive/My Drive/StampImages/"
if not os.path.exists(folder_path):
os.makedirs(folder_path)
def create_grade_stamp():
image_size=(200, 200)
font_size=125
Grade = ["S","S+","A","A+","B","B+","C","C+","D","D+","E","E+","F","F+"]
font_folder_path = "/content/drive/My Drive/Fonts/"
all_files = os.listdir(font_folder_path)
font_files = [file for file in all_files if file.endswith(".ttf") or file.endswith(".otf")]
for font_path in font_files:
try:
for grade_text in Grade:
# 이미지 생성
image = Image.new("RGB", image_size, "white")
#투명 배경
#image = Image.new("RGBA", image_size, (0,0,0,0))
draw = ImageDraw.Draw(image)
# 도장 테두리 그리기
# draw.ellipse([10, 10, image_size[0] - 10, image_size[1] - 10], outline="red", width=10)
font = ImageFont.truetype( font_folder_path+font_path, font_size)
text_width, text_height = draw.textsize(grade_text, font=font)
text_position = ((image_size[0] - text_width) / 2, (image_size[1] - text_height) / 2)
draw.text(text_position, grade_text, fill="red", font=font)
fontname = font_path.split(".")[0]
output_path=f"/content/drive/My Drive/StampImages/{fontname}/{grade_text}.png"
output_path_dic = f"/content/drive/My Drive/StampImages/{fontname}"
if not os.path.exists(output_path_dic):
os.makedirs(output_path_dic)
# 이미지 저장
image.save(output_path)
print(f"이미지 생성 완료: {output_path}")
except OSError as e:
print(f"오류 발생: {e}")
continue
create_grade_stamp()