Skip to content

Instantly share code, notes, and snippets.

@ashishnitinpatil
Last active February 15, 2023 16:52
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ashishnitinpatil/2596ad9495250d0abb2bfabba56a93f4 to your computer and use it in GitHub Desktop.
Save ashishnitinpatil/2596ad9495250d0abb2bfabba56a93f4 to your computer and use it in GitHub Desktop.
Bulk / batch compress multiple image files via compressor.io
#!/usr/bin/python3
# Requires python >= 3.6 (f-strings)
# Tested on Selenium 3.141 with Firefox 66 on Ubuntu 19.04
# Obligatory thank-you to compressor.io for such a lovely service!
# Kindly don't abuse the script and keep decent space between subsequent requests to the service! (Scraping 101)
import os
import glob
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.common.exceptions import WebDriverException
from selenium.webdriver.support import expected_conditions as EC
# You (hopefully) only need to customize following 3 variables
IMAGE_DIR = '/home/ashish/images-to-compress'
DOWNLOAD_DIR = os.path.join(IMAGE_DIR, 'compressed')
LOSSLESS = False # toggle for lossless compression option
profile = webdriver.FirefoxProfile()
profile.set_preference('browser.download.folderList', 2) # custom location
profile.set_preference('browser.download.manager.showWhenStarting', False)
profile.set_preference('browser.download.dir', DOWNLOAD_DIR)
profile.set_preference('browser.helperApps.neverAsk.saveToDisk',
'application/octet-stream,image/png,image/jpeg,image/gif')
browser = webdriver.Firefox(profile)
browser.get("https://compressor.io/compress")
def wait_for_element_by_id(browser, element_id, seconds=10, only_presence=False):
ec_func = 'element_to_be_clickable'
if only_presence:
ec_func = 'presence_of_element_located'
try:
WebDriverWait(browser, seconds).until(
getattr(EC, ec_func)((By.ID, element_id))
)
return True
except WebDriverException as e:
print(f'Waiting for {element_id} failed')
return False
def compress_image(browser, image_path, try_count=1):
if try_count >= 3:
return False
def retry():
return compress_image(browser, image_path, try_count+1)
if LOSSLESS:
if not wait_for_element_by_id(browser, 'fileupload', only_presence=True):
return retry()
_x = '//div[@class="btnCompression"]/button[@btn-radio="\'Lossless\'"]'
browser.find_element_by_xpath(_x).click()
if not wait_for_element_by_id(browser, 'file', only_presence=True):
return retry()
browser.find_element_by_id('file').send_keys(image_path)
if not wait_for_element_by_id(browser, 'toto'):
return retry()
browser.find_element_by_id('toto').click()
if not wait_for_element_by_id(browser, 'tryAgain'):
return retry()
browser.find_element_by_id('tryAgain').click()
return True
image_paths = []
for ext in ('jpeg', 'jpg', 'png'):
image_paths.extend(glob.glob(f"{IMAGE_DIR}/*.{ext}"))
total = len(image_paths)
for i, image_path in enumerate(image_paths):
if compress_image(browser, image_path):
print(f'{i+1:02d}/{total} Compressed image {image_path}')
else:
print(f'{i+1:02d}/{total} Failed to compress {image_path}')
time.sleep(1)
time.sleep(5) # Wait for final image to download correctly (just in case)
browser.quit()
# Remove "-compressor" from filenames
for filename in glob.glob(f"{DOWNLOAD_DIR}/*-compressor.*"):
os.rename(filename, filename.replace('-compressor', ''))
@ashishnitinpatil
Copy link
Author

Updated with lossless compression option & other minor improvements.

Also, if you get an error like I got (firefox 66, selenium 3.141)

selenium.common.exceptions.WebDriverException: Message: newSession

Then you need to download & extract latest geckodriver (single executable file) from https://github.com/mozilla/geckodriver/releases

wget -q https://github.com/mozilla/geckodriver/releases/download/v0.24.0/geckodriver-v0.24.0-linux64.tar.gz
tar xvzf geckodriver-v0.24.0-linux64.tar.gz
sudo mv geckodriver /usr/local/bin

@ashishnitinpatil
Copy link
Author

Using https://trimage.org/ is simpler & better for compression IMO. In-place lossless image compression with a GUI. Just drag & drop.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment