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:

import ftplib
import os

username = 'username1'
pwd = 'pwd1'
localdirpath = "/home/username/Documents/screenshots/"

files = os.listdir(localdirpath)

ftp = ftplib.FTP('')
ftp.login(username, pwd)

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)
        print('uploaded: %s' % file)

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.