Reading images in Python

Arrays | NumPy | Python Methods and Functions

Python has very powerful tools when it comes to image processing. Let`s see how to process images using different libraries like OpenCV . PIL etc.

  1. Using OpenCV: OpenCV (Open Source Computer Vision) & # 8212 ; is a computer vision library that contains various functions for performing operations on images or videos. It was originally developed by Intel, but was later backed by Willow Garage and is now backed by Itseez. This library is cross-platform, meaning it is available in multiple programming languages ​​such as Python, C ++, etc.

    # Python image reader using OpenCV

    # importing an OpenCV module (cv2)

    import cv2

    # Save the image in the specified directory
    # Read the RGB image

    img = cv2.imread ( `g4g. png`

    # Output img with window name as "image"

    cv2.imshow ( `image` , img ) 

    # Maintain output window up to
    # user presses a key

    cv2.waitKey ( 0

    # Destroying existing windows on screen
    cv2.destroyAllWindows () 


  2. Using MatplotLib: Matplotlib — is an amazing Python visualization library for 2D array plots. Matplotlib — is a multi-platform data visualization library built on NumPy arrays and designed to work with the broader SciPy stack. It was introduced by John Hunter in 2002. Matplotlib comes with a wide variety of plots. Plots help you understand trends, patterns, and correlations. They are usually tools for reasoning about quantitative information.

    # Python reader
    # image with using matplotlib

    # importing matplotlib modules

    import matplotlib.image as mpimg

    import matplotlib.pyplot as plt

    # Read images

    img = mpimg.imread ( `g4g.png` )

    # Displaying images
      plt.imshow (img)


  3. Using PIL: PIL — is a Python image library that provides the python interpreter with image editing capabilities. It was developed by Fredrik Lund and several other contributors. Pillow — it is a handy PIL fork and easy to use library developed by Alex Clarke and others.

    # Python reader
    # image using the PIL module

    # PIL import

    from PIL import Image

    # Read image

    img = Image. open ( `g4g.png` )

    # Displaying images ()

    # prints the image format

    print (img . format )

    # print image mode

    print ( img.mode)


       PNG RGBA 

Get Solution for free from DataCamp guru