Open In App

Python PIL | Image.frombytes() Method

Improve
Improve
Like Article
Like
Save
Share
Report

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]


Last Updated : 07 Aug, 2019
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads