Skip to main content
added 305 characters in body
Source Link

Here is my updated script. Notable changes include the use of NEAREST instead of ANTIALIAS, as well as the inclusion of an EXIF copy and paste. I think the major hang on the original script was the inefficiency of ANTIALIAS, as this script gives me around 95% compression in about 2 seconds per image.

from PIL import Image
from pathlib import Path
import os, sys
import glob

root_dir = "/.../"

basewidth = 5504 #sets base width of new images

for filename in glob.iglob(root_dir + '*.jpg', recursive=True): #creates for loop to refeence all .jpg files in root directory
    p = Path(filename) #converts filename into Path object
    img = p.relative_to(root_dir) #uses Path function to parse out Path into components, then uses all components following that equal to the root_dir path name (in this case, our jpeg file names)
    new_name = (root_dir + 'compressed/' + str(img)) #creates new path to save compressed files to in subfolder "compressed" (note: must create subfolder before running)
    print(new_name)
    
    #resize and reduce
    im = Image.open(filename) #sets filename as object
    wpercent = (basewidth/float(im.size[0])) #uses the base width to establish aspect ratio
    hsize = int((float(im.size[1])*float(wpercent))) #scales image height using aspect ratio
    im = im.resize((basewidth,hsize), Image.NEAREST) #sets new resolution using basewidth and hsize
    exif = im.info['exif'] #copy EXIF data
    im.save(new_name, 'JPEG', exif = exif, quality=40)
from PIL import Image
from pathlib import Path
import os, sys
import glob

root_dir = "/.../"

basewidth = 5504 #sets base width of new images

for filename in glob.iglob(root_dir + '*.jpg', recursive=True): #creates for loop to refeence all .jpg files in root directory
    p = Path(filename) #converts filename into Path object
    img = p.relative_to(root_dir) #uses Path function to parse out Path into components, then uses all components following that equal to the root_dir path name (in this case, our jpeg file names)
    new_name = (root_dir + 'compressed/' + str(img)) #creates new path to save compressed files to in subfolder "compressed" (note: must create subfolder before running)
    print(new_name)
    
    #resize and reduce
    im = Image.open(filename) #sets filename as object
    wpercent = (basewidth/float(im.size[0])) #uses the base width to establish aspect ratio
    hsize = int((float(im.size[1])*float(wpercent))) #scales image height using aspect ratio
    im = im.resize((basewidth,hsize), Image.NEAREST) #sets new resolution using basewidth and hsize
    exif = im.info['exif'] #copy EXIF data
    im.save(new_name, 'JPEG', exif = exif, quality=40)

Here is my updated script. Notable changes include the use of NEAREST instead of ANTIALIAS, as well as the inclusion of an EXIF copy and paste. I think the major hang on the original script was the inefficiency of ANTIALIAS, as this script gives me around 95% compression in about 2 seconds per image.

from PIL import Image
from pathlib import Path
import os, sys
import glob

root_dir = "/.../"

basewidth = 5504 #sets base width of new images

for filename in glob.iglob(root_dir + '*.jpg', recursive=True): #creates for loop to refeence all .jpg files in root directory
    p = Path(filename) #converts filename into Path object
    img = p.relative_to(root_dir) #uses Path function to parse out Path into components, then uses all components following that equal to the root_dir path name (in this case, our jpeg file names)
    new_name = (root_dir + 'compressed/' + str(img)) #creates new path to save compressed files to in subfolder "compressed" (note: must create subfolder before running)
    print(new_name)
    
    #resize and reduce
    im = Image.open(filename) #sets filename as object
    wpercent = (basewidth/float(im.size[0])) #uses the base width to establish aspect ratio
    hsize = int((float(im.size[1])*float(wpercent))) #scales image height using aspect ratio
    im = im.resize((basewidth,hsize), Image.NEAREST) #sets new resolution using basewidth and hsize
    exif = im.info['exif'] #copy EXIF data
    im.save(new_name, 'JPEG', exif = exif, quality=40)
Source Link

from PIL import Image
from pathlib import Path
import os, sys
import glob

root_dir = "/.../"

basewidth = 5504 #sets base width of new images

for filename in glob.iglob(root_dir + '*.jpg', recursive=True): #creates for loop to refeence all .jpg files in root directory
    p = Path(filename) #converts filename into Path object
    img = p.relative_to(root_dir) #uses Path function to parse out Path into components, then uses all components following that equal to the root_dir path name (in this case, our jpeg file names)
    new_name = (root_dir + 'compressed/' + str(img)) #creates new path to save compressed files to in subfolder "compressed" (note: must create subfolder before running)
    print(new_name)
    
    #resize and reduce
    im = Image.open(filename) #sets filename as object
    wpercent = (basewidth/float(im.size[0])) #uses the base width to establish aspect ratio
    hsize = int((float(im.size[1])*float(wpercent))) #scales image height using aspect ratio
    im = im.resize((basewidth,hsize), Image.NEAREST) #sets new resolution using basewidth and hsize
    exif = im.info['exif'] #copy EXIF data
    im.save(new_name, 'JPEG', exif = exif, quality=40)