Converting an OpenCV image into PIL image using Python

share link

by kanika dot icon Updated: Apr 6, 2023

technology logo
technology logo

Solution Kit Solution Kit  

OpenCV (Open Source Computer Vision) is free under the open-source BSD license. This library is used for face detection, object detection, motion estimation, image recognition, segmentation, and others. OpenCV was designed for computational efficiency and strongly focused on real-time applications. 


You can use the Pillow library to convert an OpenCV image to a PIL image in Python. The process involves using the tostring method in OpenCV to get the image as an array of bytes and then using the image.frombuffer method in Pillow to create the PIL image from the array. 


  • PIL (Python Imaging Library) is an open-source library for image processing tasks that require sophisticated file format support, an efficient internal representation, and powerful image manipulation capabilities. 


Here is an example of how an OpenCV image can be converted to PIL. 



Fig 1: Preview of the output that you will get on running this code from your IDE

Code


In this solution, we use the cvtColor Function of the Opencv library

Instructions

Follow the steps carefully to get the output easily.

  1. Install Jupyter Notebook on your computer.
  2. Open terminal and install the required libraries with following commands.
  3. Install numpy - pip install numpy.
  4. Install opencv - pip install opencv.
  5. Install pillow - pip install pillow.
  6. Copy the code using the "Copy" button above, and paste it into your IDE's Python file.
  7. Add "im_pil.show()" in last to view the output.
  8. Use '/' instead of '\' in the image path or add 'r' as prefix to the path like (r"path").
  9. Run the file to convert the OpenCV image into PIL image.



I hope you found this useful. I have added the link to dependent libraries, version information in the following sections.


I found this code snippet by searching for "How to convert an OpenCV image to PIL image in Python?" in kandi. You can try any such use case!

Dependent Libraries

Pillowby python-pillow

Python doticonstar image 10903 doticonVersion:9.5.0doticon
License: Others (Non-SPDX)

Python Imaging Library (Fork)

Support
    Quality
      Security
        License
          Reuse

            Pillowby python-pillow

            Python doticon star image 10903 doticonVersion:9.5.0doticon License: Others (Non-SPDX)

            Python Imaging Library (Fork)
            Support
              Quality
                Security
                  License
                    Reuse

                      numpyby numpy

                      Python doticonstar image 23755 doticonVersion:v1.25.0rc1doticon
                      License: Permissive (BSD-3-Clause)

                      The fundamental package for scientific computing with Python.

                      Support
                        Quality
                          Security
                            License
                              Reuse

                                numpyby numpy

                                Python doticon star image 23755 doticonVersion:v1.25.0rc1doticon License: Permissive (BSD-3-Clause)

                                The fundamental package for scientific computing with Python.
                                Support
                                  Quality
                                    Security
                                      License
                                        Reuse

                                          opencvby opencv

                                          C++ doticonstar image 69456 doticonVersion:4.7.0doticon
                                          License: Permissive (Apache-2.0)

                                          Open Source Computer Vision Library

                                          Support
                                            Quality
                                              Security
                                                License
                                                  Reuse

                                                    opencvby opencv

                                                    C++ doticon star image 69456 doticonVersion:4.7.0doticon License: Permissive (Apache-2.0)

                                                    Open Source Computer Vision Library
                                                    Support
                                                      Quality
                                                        Security
                                                          License
                                                            Reuse

                                                              If you do not have OpenCV or numpy that is required to run this code, you can install it by clicking on the above link and copying the pip Install command from the OpenCV page in kandi.


                                                              You can search for any dependent library on kandi like OpenCV.

                                                              Environment Tested


                                                              I tested this solution in the following versions. Be mindful of changes when working with other versions.

                                                              1. The solution is created in Python 3.9.6
                                                              2. The solution is tested on OpenCV-Python 4.7.0.72 version.


                                                              Using this solution, we are able to Convert OpenCV images in PIL image format using the OpenCV and numpy libraries in Python with simple steps. This process also facilities an easy to use, hassle free method to create a hands-on working version of code.

                                                              Support


                                                              1. For any support on kandi solution kits, please use the chat
                                                              2. For further learning resources, visit the Open Weaver Community learning page.

                                                              See similar Kits and Libraries