Category: Python

  • Telegram으로 그림 파일 보내기

    Telegram Bot으로 그림 파일을 보내면 이미지를 손실 압축해서 전송된다. 그래서 화질이 나빠진다. 이미지 그 자체를 보내는 것으로 처리해야 한다.

    img = open(filename, 'rb')
    response = requests.post(
    	f'https://api.telegram.org/bot{TOKEN}/sendDocument',
    	data = {'chat_id' : CHAT_ID,
    		   'document' : 'attach://file'},
    	f'https://api.telegram.org/bot{TOKEN}/sendPhoto?chat_id={CHAT_ID}',
    	files={'file':img},
    	timeout=5)
  • 변수 존재 여부 확인

    변수 A가 존재하는지 확인하는 방법. 나는 globals()을 이용했다. 사소한 주의점은 코딩할 때에는 변수를 따옴표로 지정해 주어야 한다.

    'A' in globals()
  • blank mp3 만들기

    Bing 에 물어봤더니 친절히 다음 코드를 알려 주었다. 아쉬운 점은 ffmpeg 가 설치된 환경에서 정상적으로 동작한다는 것을 알려주지는 않았다.

    from pydub import AudioSegment
    
    # create 1 second of blank audio
    blank = AudioSegment.silent(duration=1000)
    
    # export the blank audio as an mp3 file
    blank.export("blank.mp3", format="mp3")

  • HU로 변환하기

    DICOM 이미지의 값을 HU로 변환하는 방법.

    import pydicom
    dicom = pydicom.dcmread(filepath)
    dicom_hu = dicom.pixel_array * dicom.RescaleSlope + dicom.RescaleIntercept

    CT의 경우 slope 이 1이고 intercept 가 -1024이다. 즉, 수직 이동만 하는 것이다. 하지만 이렇게 변환해서 하는게 훨씬 더 academic 한 것 같다.