Last active
July 28, 2021 05:49
-
-
Save nockn/e61e87a74402903e867f5916f9bfe346 to your computer and use it in GitHub Desktop.
[note] How to convart from cv2image to pillowimage
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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