Python PIL | Image.frombytes () method

Python Methods and Functions

PIL.Image.frombytes() Creates a copy of image memory from the pixel data in the buffer. In its simplest form, this function takes three arguments (mode, size, and decompressed 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.

 

 
# import image object from PIL

from PIL import Image

  
# use tobyte data as raw data for frombyte function

tobytes = b < / code> 'xd8xe1xb7xebxa8xe5 xd2xb7xe1'

img = Image.frombytes ( "L" , ( 3 , 2 ), tobytes)

 
# create a list

img1 = list (img.getdata ( ))

print (img1)

Exit :

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

Another example: here we use different raw tobytes.

< code class = "undefined spaces">  

 
# import image object from PIL

from PIL import Image

 
# using tobyte data in as raw for frombyte function

tobytes = b 'xbfx8cdxbax7fxe0xf0xb8txfe'

img = Image.frombytes ( "L" , ( 3 , 2 ), tobytes)

 
# created adding a list

img1 = list (img.getdata ())

print ( img1)

Output:

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




Tutorials