Skip to content
  • (+91) 9409548155
  • support@appdividend.com
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Menu
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Python

How to Convert Python Bytes to Numpy Array

  • 29 Oct, 2025
  • Com 0
Converting Python Bytes to Numpy Array

The fastest and most correct way to convert a Python bytes object to a numpy array is to use the np.frombuffer() method. It is a zero-copy method and interprets a buffer as a 1D array.

Convert Python Bytes to Numpy Array using np.frombuffer() method

import numpy as np

bytes = b'\x0b\x00\x00\x00\x13\x00\x00\x00\x15\x00\x00\x00'

print(bytes)
# Output: b'\x0b\x00\x00\x00\x13\x00\x00\x00\x15\x00\x00\x00'

print(type(bytes))
# Output: <class 'bytes'>

numpy_array = np.frombuffer(bytes, dtype=np.int32)

print(numpy_array)
# Output: [11 19 21]

print(type(numpy_array))
# Output: <class 'numpy.ndarray'>

In this code, we have defined bytes and want to convert them to a numpy array of integers. So, we passed it to the np.frombuffer() method and it returns a proper numpy array.

We verified the data types before and after conversion using the type() method.

Floating-point conversion

Let’s say we have a bytes object and we want to convert a numpy array of float values.

Floating-point conversion from bytes to numpy array

import numpy as np

bytes = b'\x00\x00\x80\x3f\x00\x00\x20\x41'

print(bytes)
# Output: b'\x00\x00\x80\x3f\x00\x00\x20\x41'

print(type(bytes))
# Output: <class 'bytes'>

numpy_array = np.frombuffer(bytes, dtype=np.float32)

print(numpy_array)
# Output: [ 1. 10.]

print(type(numpy_array))
# Output: <class 'numpy.ndarray'>

It specifies float32 for 4-byte floats.

Image/Pixel Data (e.g., Grayscale Bytes to 2D Array)

Let’s say we have an input of uint8 pixels as bytes, and we want an output of a 2D numpy array. For that, we need to reshape the final array into a two-dimensional array using the .reshape() method.

Image_Pixel Data (e.g., Grayscale Bytes to 2D Array)

import numpy as np

image_bytes = b'\x00\xFF\x00\xFF\xFF\x00\xFF\x00\x00\xFF\x00\xFF\xFF\x00\xFF\x00'

print(image_bytes)
# Output: b'\x00\xFF\x00\xFF\xFF\x00\xFF\x00\x00\xFF\x00\xFF\xFF\x00\xFF\x00'

print(type(image_bytes))
# Output: <class 'bytes'>

array_2d = np.frombuffer(image_bytes, dtype=np.uint8).reshape(4, 4)

print(array_2d)
# Output:
# [[  0 255   0 255]
#  [255   0 255   0]
#  [  0 255   0 255]
#  [255   0 255   0]]

print(type(array_2d))
# Output: <class 'numpy.ndarray'>

Rare conversion: np.fromiter() (For Iterable Bytes, Rare)

If your bytes are not raw numeric bytes (e.g., UTF-8 “123”, not 0x7B). The np.frombuffer() method will misinterpret it; in this case, use np.fromiter().

import numpy as np

bytes_obj = b'\x01\x02\x03'

print(bytes_obj)
# Output: b'\x01\x02\x03'

print(type(bytes_obj))
# Output: <class 'bytes'>

arr_1d = np.fromiter(bytes_obj, dtype=np.uint8, count=len(bytes_obj))

print(arr_1d)
# Output: [1 2 3]

print(type(arr_1d))
# Output: <class 'numpy.ndarray'>

It treats bytes as iterable, and that is why it is slower; use only if bytes are lazily generated.

That’s all!

Post Views: 2
Share on:
Krunal Lathiya

With a career spanning over eight years in the field of Computer Science, Krunal’s expertise is rooted in a solid foundation of hands-on experience, complemented by a continuous pursuit of knowledge.

How to Convert Pandas DataFrame to Python List
numpy.ndarray.tobytes: Converting Numpy Array to Python Bytes

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Address: TwinStar, South Block – 1202, 150 Ft Ring Road, Nr. Nana Mauva Circle, Rajkot(360005), Gujarat, India

Call: (+91) 9409548155

Email: support@appdividend.com

Online Platform

  • Pricing
  • Instructors
  • FAQ
  • Refund Policy
  • Support

Links

  • About Us
  • Contact Us
  • Privacy Policy
  • Terms of services

Tutorials

  • Angular
  • React
  • Python
  • Laravel
  • Javascript
Copyright @2024 AppDividend. All Rights Reserved
Appdividend