파이썬/파이썬(python) 중급

[18-7 파이썬] 예술 작품 만들기(파이썬 라이브러리 turtle, colorgram 이용)

Olivia-BlackCherry 2022. 8. 27. 11:56

파이썬 패키지 turtlecolorgram을 이용하여 

데미안 허스트 Damien Hirst의 spot 그림을 그려보자.

작품에서 이용될 색깔은 아래와 같다.

color_list = [(230, 229, 227), (245, 233, 240), (232, 166, 63), (239, 241, 245), (47, 112, 155), (112, 152, 202), (210, 124, 164), (19, 128, 96), (150, 20, 57), (223, 201, 120), (230, 241, 237), (5, 176, 143), (172, 46, 86), (222, 77, 115), (163, 166, 37), (29, 35, 83), (226, 88, 45), (120, 173, 121), (45, 166, 204), (119, 103, 157), (214, 64, 35), (39, 56, 100), (223, 171, 190), (65, 25, 50), (153, 212, 197), (181, 187, 211), (230, 173, 164), (152, 207, 223), (30, 90, 74), (149, 14, 8)]

 

규칙이다.

- 가로 5개, 세로 5개, 총 10개의 점
- 색깔 랜덤
- 행 진행 방향: 왼쪽부터 시작
- 열 진행 방향: 아래부터 시작 
- 점의 크기: 약 10, 점 간격: 15

 

1. 기본 설정 세팅하기

from turtle import Turtle, Screen

# 색깔 무작위로 선택하기
import random
color_list = [(232, 166, 63), (239, 241, 245), (47, 112, 155), (112, 152, 202), (210, 124, 164), (19, 128, 96), (150, 20, 57), (223, 201, 120), (230, 241, 237), (5, 176, 143), (172, 46, 86), (222, 77, 115), (163, 166, 37), (29, 35, 83), (226, 88, 45), (120, 173, 121), (45, 166, 204), (119, 103, 157), (214, 64, 35), (39, 56, 100), (223, 171, 190), (65, 25, 50), (153, 212, 197), (181, 187, 211), (230, 173, 164), (152, 207, 223), (30, 90, 74), (149, 14, 8)]
def color_choice():
  color_choice = random.choice(color_list)
  return color_choice

#객체 생성
artist = Turtle()

#화면 띄우기
screen =Screen()

#rgb 이용하기
screen.colormode(255)

#마우스 클릭하면 화면 닫기
screen.exitonclick()

 

 

2. 점 찍기

dot()

파라미터로 size와 color가 온다.

artist.dot(15, color_choice())

 

 

3. 이동시키기

두 가지 방법이 있다.

3-1. 좌표 이용  

setposition(x, y) 메소드

 

상대좌표인 setx(x), sety(y)

for i in range(5):
  artist.dot(10, color_choice())
  for j in range(4):
    artist.forward(25)
    artist.dot(10, color_choice())
  (x,y) =artist.position()
  artist.setposition(x,y+25) 
  artist.setx(0)

 

3-2 각도 이용

setheading() 

0일 때 동쪽을 바라보고

90일 때 북쪽을 바라보고

180일 때 서쪽을 바라보고

270일 때 남쪽을 바라봅니다. 

#시작점 다시 설정하기
artist.setheading(225)
artist.forward(40)
artist.setheading(0)

count = 25
for i in range(1, count+1):
  artist.dot(10, color_choice())
  artist.forward(15)
  if i % 5 ==0:
    artist.setheading(90)
    artist.forward(15)
    artist.setheading(180)
    artist.forward(75)
    artist.setheading(0)

 

 

4. 펜자국 지우기

penup() 

artist.penup()

 

 

5. 화살표 지우기

hideturtle() 

artist.hideturtle()

 

[최종코드]

from turtle import Turtle, Screen
import random
color_list = [(232, 166, 63), (239, 241, 245), (47, 112, 155), (112, 152, 202), (210, 124, 164), (19, 128, 96), (150, 20, 57), (223, 201, 120), (230, 241, 237), (5, 176, 143), (172, 46, 86), (222, 77, 115), (163, 166, 37), (29, 35, 83), (226, 88, 45), (120, 173, 121), (45, 166, 204), (119, 103, 157), (214, 64, 35), (39, 56, 100), (223, 171, 190), (65, 25, 50), (153, 212, 197), (181, 187, 211), (230, 173, 164), (152, 207, 223), (30, 90, 74), (149, 14, 8)]

def color_choice():
  color_choice = random.choice(color_list)
  return color_choice
  
artist = Turtle()
#pen up
artist.penup()
artist.hideturtle()
screen =Screen()
screen.colormode(255)

#시작점 다시 설정하기
artist.setheading(225)
artist.forward(40)
artist.setheading(0)

count = 25
for i in range(1, count+1):
  artist.dot(10, color_choice())
  artist.forward(15)
  if i % 5 ==0:
    artist.setheading(90)
    artist.forward(15)
    artist.setheading(180)
    artist.forward(75)
    artist.setheading(0)
    
screen.exitonclick()