Python PIL | tobytes() 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.
Image.tobytes() Return image as a bytes object
Syntax: Image.tobytes(encoder_name=’raw’, *args)
Parameters:
encoder_name – What encoder to use. The default is to use the standard “raw” encoder.
args – Extra arguments to the encoder.
Returns: A bytes object.
Image Used:
from PIL import Image
img = Image.open(r"C:\Users\System-Pc\Desktop\tree.jpg")
img.tobytes("xbm", "rgb")
print(img)
|
Output:
PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=259x194 at 0x2D39DED2BE0
Another Example: Here using same image with change in encoder name to hex.
Image Used:
from PIL import Image
img = Image.open(r"C:\Users\System-Pc\Desktop\tree.jpg")
img.tobytes("hex", "rgb")
print(img)
|
Output:
PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=259x194 at 0x27845B91BE0
Last Updated :
02 Aug, 2019
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...