4
3

More than 3 years have passed since last update.

画像データ 型変換 【Python】

Last updated at Posted at 2020-04-02

概要

Pythonにて画像データを扱うとき、「numpy(opencv)」「pillow」「byte」にて扱うことがある。それぞれの型変換させるための備忘録。

numpy -> bytes

_, num_bytes = cv2.imencode('.jpeg', num_numpy)
num_bytes = num_bytes.tobytes()

numpy -> pil

num_pil = Image.fromarray(num_numpy)

pil -> bytes

num_byteio = io.BytesIO()
num_byteio.save(num_pil, format='png')# 仮にpngにしている
num_bytes = num_byteio.getvalue()

pil -> numpy

num_numpy = np.asarray(num_pil)

bytes -> numpy

num_byteio = io.BytesIO(num_bytes)
with Image.open(num_byteio) as img:
  num_numpy = np.asarray(img)

bytes -> pil

num_byteio = io.BytesIO(num_bytes)
num_pil = Image.open(num_byteio)
4
3
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
4
3