Thursday, October 2, 2025
HomeLanguagesPython PIL | Image.frombytes() Method

Python PIL | Image.frombytes() Method

PIL is the Python Imaging Library which provides the python interpreter with image editing capabilities. The Image module provides a class with the same name which is used to represent a PIL image. The module also provides a number of factory functions, including functions to load images from files, and to create new images.

PIL.Image.frombytes() Creates a copy of an image memory from pixel data in a buffer. In its simplest form, this function takes three arguments (mode, size, and unpacked pixel data).

Syntax: PIL.Image.frombytes(mode, size, data, decoder_name=’raw’, *args)

Parameters:
mode – The image mode. See: Modes.
size – The image size.
data – A byte buffer containing raw data for the given mode.
decoder_name – What decoder to use.
args – Additional parameters for the given decoder.

Returns: An Image object.




   
  
# importing image object from PIL
from PIL import Image
  
# using tobytes data as raw for frombyte function
tobytes = b'xd8\xe1\xb7\xeb\xa8\xe5 \xd2\xb7\xe1'
img = Image.frombytes("L", (3, 2), tobytes)
  
# creating list 
img1 = list(img.getdata())
print(img1)


Output:

[120, 100, 56, 225, 183, 235]

Another Example: Here we use different raw in tobytes.




   
  
# importing image object from PIL
from PIL import Image
  
# using tobytes data as raw for frombyte function
tobytes = b'\xbf\x8cd\xba\x7f\xe0\xf0\xb8t\xfe'
img = Image.frombytes("L", (3, 2), tobytes)
  
# creating list 
img1 = list(img.getdata())
print(img1)


Output:

[191, 140, 100, 186, 127, 224]
RELATED ARTICLES

Most Popular

Dominic
32331 POSTS0 COMMENTS
Milvus
85 POSTS0 COMMENTS
Nango Kala
6703 POSTS0 COMMENTS
Nicole Veronica
11867 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11927 POSTS0 COMMENTS
Shaida Kate Naidoo
6818 POSTS0 COMMENTS
Ted Musemwa
7080 POSTS0 COMMENTS
Thapelo Manthata
6775 POSTS0 COMMENTS
Umr Jansen
6776 POSTS0 COMMENTS