from palette import colorful_colors

PIL 라이브러리를 이용해서 uint8 -> int32로 바꾸기 본문

연구활동/기타 TIP

PIL 라이브러리를 이용해서 uint8 -> int32로 바꾸기

colorful-palette 2023. 5. 2. 19:59

uint8로 된 depth 이미지를 int32로 바꾸는 예시. 

cv2를 사용하면 자꾸 다시 uint8로 돌아가길래 PIL의 Image를 사용했다. 

from PIL import Image

import numpy as np
import matplotlib.pyplot as plt

depth_image = np.array(Image.open("./depth_25.png"))
depth_image[depth_image >= 0.00392] = 1
depth_image[depth_image < 0.00392] = 0

# 변환
depth_image_uint8 = depth_image
depth_image_int32 = depth_image_uint8.astype(np.int32)
print(depth_image_int32.dtype)

#PIL Image 객체로 변환.
depth_image_int32 = Image.fromarray(depth_image_int32)  
depth_image_int32.save("depth_25_1.png")


# 잘 변했는지 꺼내서 확인하기
image = np.array(Image.open("./depth_25_1.png"))
print(image.dtype)
plt.imshow(image)
plt.show()