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:
# Importing Image module from PIL package from PIL import Image # creating a image object img = Image.open(r"C:\Users\System-Pc\Desktop\tree.jpg") # using tobytes 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:
# Importing Image module from PIL package from PIL import Image # creating a image object img = Image.open(r"C:\Users\System-Pc\Desktop\tree.jpg") # using tobytes img.tobytes("hex", "rgb") print(img) |
Output:
PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=259x194 at 0x27845B91BE0
Recommended Posts:
- Python | Numpy matrix.tobytes()
- Numpy recarray.tobytes() function | Python
- Numpy ndarray.tobytes() function | Python
- Python PIL | GaussianBlur() method
- Python PIL | Kernel() method
- Python PIL | BoxBlur() method
- Python PIL | getbands() method
- Python PIL | logical_and() and logical_or() method
- Python PIL | ImageChops.subtract() method
- Python PIL | UnsahrpMask() method
- Python PIL | ImageChops.subtract() and ImageChops.subtract_modulo() method
- Python PIL | Image.crop() method
- Python PIL | ImageOps.expand() method
- Python PIL | ImageEnhance.Color() and ImageEnhance.Contrast() method
- Python PIL | ImageGrab.grabclipboard() method
- Python PIL | RankFilter() method
- Python PIL | ImageChops.screen() and ImageChops.offset() method
- Python PIL | MedianFilter() and ModeFilter() method
- Python PIL | Image.new() method
- Python PIL | getbands() and getextrema() method
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

