16
14

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Pillowで読み込んだデータをOpenCVで扱えるように変換するとブルーになる話

Last updated at Posted at 2015-01-22

OpenCVで細かい処理はやりたいけどファイルの入出力はPillowでやりたい!
というかOpenCVで保存するとファイルでけぇ・・。
って時ありますよね。

そんなわけでPillow -> OpenCVとOpenCV -> Pillowするサンプルを書いてみました。

from PIL import Image
import cv2
import numpy as np

#PILデータで画像を読み込む
im = Image.open('t.jpg')

#OpenCVデータに変換
ocv_im = np.asarray(im)

#OpenCVで保存
cv2.imwrite("t_ocv.jpg",ocv_im)

#PILデータへ変換
pil_im = Image.fromarray(ocv_im)
#PILで保存
pil_im.save("t_pil.jpg")

で出てきたファイル。

t_ocv.jpg
t_ocv.jpg

t_pil.jpg
t_pil.jpg

なぜかt_ocv.jpgが限りなく青に近いブルー・・・。

調べてくとOpenCVさんはカラーがRGBではなくBGR・・・なんだそれ・・・。

というわけでocv_imをRGBに変換すれば問題なし。

cv2.imwrite("t_ocv.jpg",ocv_im)

cv2.imwrite("t_ocv.jpg",ocv_im[:, :, ::-1].copy())

解決。以上。

16
14
2

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
16
14

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?