LoginSignup
1
2

More than 1 year has passed since last update.

[Python cv2] img = cv2.imread(input_file)。このimgってファイルからしか作れないのか

Posted at

概要

Pythonでcv2を使う時 imread が便利なので使うことが多いです。

img = cv2.imread(input_file)

ここで作られる imgimread でお手軽に作れます。

しかし、すでに読み込み済の画像のバイナリデータから、上記の img 相当のオブジェクトを作る関数が見つけられず困っていました。

結論

import numpy as np

# dataは画像のデータ
img_array = np.frombuffer(data, dtype=np.uint8)
img = cv2.imdecode(img_array, cv2.IMREAD_COLOR) # このimgがimreadで取得したのと同じimg

(詳細は下記「参考」にも書いたURLをご確認ください。)

補足

# 上記dataは以下のような形で取得した画像のデータ
with open(input_file, "rb") as f:
    data = f.read()

参考

https://www.my-memorandum.tk/img2binarytext-opencv
この記事の内容を自分用にメモしただけなので内容はまったく同じです。
大変勉強になりました、ありがとうございます。

1
2
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
1
2