Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Python to achieve Image Compression

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Editor to share with you how to use Python to achieve picture compression, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

1. Introduction

Let's take a look at the original selfie.

The image size is 53KB.

Next, it's time to show off the real technology.

2. PIL module 2.1 quality mode

2.1.1 ordinary picture compression

We use the quality method of the PIL module to compress

Code display:

#-*-coding: utf-8-*-# @ auth: carl_DJ# @ time: 2022-01-12from PIL import Image# reads the img file img_file ='. / pico.jpg'im = Image.open (img_file) # quality is the set compression ratio im.save ('pico-ouo.jpg',quality = 20)

Running result

Well, the result is quite satisfactory.

That is, to meet the requirement that the little loser does not want to change the picture.

It also meets the requirement that the picture of the small loser is not distorted after being compressed.

2.1.2 very large image compression

Let's see if the oversized picture can be compressed.

Here we use the goddess photos of the little losers to compress.

Original drawing 120m

Let's see how much M there can be after compression.

Run the code

#-*-coding: utf-8-*-# @ auth: carl_DJ# @ time: 2022-01-12from PIL import Image,ImageFile# prevent pictures from exceeding 178956970 pixels while error ImageFile.LOAD_TRUNCATED_IMAGES = TrueImage.MAX_IMAGE_PIXELS = None# read img file img_file ='. / king-letter.jpg'im = Image.open (img_file) # quality is set compression ratio im.save ('king-letter-ouo.jpg',quality = 20)

As a result, let's look at the size of the image.

Perfect.

We're opening the picture.

Perfect two in a row.

Note

Because the picture we used is 120m, which is over 178956970 pixels.

Therefore, we need to use two methods of imageFile to limit errors due to oversize:

ImageFile.LOAD_TRUNCATED_IMAGES = TrueImage.MAX_IMAGE_PIXELS = None

If there is no limit, the error message is reported:

PIL.Image.DecompressionBombError: Image size (206621200 pixels) exceeds limit of 178956970 pixels, could be decompression bomb DOS attack.2.2 thumbnail mode

Mastered the quality mode

Then we use the thumbnail method of PIL to compress the image.

We also use oversized pictures.

This time I use the goddess photo of Tang San.

Code presentation

#-*-coding: utf-8-*-# @ auth: carl_DJ# @ time: 2022-01-12from PIL import Image,ImageFile# prevent the picture from exceeding 178956970 pixels and report an error ImageFile.LOAD_TRUNCATED_IMAGES = TrueImage.MAX_IMAGE_PIXELS = None# read the img file img_file ='. / king-letter.jpg'im = Image.open (img_file) # get the original size picture size w im.size# image and compress im.thumbnail = ((w / / 2) H / / 2)) # Save im.save ('king-letter-oua.jpg')

Running result

We can see that although the thumbnail method compresses the picture to 50%, the size of the picture is not much smaller.

3. OpenCV module

In addition to the PIL module, there is also an OpenCV module that can also compress pictures

3.1 installation

Opencv module needs to be installed

The old rule is to install pip directly:

Pip install opencv-python

Install by other means:

"Python3, choose Python to install third-party libraries automatically, and say goodbye to pip from now on! "

"Python3: I quietly import all the Python libraries with just one line of code! "

Execute code #-*-coding: utf-8-*-# @ auth: carl_DJ# @ time: 2022-01-12from cv2 import cv2imgs = cv2.imread ('. / king.jpg') # Zoom to the original 1/4resize_img = cv2.resize (imgs, (0,0), fx=0.25, fy=0.25, interpolation=cv2.INTER_NEAREST) # rewrite the picture and save cv2.imwrite ('king-ouv.jpg', resize_img)

Running result

Original drawing

Compressed picture

The above is all the contents of the article "how to use Python to achieve picture compression". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report