import
1import numpy as np 2from PIL import Image 3import sys 4import argparse 5 6 7def preprocess(path, col): 8 9 img = Image.open(path) 10 img = img.convert('L') 11 12 width = col*4 13 height = int(width*(img.height/img.width)) 14 height -= height%4 15 16 img = img.resize((width, height)) 17 img = np.matrix(img) 18 img = (img/128.) - 1. 19 20 return img 21 22 23def load_tsuki_matrixs(): 24 25 tsuki_0 = np.matrix([[-1, -1, -1, -1], 26 [-1, -1, -1, -1], 27 [-1, -1, -1, -1], 28 [-1, -1, -1, -1]]) 29 30 tsuki_1 = np.matrix([[-1, -1, -1, 1], 31 [-1, -1, -1, 1], 32 [-1, -1, -1, 1], 33 [-1, -1, -1, 1]]) 34 35 tsuki_2 = np.matrix([[-1, -1, 1, 1], 36 [-1, -1, 1, 1], 37 [-1, -1, 1, 1], 38 [-1, -1, 1, 1]]) 39 40 tsuki_3 = np.matrix([[-1, 1, 1, 1], 41 [-1, 1, 1, 1], 42 [-1, 1, 1, 1], 43 [-1, 1, 1, 1]]) 44 45 tsuki_4 = np.matrix([[1, -1, -1, -1], 46 [1, -1, -1, -1], 47 [1, -1, -1, -1], 48 [1, -1, -1, -1]]) 49 50 tsuki_5 = np.matrix([[1, 1, -1, -1], 51 [1, 1, -1, -1], 52 [1, 1, -1, -1], 53 [1, 1, -1, -1]]) 54 55 tsuki_6 = np.matrix([[1, 1, 1, -1], 56 [1, 1, 1, -1], 57 [1, 1, 1, -1], 58 [1, 1, 1, -1]]) 59 60 tsuki_7 = np.matrix([[1, 1, 1, 1], 61 [1, 1, 1, 1], 62 [1, 1, 1, 1], 63 [1, 1, 1, 1]]) 64 65 return [tsuki_0, tsuki_1, tsuki_2, tsuki_3, 66 tsuki_4, tsuki_5, tsuki_6, tsuki_7] 67 68 69def index2tsuki(index): 70 71 if index==0: 72 return emoji.emojize(':new_moon:', use_aliases=True) 73 if index==1: 74 return emoji.emojize(':waxing_crescent_moon:', use_aliases=True) 75 if index==2: 76 return emoji.emojize(':first_quarter_moon:', use_aliases=True) 77 if index==3: 78 return emoji.emojize(':waxing_gibbous_moon:', use_aliases=True) 79 if index==4: 80 return emoji.emojize(':waning_crescent_moon:', use_aliases=True) 81 if index==5: 82 return emoji.emojize(':last_quarter_moon:', use_aliases=True) 83 if index==6: 84 return emoji.emojize(':waning_gibbous_moon:', use_aliases=True) 85 else: 86 return emoji.emojize(':full_moon:', use_aliases=True) 87 88 89if __name__ == "__main__": 90 91 92 parser = argparse.ArgumentParser() 93 parser.add_argument("--path", default="t.jpg", type=str) 94 parser.add_argument("--col", default="50", type=int) 95 args = parser.parse_args() 96 97 tsuki_matrixs = load_tsuki_matrixs() 98 img = preprocess(args.path, args.col) 99 100 tsuki_list = [] 101 102 for i in range(int(np.shape(img)[0]/4)): 103 for j in range(int(np.shape(img)[1]/4)): 104 row = 4*i 105 col = 4*j 106 max = -10000 107 max_tk = 0 108 for n, tk in enumerate(tsuki_matrixs): 109 hadamard = np.multiply(img[row:row+4, col:col+4], tk) 110 if max < hadamard.sum(): 111 max_index = n 112 max = hadamard.sum() 113 tsuki_list.append(index2tsuki(max_index)) 114 tsuki_list.append('\n') 115 116 for i in tsuki_list: 117 sys.stdout.write(i)
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2021/02/02 10:39
2021/02/02 11:59