200113 TIL

이미지 가로 세로 크기 구하기

from PIL import Image
image = Image.open('image_file.phg')
width, height = image.size

via

이미지 내에서 일치하는 이미지를 찾기

opencv의 template matching를 사용한다.

import cv2
import numpy as np

img = cv2.imread('messi5.jpg',0) # 큰 거
img2 = img.copy()
template = cv2.imread('template.jpg',0) # 작은 거
res = cv2.matchTemplate(img,template,'cv2.TM_SQDIFF_NORMED')

이미지가 없으면 에러가 난다.

이런 방법 도 있다고 한다.

딕셔너리 내에서 키값 기준으로 최빈값 찾기

input_dict = {'A': 1963, 'B': 1963, 'C': 1964, 'D': 1964, 'E': 1964, 'F': 1965, 'G': 1965, 'H': 1966, 'I': 1967, 'J': 1967, 'K': 1968, 'L': 1969 ,'M': 1969, 'N': 1970}

collections 모듈을 사용하는 방법

from collections import Counter 
value, count = Counter(input_dict.values()).most_common(1)[0]

다른 딕셔너리를 사용하는 방법

my_counter_dict = {}
for v in input_dict.values():
    my_counter_dict[v] = my_counter_dict.get(v, 0)+1

# Value hold by `my_counter_dict`:
#  {1963: 2, 1964: 3, 1965: 2, 1966: 1, 1967: 2, 1968: 1, 1969: 2, 1970: 1}

딕셔너리 없이 그냥 찾는 방법

values_list = list(input_dict.values())
max(set(values_list), key=values_list.count)
1964

via StackOverflow

PermissionError: [Errno 1] Operation not permitted:

PermissionError: [Errno 1] Operation not permitted: 에러를 마주칠 때가 있다. 이번 같은 경우는 os.remove() 에 파일이 아니라 폴더를 넣어서 그랬는데 폴더는 os.rmdir() 를 사용한다. if os.path.isdir() 를 미리 쓰면 될 듯.


use setter for headless

최근에 브라우저들을 헤드리스로 운용하는데 이런 워닝 메시지들이 자꾸 보이기 시작했다.

DeprecationWarning: use setter for headless property instead of set_headless

무슨 헤드리스 프로퍼티를 주려면 세터(setter)를 쓰라나. 뭔 소린가 찾아봐야지 생각만 하고 한동안 냅뒀다가 새해를 맞이하여 워닝 메세지를 검색해보았다.

결론은 이 StackOverflow 답글 에 따르면

options = webdriver.ChromeOptions()
options.set_headless()

대신에

options.headless = True

ssh 키를 제거하는 안전한 방법

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@       WARNING: POSSIBLE DNS SPOOFING DETECTED!          @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
The RSA host key for foo-bar.net has changed,
and the key for the corresponding IP address 127.0.0.1
is unchanged. This could either mean that
DNS SPOOFING is happening or the IP address for the host
and its host key have changed at the same time.
Offending key for IP in /home/user/.ssh/known_hosts:6
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@    WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!     @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!

ssh 키가 안 맞는다고 뱉는 경우.

6번째 줄에 어느 줄의 키가 안 맞는지 적혀있으니 직접 ~/.ssh/known_hosts 파일을 열어서 수정해도 되겠지만 귀찮으니 파일을 열지 않고 해결할 수 있는 방법을 알고 싶었다.

man ssh-keygen

-R hostname

Removes all keys belonging to hostname from a known_hosts file. This option is useful to delete hashed hosts (see the -H option above).

말인즉슨 ssh-keygen -R 대충호스트네임 을 입력해서 충돌하는 키를 지울 수 있다고 한다. 다만 이 경우 같은 호스트네임을 모두 지운다. 포트가 있을 경우 반드시 괄호를 칠 것 [localhost]:portnumber.

via StackOverflow


ls 역시간순 정렬하기

ls 명령어를 이용해서 디렉터리 안의 항목을 리스트 할 수 있다. -t 플래그를 주면 마지막 업데이트 시각을 기준으로 정렬하고 -u 플래그로 마지막 액세스 시각을 기준으로 정렬할 수 있다. 그리고 -U 플래그로 생성일 기준 정렬을 할 수 있다.

     -t      Sort by time modified (most recently modified first) before sorting the operands by lexicographi-
             cal order.

     -u      Use time of last access, instead of last modification of the file for sorting (-t) or long print-
             ing (-l).

     -U      Use time of file creation, instead of last modification for sorting (-t) or long output (-l).

끝으로 -r 플래그로 역순 정렬을 할 수 있다. (StackOverflow)

그러니까 아래와 같은 명령어로 역시간순, 그러니까 가장 최근에 업데이트한 파일이 보이도록 ls 할 수 있다. 비슷하게 uU 를 활용할 수 있겠다.

ls -t -r

요걸 ~/.bashrcalias lst="ls -t -r" 해두었다.