Life2Coding
Save OpenCV Images in JPEG with Quality and PNG with Compression

This post will be helpful in learning OpenCV using Python programming. Here I will show how to implement OpenCV functions and apply it in various aspects using some examples. Then the output will be shown with some comparisons as well.

Requirements:

First, you need to setup your Python Environment with OpenCV. You can easily do it by following Life2Coding’s tutorial on YouTube: Linking OpenCV 3 with Python 3

Goals:

In this tutorial, I will show you how to save OpenCV images in different image formats. We will also see how to use image quality for JPEG images and compression ratio for PNG images.

Documentation:

Python: cv2.imwrite(filename, img[, params]) → retval

Saves an image to a specified file.

Parameters: 

  • filename – Name of the file.
  • image – Image to be saved.
  • params
    • Format-specific save parameters encoded as pairs paramId_1, paramValue_1, paramId_2, paramValue_2, …. The following parameters are currently supported:
      • For JPEG, it can be a quality (CV_IMWRITE_JPEG_QUALITY) from 0 to 100 (the higher is the better). Default value is 95.
      • For PNG, it can be the compression level (CV_IMWRITE_PNG_COMPRESSION) from 0 to 9. A higher value means a smaller size and longer compression time. Default value is 3.
      • For PPM, PGM, or PBM, it can be a binary format flag (CV_IMWRITE_PXM_BINARY), 0 or 1. Default value is 1.

 Steps:

  • Load Image using cv2.imread()
  • Display Image using cv2.imshow()
  • We will define the image extension and also quality and compression ratio
  • Save the output in an image file using cv2.imwrite()
  • Wait for keyboard button press using cv2.waitKey()
  • Exit window and destroy all windows using cv2.destroyAllWindows()

Example Code:

import cv2
import numpy as np

def save(path, image, jpg_quality=None, png_compression=None):
  '''
  persist :image: object to disk. if path is given, load() first.
  jpg_quality: for jpeg only. 0 - 100 (higher means better). Default is 95.
  png_compression: For png only. 0 - 9 (higher means a smaller size and longer compression time).
                  Default is 3.
  '''

  if jpg_quality:
    cv2.imwrite(path, image, [int(cv2.IMWRITE_JPEG_QUALITY), jpg_quality])
  elif png_compression:
    cv2.imwrite(path, image, [int(cv2.IMWRITE_PNG_COMPRESSION), png_compression])
  else:
    cv2.imwrite(path, image)

def main():
    
    imgpath = "Hanif.jpg"
    img = cv2.imread(imgpath)

    #display the image
    cv2.imshow('Hanif_Life2Coding', img)

    # save the image in JPEG format with 85% quality
    outpath_jpeg = "Hanif_Save_JPEG.jpg"

    save(outpath_jpeg,img,jpg_quality=85)

    outpath_png = "Hanif_Save_PNG.png"

    # save the image in PNG format with 4 Compression
    save(outpath_png, img,png_compression=4)

    cv2.waitKey(0)
    #destroy a certain window
    cv2.destroyWindow('Hanif_Life2Coding')

if __name__ == "__main__":
    main()

Output:

Capture-e1517744118746 Save OpenCV Images in JPEG with Quality and PNG with Compression

life2coding_icon [] Save OpenCV Images in JPEG with Quality and PNG with Compression

One thought on “Save OpenCV Images in JPEG with Quality and PNG with Compression

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.