본문 바로가기

python334

python show image with polygon, 파이썬 이미지 폴리곤하고 같이 그리기 python show image with polygon, 파이썬 이미지 폴리곤하고 같이 그리기 pillow와 matplotlb을 이용하여 그림위에 폴리곤을 그리는 방법은 아래 코드와 같습니다. from PIL import Image, ImageDraw import matplotlib.pyplot as plt # Open the image image = Image.open('image.jpg') # Create a new image with the same size as the original image poly_image = Image.new('RGBA', image.size, (0, 0, 0, 0)) # Draw the polygon on the new image draw = ImageDraw.Draw.. 2023. 4. 7.
스타크래프트 유닛 마린을 파이썬 클래스로 작성하는 방법 [파이썬 기초 클래스 공부하기 예시]스타크래프트 마린을 파이썬 클래스로 작성하는 방법¶ 파이썬 클래스를 처음 공부할때 자주 사용되는 마린 만드는 예시 입니다. ! In [1]: class Marine: # __init__ 함수를 이용하여 health 와 attack_power를 설정해 줄 수 있게 합니다. # 단 입력을 안하면 health가 40 attack_power가 6 으로 자동 설정 됩니다. def __init__(self, health=40, attack_power=6): self.health = health self.max_health = health self.attack_power = attack_power print(f"New marine has {self.health} health") #.. 2023. 4. 6.
파이썬 함수 만드는 법 파이썬 함수 만드는 법¶ 함수 구조 In [1]: def 함수이름(매개변수): # 함수 내용 return 반환값 두 매개 변수를 더하는 함수 예시 In [2]: # 아래 함수는 add_numbers라는 이름을 가지며, x와 y 두 개의 매개변수를 받아들입니다. # 함수 내부에서는 두 숫자를 더한 값을 result 변수에 저장하고, 이 값을 return을 사용하여 반환합니다. def add_numbers(x, y): result = x + y return result 예시 함수 호출 결과 In [3]: add_numbers(3, 5) Out[3]: 8 2023. 4. 3.
파이썬 숫자를 순서대로 나열하기, 숫자 나열하기 : numpy linspace 파이썬 숫자를 순서대로 나열하기, 숫자 나열하기 : numpy linspace¶ In [1]: import numpy as np # [numpy linspace 함수] # 시작 숫자, 마지막 숫자, 그리고 간극 갯수 순서대로 입력하여, # 시작 숫자와 마지막 숫자 사이에 간극 갯수 만큼 숫자대로 나열합니다. # 0과 9 사이 10 개의 숫자를 나열하는 명령은 아래와 같습니다. np.linspace(0, 9, 10) Out[1]: array([0., 1., 2., 3., 4., 5., 6., 7., 8., 9.]) In [2]: # 만약 마지막 숫자를 제외 하고 싶다면 # endpoint=False 옵션을 사용할 수 있습니다. np.linspace(0, 9, 10, endpoint=False) Out[2].. 2023. 4. 2.
넘파이 전치(transpose) 연산, numpy transpose, 행 열 뒤집기 넘파이 전치(transpose) 연산, numpy transpose, 행 열 뒤집기¶ In [1]: import numpy as np In [2]: A = np.array([[3.0, 4.0, 7.0], [10.0, 11.0, 14.0]]) A Out[2]: array([[ 3., 4., 7.], [10., 11., 14.]]) In [3]: A.T Out[3]: array([[ 3., 10.], [ 4., 11.], [ 7., 14.]]) [참조] 기타 넘파이 기능 넘파이를 활용한 워드카운트 CLICK ! https://noanomal.tistory.com/14 어레이 내 0보다 큰 값 찾기CLICK ! https://noanomal.tistory.com/57 넘파이 값 추가 insert CLICK !.. 2023. 3. 31.
matplotlib pyplot 사이즈 조절, rcParams matplotlib pyplot 사이즈 조절, rcParams¶ plt 객체 네 rcParams["figure.figsize"] 의 변수를 튜플형태로 제설정 해주면 사이즈가 변환된다 rcParams["figure.figsize"] 의 변수는 (가로 길이, 세로길이) 로 구성되어 있다 In [5]: import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = (3,3) plt.plot() # multi plot, linewidth = 라이 두께 plt.plot([2, 0],[0, 2]) plt.plot([0, 2],[0, 2]) plt.show() In [6]: import matplotlib.pyplot as plt plt.rcParams["figu.. 2023. 3. 30.