Save Image | OpenCV vs Pillow | Python
This part will show you the side-by-side comparisons of using OpenCV (cv2) and Pillow (PIL) to write/save images.
Save an Image
# Pillow
pil_img.save("new_image.jpg")
# OpenCV
cv2.imwrite("new_image.jpg", cv2_img)
Full Example
You might also want to learn how to read images using cv2 and PIL.
Pillow
from PIL import Image
# read images
pil_img = Image.open("test_images/test1.jpg")
# write images
pil_img.save("test_images/pil_image.jpg")
OpenCV
import cv2
# read images
cv2_img = cv2.imread("test_images/test1.jpg")
# write images
cv2.imwrite("test_images/cv2_image.jpg", cv2_img)
Syntax
Pillow
Image.save(fp, format=None, **params)
Parameters:
fp
: A filename (string),pathlib.Path
object or file object.format
: Optional format override. If omitted, the format to use is determined from the filename extension. This parameter should always be used if a file object is used instead of a filename.params
: Extra parameters to the image writer. See the documentation for available options for each writer.
Returns:
None
OpenCV
cv2.imwrite(filename, img[, params])
Parameters:
filename
: Name of the file.img
: Image or Images to be saved (Numpy array).params
: Format-specific parameters encoded as pairs (paramId_1, paramValue_1, paramId_2, paramValue_2, … .) see cv::ImwriteFlags.
Returns:
True
if succeeded, OtherwiseFalse
.