Beispiel #1
0
def _process_file(input_file, output_file, apikey):
    """Shrinks input_file to output_file.

    This function should be used only inside process_directory.
    It takes input_file, tries to shrink it and if shrink was successful
    save compressed image to output_file. Otherwise raise exception.

    @return compressed: PNGResponse
    """
    bytes_ = read_binary(input_file)
    compressed = shrink(bytes_, apikey)

    if compressed.success and compressed.bytes:
        write_binary(output_file, compressed.bytes)
    else:
        if compressed.errno in FATAL_ERRORS:
            raise StopProcessing(compressed)
        elif compressed.errno == TinyPNGError.InternalServerError:
            raise RetryProcessing(compressed)

    return compressed
Beispiel #2
0
 def _decode_word(self, stream):
     q = read_unary(stream)
     r = read_binary(stream, q)
     symbol = int(math.pow(2, q) + r - 1)
     return symbol if self._direct else self._codebook[symbol]
Beispiel #3
0
 def _read_codebook(self, stream):
     number_of_symbols = read_binary(stream, 16)
     symbol_size = read_binary(stream, 4)
     return {i: read_binary(stream, symbol_size) for i in range(number_of_symbols)}