Tags

Python Script to Optimise Images and upload to FTP server

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()