The Wayback Machine - https://web.archive.org/web/20240811144832/https://www.geeksforgeeks.org/python-pil-tobytes-method/
Open In App

Python PIL | tobytes() Method

Last Updated : 02 Aug, 2019
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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

Previous Article
Next Article

Similar Reads

Python | Numpy matrix.tobytes()
With the help of Numpy matrix.tobytes() method, we can find the byte code for the matrix by using the matrix.tobytes() method. Syntax : matrix.tobytes() Return : Return byte code for matrix Example #1 : In this example we can see that by using matrix.tobytes() method we are able to find the byte code for the given matrix. # import the important mod
1 min read
Numpy recarray.tobytes() function | Python
In numpy, arrays may have a data-types containing fields, analogous to columns in a spreadsheet. An example is [(a, int), (b, float)], where each entry in the array is a pair of (int, float). Normally, these attributes are accessed using dictionary lookups such as arr['a'] and arr['b']. Record arrays allow the fields to be accessed as members of th
3 min read
Python PIL | GaussianBlur() method
PIL is the Python Imaging Library which provides the python interpreter with image editing capabilities. The ImageFilter module contains definitions for a pre-defined set of filters, which can be used with the Image.filter() method. PIL.ImageFilter.GaussianBlur() method create Gaussian blur filter. Syntax: PIL.ImageFilter.GaussianBlur(radius=5) Par
1 min read
Python PIL | Kernel() method
PIL is the Python Imaging Library which provides the python interpreter with image editing capabilities. The ImageFilter module contains definitions for a pre-defined set of filters, which can be used with the Image.filter() method. PIL.ImageFilter.Kernel() Create a convolution kernel. The current version only supports 3x3 and 5x5 integer and float
2 min read
Python PIL | BoxBlur() method
PIL is the Python Imaging Library which provides the python interpreter with image editing capabilities. The ImageFilter module contains definitions for a pre-defined set of filters, which can be used with the Image.filter() method.PIL.ImageFilter.BoxBlur() Blurs the image by setting each pixel to the average value of the pixels in a square box ext
2 min read
Python PIL | getbands() method
PIL is the Python Imaging Library which provides the python interpreter with image editing capabilities. PIL.Image.getbands() method returns a tuple containing the name of each band in the image.For example, getbands on an RGB image returns (“R”, “G”, “B”). Syntax: PIL.Image.getbands() Parameters: no arguments Returns: A tuple containing band names
1 min read
Python PIL | logical_and() and logical_or() method
PIL is the Python Imaging Library which provides the python interpreter with image editing capabilities. The ImageChops module contains a number of arithmetical image operations, called channel operations (“chops”). These can be used for various purposes, including special effects, image compositions, algorithmic painting, and more. PIL.ImageChops.
1 min read
Python PIL | ImageChops.subtract() method
PIL is the Python Imaging Library which provides the python interpreter with image editing capabilities. The ImageChops module contains a number of arithmetical image operations, called channel operations (“chops”). These can be used for various purposes, including special effects, image compositions, algorithmic painting, and more. PIL.ImageChops.
1 min read
Python PIL | UnsharpMask() method
PIL is the Python Imaging Library which provides the python interpreter with image editing capabilities. The ImageFilter module contains definitions for a pre-defined set of filters, which can be used with the Image.filter() method.PIL.ImageFilter.UnsharpMask() method applies the Unsharp mask filter to the input image. Syntax: PIl.ImageFilter.Unsha
2 min read
Python PIL | ImageChops.subtract() and ImageChops.subtract_modulo() method
PIL is the Python Imaging Library which provides the python interpreter with image editing capabilities. ImageChops.subtract() method - This method subtracts two images, dividing the result by scale and adding the offset. If omitted, scale defaults to 1.0, and offset to 0.0. Syntax: ImageChops.subtract(image1, image2, scale = 1.0, offset = 0) Param
2 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg