Image data type conversion [Python]

Overview

When handling image data in Python, it may be handled by "numpy (opencv)", "pillow", and "byte". A memorandum for converting each type.

numpy-> bytes (unverified)

num_bytes = num_numpy.tobytes()

numpy -> pil

num_pil = Image.fromarray(num_numpy)

pil -> bytes

num_byteio = io.BytesIO()
num_byteio.save(num_pil, format='png')#Temporarily make it 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)

Recommended Posts

Image data type conversion [Python]
Python data type summary memo
Python numeric type
python image processing
Use PostgreSQL data type (jsonb) from Python
Data analysis python
Python learning basics ~ What is type conversion? ~
Python2 string type
Python: Preprocessing in machine learning: Data conversion
Python # string type
[python] Read data
Relationship between Firestore and Go data type conversion
First python (data type, control statement, function definition)
[Python] Conversion memo between time data and numerical data
Data analysis with python 2
[Python] 2's complement conversion
Python Data Visualization Libraries
Data analysis using Python 0
Data analysis overview python
First Python image processing
Data cleaning using Python
Image format in Python
python unix-time <-> datetime conversion
Image processing with Python
Python data analysis template
[Python tutorial] Data structure
[Python] Sorting Numpy data
Check Python # type identity
Data analysis with Python
Whole type conversion for each dataframe column in python
Image processing with Python (Part 2)
Sample data created with python
[Introduction to cx_Oracle] (Part 6) DB and Python data type mapping
My python data analysis container
Handle Ambient data in Python
data structure python push pop
Python for Data Analysis Chapter 4
Image editing with python OpenCV
Display UTM-30LX data in Python
Get Youtube data with python
Python: 3D array image (numpy.array)
Python --Check type of values
Sorting image files with Python (2)
Sorting image files with Python (3)
Data Science Cheat Sheet (Python)
[Python] Notes on data analysis
Python application: Data cleansing # 3: Use of OpenCV and preprocessing of image data
My python data analytics environment
Image processing with Python (Part 1)
Python application: data visualization # 2: matplotlib
Tweet with image in Python
Sorting image files with Python
Python data analysis learning notes
Python immutable type int memo
Image processing with Python (Part 3)
Image processing by python (Pillow)
Image Processing Collection in Python
[Python] Plot time series data
Python for Data Analysis Chapter 2
[Python] Use JSON format data as a dictionary type object
Data analysis using python pandas