In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "the method of saving Python Pillow Image.save as jpg image compression". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "the method of saving Python Pillow Image.save as jpg image compression".
Pillow picture format conversion
The Pillow library supports multiple image formats, and you can read pictures directly using the open () method, regardless of the type of picture. Pillow can easily convert between image formats.
There are two main ways to convert between image formats, which are described below:
Save ()
As the name implies, the save () method is used to save images, and when no file format is specified, it is stored in the default picture 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 image.
Examples are as follows:
From PIL import Imageim = Image.open ("C:/Users/Administrator/Desktop/c-net.png") im.save ('CJV _ peg _ UsersActuator _
There will be a picture in c.bianchneg.net.BMP format on your computer desktop.
Convert () + save ()
Note that not all image formats can be converted using the save () method. For example, if you save a picture in PNG format to JPG format, the following error will occur if you directly use the save () method:
From PIL import Imageim = Image.open ("C:/Users/Administrator/Desktop/c-net.png") im.save ('CJV _ peg _ UsersActuator _
The error message is as follows:
# system error. RGBA cannot be used as the mode of JPEG image
OSError: cannot write mode RGBA as JPEG
The error is caused by inconsistent PNG and JPG image modes. Where PNG is a four-channel RGBA mode, that is, red, green, blue, and Alpha transparent color, and JPG is a three-channel RGB mode. Therefore, in order to achieve the conversion of picture format, it is necessary to transform PNG into three-channel RGB mode.
The convert () method provided by the Image class can convert the image mode. This function provides multiple parameters, such as mode, matrix, dither, etc., of which the most critical parameter is mode, and the rest need not be concerned about. The syntax format is as follows:
Convert (mode,parms**)
Mode: refers to the image mode to be converted
Params: other optional parameters.
The modified code is as follows:
From PIL import Imageim = Image.open ("C:/Users/Administrator/Desktop/c-net.png") # at this point, a new image object is returned. Convert the picture mode image=im.convert ('RGB') # call save () to save the image.save (' Cpurt hand UsersUniverse administrator'. Biancheng.net.jpg')
Through the above code, the picture in PNG format is successfully converted to JPG format.
Pillow Image.save is saved as jpg picture compression
In the process of using the Image.save () method in Pillow to save the jpg image with the default parameters, it is found that the image is badly compressed, causing the original large size to become tens of K. This is because the internal compression algorithm is used to compress the picture in the process of saving as jpg.
But sometimes it is often necessary that the size of the picture should not change too much or too small. So you can add parameters when using this method:
ImObj.save (img_name, quality=95)
Quality parameter: saves the quality of the image, with values ranging from 1 (worst) to 95 (best). The default value is 75, and values higher than 95 should be avoided as much as possible; 100 disables part of the JPEG compression algorithm and results in almost no gain in large file image quality.
When this parameter is used, the size of the picture increases. If the size of the picture is not enough to 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 the save method also has a parameter that can be used with quality, which can greatly increase the image size:
ImObj.save (new_name, quality=95, subsampling=0)
Subsampling parameter: sub-sampling, the practice of encoding an image by realizing that the resolution of chromaticity information is lower than that of luminance information.
The possible sub-sample values are 0 minute 1 and 2, corresponding to 4-month-old, 4-man, 4-man, and 4-man, and 4:1:1 (or 4:2:0? ).
After practice, setting the value to 0 can meet the requirement of increasing the size of the image.
Note: the parameters of the above method are only for images saved in JPG/JPEG format.
At this point, I believe that everyone on the "Python Pillow Image.save saved as jpg image compression method" have a deeper understanding, might as well to the actual operation of it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.