How to convert PNG to JPG in Python using OpenCV

You can convert a png image to jpg in Python using OpenCV by following the given steps. I highly recommend you get the “Computer Vision: Models, Learning, and Inference Book” to learn Computer Vision.

How to convert PNG to JPG in Python using OpenCV
How to convert PNG to JPG in Python using OpenCV

Step 1

Import the OpenCV library. If OpenCV is not installed in your system, then install it using This Method.

import cv2

Step 2

Now read the image from the location. In my case “C:\\AiHints” is the location and “cat.png” is the name of the image. Change it according to your image location and name.

image = cv2.imread("C:\\AiHints\\cat.png")
#imread is use to read an image from a location

Step 3

This code will convert the above png image into jpg and will save it at the location “C:\\AiHints”.

cv2.imwrite("C:\\AiHints\\cat.jpg", image)

Step 4

waitKey() open the image for a specific time in milliseconds until you press any key. “destroyAllWindows()” will destroy all the windows that we created.

cv2.waitKey()
cv2.destroyAllWindows()

Leave a Comment

Your email address will not be published.