PIL can be used to compress an image without losing quality by using the “save” function with the “quality” parameter set to a high value. First, import PIL and open the image file. Then, use the “resize” method to reduce the size of the image while maintaining its aspect ratio. Finally, save the compressed image using the “save” function with the “quality” parameter set to 95 or higher. This will ensure that the image is compressed without significant loss in quality. Here’s a code example:
from PIL import Image
img = Image.open('image.jpg')
resized_img = img.resize((500, 500))
resized_img.save('compressed_image.jpg', 'JPEG', quality=95)