Notice
Recent Posts
Recent Comments
Links
«   2025/04   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30
Today
In Total
관리 메뉴

A Joyful AI Research Journey🌳😊

[2] 241031 Task: ChatGPT 활용한 프로그램 만들어보기: 영어 캘리그라피 이미지 생성기 [Goorm All-In-One Pass! AI Project Master - 4th Session, Day 2] 본문

🌳AI & Quantum Computing Bootcamp✨/Tasks

[2] 241031 Task: ChatGPT 활용한 프로그램 만들어보기: 영어 캘리그라피 이미지 생성기 [Goorm All-In-One Pass! AI Project Master - 4th Session, Day 2]

yjyuwisely 2024. 10. 31. 15:17

241031 Thu 2nd class

1시간 동안 만드는 시간을 주었다. 

생각해본 주제 

- OpenCV 이용해서 객체 detection

- 로또 추첨기

- 영어 이름 추천

- 이름을 캘리그라피로 만들어줌 


마지막 주제가 실용적이어서 골랐다.

ChatGPT를 이용했는데 오류가 났고 Colab의 Gemini가 오래된 버전의 코드라고 알려줘서 해결했다.

수업 시간의 강사님의 말대로 옛날 버전의 코드를 ChatGPT가 알려주는 상황이랑 똑같았다.

두 가지 버전을 만든 이유는 첫번째 버전은 긴 문장이 짤려서 그런데 마지막 버전은 긴 문장도 가능하다. 


영어 캘리그라피 이미지 생성기

중앙 정렬, 이미지에 마진 큼

import requests
from PIL import Image, ImageDraw, ImageFont
from IPython.display import display
# URL to download a font from Google Fonts
# Google Fonts에서 폰트를 다운로드하기 위한 URL
url = 'https://github.com/google/fonts/raw/main/ofl/greatvibes/GreatVibes-Regular.ttf'
response = requests.get(url)
# Save the font locally
# 폰트를 로컬에 저장
font_filename = 'GreatVibes-Regular.ttf'
with open(font_filename, 'wb') as f:
f.write(response.content)
# Now use this downloaded font in your image generation
# 이제 다운로드한 폰트를 이미지 생성에 사용
def create_calligraphy_image(sentence, output_filename='calligraphy.png'):
# Create a blank image (white background)
# 빈 이미지 생성 (흰색 배경)
width, height = 800, 400
image = Image.new('RGB', (width, height), 'white')
# Set up the drawing context
# 그리기 컨텍스트 설정
draw = ImageDraw.Draw(image)
# Load the downloaded font
# 다운로드한 폰트 불러오기
font = ImageFont.truetype(font_filename, size=80)
# Calculate the position to center the text
# 텍스트를 중앙에 배치하기 위한 위치 계산
text_bbox = draw.textbbox((0, 0), sentence, font=font)
text_width = text_bbox[2] - text_bbox[0] # Calculate width from bbox / 경계 상자에서 텍스트 너비 계산
text_height = text_bbox[3] - text_bbox[1] # Calculate height from bbox / 경계 상자에서 텍스트 높이 계산
position = ((width - text_width) // 2, (height - text_height) // 2)
#text_width, text_height = draw.textsize(sentence, font=font)
#position = ((width - text_width) // 2, (height - text_height) // 2)
# Draw the text in calligraphy style
# 캘리그라피 스타일로 텍스트 그리기
draw.text(position, sentence, fill='black', font=font)
# Save the image
# 이미지 저장
image.save(output_filename)
print(f"Calligraphy image saved as {output_filename}") # 캘리그라피 이미지가 저장된 파일명 출력
# Display the image in Google Colab
# Google Colab에서 이미지 표시
display(image)
# Example usage
# 예시 사용
create_calligraphy_image("Live wisely")
create_calligraphy_image("Believe in yourself")
create_calligraphy_image("This too shall pass away.")

결과)


긴 문장 영어 캘리그라피 이미지 생성기

import requests
from PIL import Image, ImageDraw, ImageFont
from IPython.display import display
# URL to download a font from Google Fonts
# Google Fonts에서 폰트를 다운로드하기 위한 URL
url = 'https://github.com/google/fonts/raw/main/ofl/greatvibes/GreatVibes-Regular.ttf'
response = requests.get(url)
# Save the font locally
# 폰트를 로컬에 저장
font_filename = 'GreatVibes-Regular.ttf'
with open(font_filename, 'wb') as f:
f.write(response.content)
def create_long_calligraphy_image(sentence, output_filename='calligraphy.png'):
# Load the downloaded font
# 다운로드한 폰트 불러오기
font = ImageFont.truetype(font_filename, size=80)
# Create a temporary image to calculate text size
# 텍스트 크기를 계산하기 위한 임시 이미지 생성
temp_image = Image.new('RGB', (1, 1), 'white')
draw = ImageDraw.Draw(temp_image)
# Get the bounding box for the sentence to determine the required size
# 텍스트의 경계 상자를 구해 필요한 크기를 결정
text_bbox = draw.textbbox((0, 0), sentence, font=font)
text_width = text_bbox[2] - text_bbox[0] # Calculate width from bbox / 경계 상자에서 너비 계산
text_height = text_bbox[3] - text_bbox[1] # Calculate height from bbox / 경계 상자에서 높이 계산
# Set padding and adjust canvas size
# 패딩 설정 및 캔버스 크기 조정
padding = 20
width = text_width + padding * 2
height = text_height + padding * 2
# Create a new image with updated dimensions
# 조정된 크기로 새로운 이미지 생성
image = Image.new('RGB', (width, height), 'white')
# Set up the drawing context for the actual image
# 실제 이미지에 그리기 위한 그리기 컨텍스트 설정
draw = ImageDraw.Draw(image)
# Calculate the position to center the text
# 텍스트를 중앙에 배치하기 위한 위치 계산
position = ((width - text_width) // 2, (height - text_height) // 2)
# Draw the text in calligraphy style
# 캘리그라피 스타일로 텍스트 그리기
draw.text(position, sentence, fill='black', font=font)
# Save the image
# 이미지 저장
image.save(output_filename)
print(f"Calligraphy image saved as {output_filename}") # 캘리그라피 이미지가 저장된 파일명 출력
# Display the image in Google Colab
# Google Colab에서 이미지 표시
display(image)
# Example usage
# 예시 사용
create_long_calligraphy_image("It's kind of fun to do the impossible.")
create_long_calligraphy_image("Goorm All-in-One Pass: AI Project Master Bootcamp")

결과) 

728x90
반응형
Comments