LoginSignup
3
5

More than 5 years have passed since last update.

OpenCVで開いた画像をPILで扱えるように変換する

Posted at

OpenCVで処理した画像をPILに変換する際に詰まったので備忘録として残しておきます。
関数cv2pilでOpenCV形式の画像をBGRからRGBに変換し、PIL形式に変換後もRGB(8bit x 3)に変換するように実装しました。

cv2pil.py
import cv2
from PIL import Image

def cv2pil(image_cv):
    image_cv = cv2.cvtColor(image_cv, cv2.COLOR_BGR2RGB)
    image_pil = Image.fromarray(image_cv)
    image_pil = image_pil.convert('RGB')

    return image_pil

if __name__ == '__main__':
    image_cv = cv2.imread('test.jpg')
    image_pil = cv2pil(image_cv)

    image_pil.show()
3
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
5