To compress a JPEG image in Python, you can use the PIL
library. Here's an example code snippet that compresses a JPEG image file:
main.py294 chars10 lines
To use this function, you simply need to call it with the input and output file paths, as well as the desired quality level (between 1 and 95, where 95 is the highest quality):
main.py115 chars6 lines
This will compress the example.jpg
file with a quality level of 50 and save the output to compressed.jpg
. You can adjust the quality parameter to achieve the desired compression level.
gistlibby LogSnag