.frombytes()
Published Mar 21, 2025
Contribute to Docs
The .frombytes()
method in Pillow creates an image from raw byte data. It reconstructs an image using a specified mode, size, and optional decoder arguments. This method is useful when working with raw image data that needs to be converted into a Pillow Image
object.
Syntax
Image.frombytes(mode, size, data, decoder_name='raw', *args)
Parameters:
mode
(str): Specifies the color mode of the image (e.g.,"RGB"
,"RGBA"
,"L"
for grayscale).size
(tuple): The dimensions of the image as(width, height)
data
(bytes): The raw image data for the given mode.decoder_name
(str, optional, default='raw'
): Specifies the decoder used to interpret the raw byte data.*args
(optional): Additional arguments for the decoder.
Example
The example below creates an RGB image from raw byte data:
from PIL import Image# Create a new image (64x64) with a gradient patternsize = (64, 64)# Generate pixel data (simple color gradient)pixels = []for y in range(size[1]):for x in range(size[0]):r = (x * 4) % 256 # Red intensity increases horizontallyg = (y * 4) % 256 # Green intensity increases verticallyb = ((x + y) * 2) % 256 # Blue is a mix of both coordinatespixels.extend([r, g, b])# Convert list to bytes and create an image from raw datadata = bytes(pixels)img = Image.frombytes("RGB", size, data)# Display the imageimg.show()
The above code generates the below output image:
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Python:Pillow on Codecademy
- Career path
Data Scientist: Machine Learning Specialist
Machine Learning Data Scientists solve problems at scale, make predictions, find patterns, and more! They use Python, SQL, and algorithms.Includes 27 CoursesWith Professional CertificationBeginner Friendly95 hours - Course
Learn Python 3
Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.With CertificateBeginner Friendly23 hours