Automation of preparing images for the web rendering

date : 02/12/2020

For handling the image preparation for the website I used the Imagemagick with python to automate this task. I am still developing this tool further to advance it. I need to add a function for checking the size of the files.

import os



def readMas(dir):
    """
    read all the files in the image directory of the web site and return their address
    """
    n=0
    fs = []
    for (dirpath, dirnames, filenames) in os.walk(dir):
        if len(filenames)>0:
            for f in filenames:
                pth = os.path.join(dirpath,f)
                fs.append(pth)
            pass
    return fs

def imgConv(paths, remove= True):
    """
    Call magicimage from cmd and convert the png files to jpg. It will remove the old one
    """
    cmd_main = "magick convert -sampling-factor 4:2:0 -strip -quality 65 -interlace JPEG -colorspace sRGB"
    for path in paths:
        print(path)
        split=path.split(".")
        if split[-1].lower() == "png":
            cmd = cmd_main+" "+path+" "+split[0]+".jpg"
            os.system(cmd)
            if remove:
                os.remove(path)
                pass
        elif split[-1].lower() == "jpg":
            print("nice!")
            #if check the file size > thershholder:
                #cmd = cmd_main+" "+path+" "+split[0]+".jpg"
                #os.system(cmd)
    return None

# check the size funcion
# check the time of creation and add the counter to the file name
def addCapNumber(paths):
    n = 0
    times=[]
    for path in paths:
        time = os.path.gettime(path)
        pass

def main():
    img_path = os.path.normpath(os.path.join(os.getcwd(), "docs\\images\\week3"))
    f = readMas(img_path)
    imgConv(f)
    return None

if __name__ == '__main__':
    main()