Python equal-scale compression with quality manipulation of images
A Python PIL
1. The key points to obtain high quality in image processing using PIL Image are as follows:
Copy the code
-
1. Use ANTIALIAS for processing; 2, save is to set quality;Copy the code
2. Core code:
-
im.resize(box, Image.ANTIALIAS)im.save(path, 'JPEG', quality = 95) Copy the code
3. Examples:
Copy the code
-
From PIL import Imagefile = '1.jpg'img = image.open (file)w,h = img.sizew,h = round(w * 0.2),round(h * 0.2) Img = img.resize((w,h), image.antialias)img.save('1.jpg', optimize=True, quality=85Copy the code
2. Python OpenCV
1, use OpencV to save the image
Cv2.imwrite (Storage path, image variable, [save disk identifier])Copy the code
2. Description of saving mark:
Cv2. CV_IMWRITE_JPEG_QUALITY Sets the quality of the image in. Jpeg or. JPG format to 0-- 100 (the higher the quality, the higher the quality). CV_IMWRITE_WEBP_QUALITY cv2.CV_IMWRITE_PNG_COMPRESSION cv2.CV_IMWRITE_PNG_COMPRESSION cv2.CV_IMWRITE_PNG_COMPRESSION cv2.CV_IMWRITE_PNG_COMPRESSION cv2.CV_IMWRITE_PNG_COMPRESSION The value ranges from 0 to 9 (the greater the value, the greater the compression ratio), and the default is 3Copy the code
3. Example of saving disk identifier:
Copy the code
Cv2. imwrite('img.jpg',img,[int(cv2.imwrite_jpeg_qualty),70]) save the img variable to img.png with image quality 70Copy the code
4. Different types of pictures:
PNG suffix must be. PNG, image quality 0-9, default is 3,0 is the best quality, 9 is the worst.
cv2.imwrite("123.png", img, [int(cv2.IMWRITE_PNG_COMPRESSION), 0])
Copy the code
Save JPG image, image suffix must be.jpg, image quality 0-100, default is 95,100 best, 0 worst.
cv2.imwrite("123.jpg", img, [int(cv2.IMWRITE_JPEG_QUALITY), 100])
Copy the code
5. Examples:
-
Import cv2cv2.namedWindow("Image") # create window img = cv2.imread('ver.jpg')cv2.imshow("Image",img)cv2.imwrite('test.jpg',img,[int(cv2.IMWRITE_JPEG_QUALITY),70])cv2.waitKey(0) cv2.destroyWindow("Image")Copy the code