pbj0812의 코딩 일기

[PYTHON] PIL 을 이용한 이미지 생성시간 추출 본문

ComputerLanguage_Program/PYTHON

[PYTHON] PIL 을 이용한 이미지 생성시간 추출

pbj0812 2020. 10. 18. 22:52

0. 목표

 - PIL을 이용한 이미지 생성시간 추출

  * os.path.getmtime()을 이용해도 됨

1. 실습

 1) library 호출

from PIL import Image

 2) 이미지 호출

imgfile = './kalimba_ori.jpeg'
img = Image.open(imgfile)

 3) 메타 데이터 확보

meta_data = img._getexif()

 4) 메타 데이터 확인

  - 36867 이 생성시각

print(meta_data)

  - 결과

{36864: b'0220', 37121: b'\x01\x02\x03\x00', 37377: (491, 100), 36867: '2020:10:17 00:42:57', 36868: '2020:10:17 00:42:57', 37378: (153, 100), 37379: (88, 100), 37380: (0, 10), 37381: (153, 100), 37383: 2, 37385: 0, 37386: (430, 100), 37510: b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00', 40961: 1, 40962: 4032, 41990: 0, 37520: '0118', 37521: '0118', 37522: '0118', 40963: 1960, 272: 'SM-N950N', 274: 6, 531: 1, 271: 'samsung', 33434: (1, 30), 40965: 908, 282: (72, 1), 33437: (17, 10), 41729: b'\x01\x00\x00\x00', 283: (72, 1), 42016: 'G12LLKA02SM G12LLKL01GM\n', 34850: 2, 34855: 160, 296: 2, 41986: 0, 40960: b'0100', 41987: 0, 305: 'N950NKSU5DTG1', 306: '2020:10:17 00:42:57', 41988: (0, 0), 41989: 26, 41992: 0, 41993: 0, 41994: 0, 34665: 202, 37500: b'\x07\x00\x01\x00\x07\x00\x04\x00\x00\x000100\x02\x00\x04\x00\x01\x00\x00\x00\x00 \x01\x00\x0c\x00\x04\x00\x01\x00\x00\x00\x00\x00\x00\x00\x10\x00\x05\x00\x01\x00\x00\x00Z\x00\x00\x00@\x00\x04\x00\x01\x00\x00\x00\x00\x00\x00\x00P\x00\x04\x00\x01\x00\x00\x00\x01\x00\x00\x00\x00\x01\x03\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'}

 5) 생성시각 확보

make_time = meta_data[36867]
print(make_time)

  - 결과

2020:10:17 00:42:57

 6) 연월일시분초 분리

tmp_time = make_time.split() 
print(tmp_time)
yymmdd = tmp_time[0].split(':') 
hhmmss = tmp_time[1].split(':')
print(yymmdd)
print(hhmmss)

  - 결과

['2020:10:17', '00:42:57']
['2020', '10', '17']
['00', '42', '57']

2. 응용

 - 타임스탬프 클론코딩에 코드 변형

 - 위 코드에 이어서...

 - 연산을 위하여 int로 변형하여 계산

import cv2
import numpy as np
from PIL import Image
import math
import time
import datetime

image = cv2.imread(imgfile)

x, y, z = np.shape(image)

if x > y:
    min_len = y
else:
    min_len = x
    
image2 = image[int(x/2) - int((min_len)/2):int(x/2) + int((min_len)/2), int(y/2) - int((min_len)/2):int(y/2) + int((min_len)/2), :]

image_test = cv2.cvtColor(image2, cv2.COLOR_BGR2RGB) # Converting BGR to RGB

x2, y2, z2 = np.shape(image2)

color = (255, 255, 255)

thickness = math.ceil(min_len / 50)

cv2.rectangle(image2, (int(0.05 * x2), int(0.05 * y2)), (int(x2 - 0.05 * x2), int(y2 - 0.05 * y2)), color, thickness)

image_test = cv2.cvtColor(image2, cv2.COLOR_BGR2RGB) # Converting BGR to RGB

########## 수정 시작 ##########

if int(hhmmss[0]) < 13:
    AM_OR_PM = ' AM'
    t_hour = "%02d" %(int(hhmmss[0]))
else:
    AM_OR_PM = ' PM'
    t_hour = "%02d"%(int(hhmmss[0]) - 12)
    
t_min = "%02d"%(int(hhmmss[1]))
first_line = t_hour + ':' + t_min + AM_OR_PM

s = "%04d-%02d-%02d"%(int(yymmdd[0]), int(yymmdd[1]), int(yymmdd[2]))
dt = datetime.datetime.strptime(s, '%Y-%m-%d')
second_line = dt.strftime('%B %d, %Y')

########## 수정 종료 ##########

cv2.putText(image2, first_line, (int(0.10 * x2), int(0.8 * y2)), cv2.FONT_HERSHEY_PLAIN, int(thickness/4), color, int(thickness/4), 1)
cv2.putText(image2, second_line, (int(0.10 * x2), int(0.85 * y2)), cv2.FONT_HERSHEY_PLAIN, int(thickness/8), color, int(thickness/8), 1)

image_test = cv2.cvtColor(image2, cv2.COLOR_BGR2RGB) # Converting BGR to RGB
display(Image.fromarray(image_test))

cv2.imwrite("./result.jpeg", image2)

  - 결과

3. 참고

 - PYTHON으로 이미지파일의 EXIF 정보 다루기

 - Get date and time when photo was taken from EXIF data using PIL

 - Python | os.path.getmtime() method

 - 파일 생성일이랑 수정일 알아내는 법

 - 4) 객체 정보 확인하기

Comments