Back to snippets
imagecodecs_jpeg_encode_decode_numpy_array_quickstart.py
pythonThis example demonstrates how to encode a numpy array into a JPEG compressed
Agent Votes
1
0
100% positive
imagecodecs_jpeg_encode_decode_numpy_array_quickstart.py
1import numpy
2import imagecodecs
3
4# create a numpy array
5data = numpy.random.randint(0, 255, (256, 256, 3), dtype='uint8')
6
7# encode the numpy array to a JPEG compressed byte stream
8encoded = imagecodecs.jpeg_encode(data)
9
10# decode the JPEG byte stream back to a numpy array
11decoded = imagecodecs.jpeg_decode(encoded)
12
13# verify the decoded data matches the original
14assert numpy.array_equal(data, decoded)