Skip to content

Instantly share code, notes, and snippets.

@nockn
Last active July 28, 2021 05:49
Show Gist options
  • Save nockn/e61e87a74402903e867f5916f9bfe346 to your computer and use it in GitHub Desktop.
Save nockn/e61e87a74402903e867f5916f9bfe346 to your computer and use it in GitHub Desktop.
[note] How to convart from cv2image to pillowimage
import cv2
from PIL import Image
def conver_cv2pilimage(cvImage):
"""cv2(np.arry)の画像をpillowに変換する
Args:
cvImage(numpy.ndarray): OpenCVで読み込んだ画像
Returns:
pilImage(PIL.Image.Image): Pillow形式の画像
"""
pilImage = cvImage.copy()
if pilImage.ndim == 2: # モノクロ
pass
elif pilImage.shape[2] == 3: # カラー
pilImage = cv2.cvtColor(pilImage, cv2.COLOR_BGR2RGB)
elif pilImage.shape[2] == 4: # 透過
pilImage = cv2.cvtColor(pilImage, cv2.COLOR_BGRA2RGBA)
pilImage = Image.fromarray(pilImage)
return pilImage
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment