Created
October 30, 2020 18:01
-
-
Save anmolj7/63f0bda5b29e022fdbecc8d2da9cc692 to your computer and use it in GitHub Desktop.
Python file to compress a batch of jpegs/pngs of a given folder. Didn't make this a repo because was too lazy, and still had some modifications to do before posting it as a proper repo.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
__author__ = "Anmol Jhamb, https://instagram.com/thetechgeek_" | |
from PIL import Image | |
import os | |
import sys | |
def breakline(): | |
print("*"*50) | |
def cls(): | |
if os.name == "possix": | |
os.system("clear") | |
else: | |
os.system("cls") | |
def main(): | |
breakline() | |
print("Created By Anmol Jhamb") | |
breakline() | |
folder_name = input("Enter the folder's name: ") #Asking for the file's name. | |
os.chdir(os.path.join(os.getcwd(), folder_name)) #Changing the current directory to the one with the folder. | |
files = os.listdir() | |
files = [file for file in files if os.path.isfile(file)] | |
for index, file in enumerate(files): | |
cls() | |
print(f"Currently on the file {index+1}/{len(files)}", end="\n") | |
image = Image.open(file) | |
image = image.convert("RGB") | |
if not os.path.exists("EDITED"): | |
os.makedirs("EDITED") | |
edited = os.path.join(os.getcwd(), "EDITED") | |
og_image = image | |
image.thumbnail((1000, 1000)) | |
image.save("temp.jpeg") | |
i = 1 | |
while os.stat("temp.jpeg").st_size/1024 >= 40: | |
image = og_image | |
if i == 8: | |
break | |
image.thumbnail((1000-i*100, 1000-i*100)) | |
image.save("temp.jpeg") | |
i+=1 | |
os.remove("temp.jpeg") | |
image.save(os.path.join(edited, file)) | |
cls() | |
print("All the files were compressed successfully.") | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment