0
import os
import shutil as sh
import cv2
import sys
import PIL
from PIL import Image

img = PIL.Image.open("target.webp")

width, height = img.size
print(width, height)

# define a function for
# compressing an image
def compressMe(file, verbose=False):
    # Get the path of the file
    filepath = os.path.join(os.getcwd(),
                            file)

    # open the image
    picture = Image.open(filepath)

    # Save the picture with desired quality
    # To change the quality of image,
    # set the quality variable at
    # your desired level, The more
    # the value of quality variable
    # and lesser the compression
    picture.save("Compressed_" + file,
                 "JPEG",
                 optimize=True,
                 quality=10)
    return


# Define a main function
def main():
    verbose = False

    # checks for verbose flag
    if (len(sys.argv) > 1):

        if (sys.argv[1].lower() == "-v"):
            verbose = True

    # finds current working dir
    cwd = os.getcwd()

    formats = ('.jpg', '.jpeg')

    # looping through all the files
    # in a current directory
    for file in os.listdir(cwd):

        # If the file format is JPG or JPEG
        if os.path.splitext(file)[1].lower() in formats:
            print('compressing', file)
            compressMe(file, verbose)

    print("Done")

if __name__ == "__main__":
    main()

compressMe('target.webp')
img2 = PIL.Image.open("Compressed_target.webp")
PIL.Image._show(img2)

width, height = img2.size
print(width, height)

I was trying to make the resolution of a .webp lower, basically file compression. After I run the code, the output is obviously way more blurry, and the file size has decreased from 42.8kB all the way to 8kB. But after checking the file properties, both the initial file and the output file have a resolution of 500x450. Additionally, I have also tried to use the image.size function and it still says 500x450, I would like to ask if there is a way to lower the resolution other than the file size.

GR_ Gamer
  • 7
  • 1
  • 1

1 Answers1

0

I was trying to make the resolution of a .webp lower, basically file compression.

That is not what "compression" means, at least in this context. .webp uses lossy compression, which is to say that the compressed output does not exactly match the uncompressed input. But the size in pixels is not part of the compression. You have exactly the same number of pixels.

To reduce the number of pixels, you resize the image. But note that this can have a larger impact on image quality than compression, at least with modern image compression techniques.

MSalters
  • 173,980
  • 10
  • 155
  • 350