(target_size, tar_handle, img_object, output_filename)
Takes a tar file handle and a TarInfo object inside that tarfile and
    optionally transforms it and then writes it out to output_filename
Takes a tar file handle and a TarInfo object inside that tarfile and
    optionally transforms it and then writes it out to output_filename
Takes a tar file handle and a TarInfo object inside that tarfile and optionally transforms it and then writes it out to output_filename
def transform_and_save(target_size, tar_handle, img_object, output_filename):
    """
    Takes a tar file handle and a TarInfo object inside that tarfile and
    optionally transforms it and then writes it out to output_filename
    """
    img_handle = tar_handle.extractfile(img_object)
    img = Image.open(img_handle)
    width, height = img.size

    # Take the smaller image dimension down to target_size
    # while retaining aspect_ration. Otherwise leave it alone
    if width < height:
        if width > target_size:
            scale_factor = float(target_size) / width
            width = target_size
            height = int(height*scale_factor)
    else:
        if height > target_size:
            scale_factor = float(target_size) / height
            height = target_size
            width = int(width*scale_factor)
    if img.size[0] != width or img.size[1] != height:
        img = img.resize((width, height), resample=Image.LANCZOS)
        img.save(output_filename, quality=95)
    else:
        # Avoid recompression by saving file out directly without transformation
        dname, fname = os.path.split(output_filename)
        tar_handle.extract(img_object, path=dname)
        if fname != img_object.name:
            # Rename if name inside of tar is different than what we want it
            # called on the outside
            shutil.move(os.path.join(dname, img_object.name), output_filename)
    assert(os.stat(output_filename).st_size > 0), "{} has size 0".format(output_filename)
def transform_and_save ( target_size , tar_handle , img_object , output_filename ) : img_handle = tar_handle . extractfile ( img_object ) img = Image . open ( img_handle ) width , height = img . size # Take the smaller image dimension down to target_size # while retaining aspect_ration. Otherwise leave it alone if width < height : if width > target_size : scale_factor = float ( target_size ) / width width = target_size height = int ( height * scale_factor ) else : if height > target_size : scale_factor = float ( target_size ) / height height = target_size width = int ( width * scale_factor ) if img . size [ 0 ] != width or img . size [ 1 ] != height : img = img . resize ( ( width , height ) , resample = Image . LANCZOS ) img . save ( output_filename , quality = 95 ) else : # Avoid recompression by saving file out directly without transformation dname , fname = os . path . split ( output_filename ) tar_handle . extract ( img_object , path = dname ) if fname != img_object . name : # Rename if name inside of tar is different than what we want it # called on the outside shutil . move ( os . path . join ( dname , img_object . name ) , output_filename ) assert ( os . stat ( output_filename ) . st_size > 0 ) , "{} has size 0" . format ( output_filename )