Hackerss.com

Hackerss.com is a community of amazing hackers

Hackerss is a community for developers, data scientitst, ethical hackers, hardware enthusiasts or any person that want to learn / share their knowledge of any aspect of digital technology.

Create account Log in
hackerss
hackerss

Posted on

Python Code to compress jpg files


import os
import sys
import subprocess

def compress_jpg(file_path, quality):
    """
    Compress jpg files
    """
    if not os.path.exists(file_path):
        print("File not found")
        return
    if not os.path.isfile(file_path):
        print("Not a file")
        return
    if not file_path.endswith(".jpg"):
        print("Not a jpg file")
        return
    if quality < 0 or quality > 100:
        print("Quality should be between 0 and 100")
        return
    subprocess.call(["convert", file_path, "-quality", str(quality), file_path])

def main():
    """
    Main function
    """
    compress_jpg(sys.argv[1], int(sys.argv[2]))

if __name__ == "__main__":
    main()
Enter fullscreen mode Exit fullscreen mode

Top comments (0)