示例#1
0
def do_optimization(t: Task) -> TaskResult:
    """ Try to reduce file size of an image.
    Expects a Task object containing all the parameters for the image processing.
    The actual processing is done by the corresponding function,
    according to the detected image format.
    :param t: A Task object containing all the parameters for the image processing.
    :return: A TaskResult object containing information for single file report.
    """
    # TODO: Catch exceptions that may occur here.
    try:
        img = Image.open(t.src_path)

        # TODO: improve method of image format detection (what should happen if the
        #       file extension does not match the image content's format? Maybe we
        #       should skip unsupported formats?)
        if img.format.upper() == 'PNG':
            return optimize_png(t)
        elif img.format.upper() in ('JPEG', 'MPO'):
            return optimize_jpg(t)
        else:
            try:
                had_exif = True if piexif.load(t.src_path)['Exif'] else False
            except piexif.InvalidImageDataError:  # Not a supported format
                had_exif = False
            except ValueError:  # No exif info
                had_exif = False
            return TaskResult(img=t.src_path,
                              orig_format=img.format.upper(),
                              result_format=img.format.upper(),
                              orig_mode=img.mode,
                              result_mode=img.mode,
                              orig_colors=0,
                              final_colors=0,
                              orig_size=os.path.getsize(t.src_path),
                              final_size=0,
                              was_optimized=False,
                              was_downsized=False,
                              had_exif=had_exif,
                              has_exif=had_exif)

    except IOError:
        try:
            had_exif = True if piexif.load(t.src_path)['Exif'] else False
        except piexif.InvalidImageDataError:  # Not a supported format
            had_exif = False
        except ValueError:  # No exif info
            had_exif = False
        return TaskResult(img=t.src_path,
                          orig_format="",
                          result_format="",
                          orig_mode="",
                          result_mode="",
                          orig_colors=0,
                          final_colors=0,
                          orig_size=os.path.getsize(t.src_path),
                          final_size=0,
                          was_optimized=False,
                          was_downsized=False,
                          had_exif=had_exif,
                          has_exif=had_exif)
示例#2
0
def do_optimization(t: Task) -> TaskResult:
    """ Try to reduce file size of an image.

    Expects a Task object containing all the parameters for the image processing.

    The actual processing is done by the corresponding function,
    according to the detected image format.

    :param t: A Task object containing all the parameters for the image processing.
    :return: A TaskResult object containing information for single file report.
    """
    img = Image.open(t.src_path)

    if img.format.upper() == 'PNG':
        return optimize_png(t)
    # elif img.format.upper() == 'JPEG':
    else:
        return optimize_jpg(t)
示例#3
0
def do_optimization(t: Task) -> TaskResult:
    """ Try to reduce file size of an image.
    Expects a Task object containing all the parameters for the image processing.
    The actual processing is done by the corresponding function,
    according to the detected image format.
    :param t: A Task object containing all the parameters for the image processing.
    :return: A TaskResult object containing information for single file report.
    """
    # TODO: Catch exceptions that may occur here.
    img = Image.open(t.src_path)

    # TODO: improve method of image format detection (what should happen if the
    #       file extension does not match the image content's format? Maybe we
    #       should skip unsupported formats?)
    if img.format.upper() == 'PNG':
        return optimize_png(t)
    elif (img.format.upper() == 'JPEG'):
        return optimize_jpg(t)
    else:
        pass