Below is the python script to optimize PNG images files using OPTIPNG library and upload compressed images in FTP folder.
If you don’t have OPTIPNG library, download from this url:Â http://optipng.sourceforge.net/
import ftplib
import os
username = 'username1'
pwd = 'pwd1'
localdirpath = "/home/username/Documents/screenshots/"
files = os.listdir(localdirpath)
ftp = ftplib.FTP('ftp.host.com')
ftp.login(username, pwd)
ftp.cwd('images')
for file in files:
if '.png' in file:
filepath = os.path.join(localdirpath, file)
os.system('optipng -o7 %s' % filepath)
binary_file = open(filepath,'rb')
ftp.storbinary('STOR %s' % file, binary_file)
binary_file.close()
os.remove(filepath)
print('uploaded: %s' % file)
ftp.quit()