Exemple #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
Exemple #2
0
    def _make_codebook(self, stream):
        alphabet = self._hist.keys()
        ordered_alphabet = sorted(alphabet, key=self._hist.get, reverse=True)
        codebook = {symbol: code for code, symbol in enumerate(ordered_alphabet)}

        # Zapis książki kodów do strumienia
        symbol_size = int(math.ceil(math.log(max(alphabet) or 1, 2)))
        write_binary(stream, len(alphabet), num_bits=16)
        write_binary(stream, symbol_size, num_bits=4)
        for symbol in ordered_alphabet:
            write_binary(stream, symbol, num_bits=symbol_size)
        return codebook
Exemple #3
0
 def _encode_word(self, word, stream):
     symbol = word if self._direct else self._codebook[word]
     q = int(math.floor(math.log(symbol + 1, 2)))
     r = symbol + 1 - math.pow(2, q)
     write_unary(stream, q)
     write_binary(stream, r, num_bits=q)