PythonPillowImage. save Save as jpg Image Compression Problem

  • 2021-12-13 08:51:40
  • OfStack

Directory Pillow Image Format Conversion save () convert () + save () Pillow Image. save Save as jpg Image Compression

Pillow Image Format Conversion

The Pillow library supports a variety of image formats, and you can read images directly using the open () method, regardless of the type of images. Pillow can easily convert between picture formats.

There are two main ways to convert picture formats, which are introduced below:

save()

As the name implies, the save () method is used to save images. When no file format is specified, it will be stored in the default image format; If you specify a picture format, the picture is stored in the specified format. The syntax format of save () is as follows:

Image.save(fp, format=None)

The parameters are described as follows:

fp: The storage path of the picture, including the name of the picture and the string format; format: Optional parameter to specify the format of the picture.

Examples are as follows:


from PIL import Image
im = Image.open("C:/Users/Administrator/Desktop/c-net.png")
im.save('C:/Users/Administrator/Desktop/c.biancheng.net.bmp')

A picture in c. bianchneg. net. BMP format will appear on your computer desktop.

convert()+save()

Note that not all image formats can be converted by the save () method, such as saving images in PNG format to JPG format. If you use save () method directly, the following errors will occur:


from PIL import Image
im = Image.open("C:/Users/Administrator/Desktop/c-net.png")
im.save('C:/Users/Administrator/Desktop/c.biancheng.net.jpg')

The error message is as follows:

# System error, RGBA cannot be used as mode for JPEG pictures

OSError: cannot write mode RGBA as JPEG

The reason for the error is that the PNG and JPG image modes are not 1. Among them, PNG is a 4-channel RGBA mode, that is, red, green, blue and Alpha transparent color; The JPG is a 3-channel RGB mode. Therefore, in order to realize the conversion of picture format, it is necessary to convert PNG into 3-channel RGB mode.

The convert () method provided by the Image class enables image mode conversion. This function provides several parameters, such as mode, matrix, dither, etc. The most critical parameter is mode, and other parameters need not be concerned. The syntax format is as follows:


convert(mode,parms**)
mode: Refers to the image mode to be converted; params: Additional optional parameters.

The modified code is as follows:


from PIL import Image
im = Image.open("C:/Users/Administrator/Desktop/c-net.png")
# Return at this time 1 A new one image Object, convert picture mode 
image=im.convert('RGB')
# Call save() Save 
image.save('C:/Users/Administrator/Desktop/c.biancheng.net.jpg')

Through the above code, the image of PNG format is successfully converted to JPG format.

Pillow Image. save Save as jpg Image Compression

In the process of using Image. save () method in Pillow and using default parameters to save jpg pictures, it is found that the pictures are seriously compressed, which leads to the original large size becoming several 10K. This is because in the process of saving as jpg, the compression algorithm is used internally to compress the picture.

But sometimes it is necessary that the size of the picture should not change too much or too little. So you can add parameters when using this method:


imObj.save(img_name, quality=95)

quality Parameter: Saves image quality with values ranging from 1 (worst) to 95 (best). The default value is 75, and the value higher than 95 should be avoided as much as possible in use; 100 disables part of the JPEG compression algorithm and results in almost no gain in image quality for large files.

When this parameter is used, the picture size increases. If the size of the picture can't meet your needs, is there any other way to increase the size of the picture?

By consulting the data and trying, it is found that save method also has one parameter that can be used with quality, which can greatly increase the picture size:


imObj.save(new_name, quality=95, subsampling=0)

subsampling Parameter: Sub-sampling, the practice of encoding images by realizing that the resolution of chrominance information is lower than that of luminance information. (Reference: https://en.wikipedia.org/wiki/Chroma_subsampling)

The possible subsample values are 0, 1, and 2, corresponding to 4: 4: 4, 4: 2: 2, and 4: 1: 1 (or 4: 2: 0?) .

After practice, setting the value to 0 can meet the demand of increasing the picture size.

Note: The parameters of the above method are only for images saved in JPG/JPEG format.

Reference document: https://pillow.readthedocs.io/en/5. 1. x/handbook/image-file-formats. html # jpeg

Reference document: https://pillow.readthedocs.io/en/4. 0. x/PIL.html


Related articles: