def Encode(self, quality, speed): file_path = GetFilePath('source1_64x48.png') original_image = wp2.ArgbBuffer() wp2.ReadImage(file_path, original_image) config = wp2.EncoderConfig() config.quality = quality config.speed = speed self.assertTrue(config.IsValid()) memory_writer = wp2.MemoryWriter() wp2.Encode(original_image, memory_writer, config) return memory_writer.GetBytes()
def _display_bytes(self, has_alpha): """Updates the window with the image stored in 'self._buffer_argb'.""" if not self._buffer_argb.IsEmpty(): # Note: converting the whole buffer to WP2_RGBA_32 at every chunk is not # efficient. buffer_rgba = wp2.ArgbBuffer(wp2.WP2_RGBA_32) buffer_rgba.ConvertFrom(self._buffer_argb) bytes_rgba = buffer_rgba.GetBytes() img_size = (buffer_rgba.width, buffer_rgba.height) self._img = Image.frombytes('RGBA', img_size, bytes_rgba) if has_alpha: if self._checkerboard is None: self._create_checkerboard(img_size) # Tkinter does not support transparency so the checkerboard is pasted # under the image. self._img = Image.alpha_composite(self._checkerboard, self._img) self._img_tk = ImageTk.PhotoImage(self._img) self._img_label.configure(image=self._img_tk)
def main(): parser = argparse.ArgumentParser(description='Simple wp2 encoder.') parser.add_argument('-v', '--version', action='store_true', help='Print libwebp2 version.', dest='version') parser.add_argument('input', nargs='?', help='Path to the image to encode.') parser.add_argument( '-f', '--frames', nargs='+', help='Paths to the images to encode as an animation, interleaved with ' 'durations in milliseconds. Usage: frame1.png 12 frame2.png 16 ...', dest='frames') parser.add_argument('-o', '--output', help='Path to the encoded image.', dest='output') parser.add_argument('-q', '--quality', default='75.0', type=float, help='Quality: from 0 (lossy) to 100 (lossless).', dest='quality') parser.add_argument('-s', '--speed', default='6', type=int, help='Speed: from 0 (faster) to 9 (slower, better).', dest='speed') args = parser.parse_args() # ---------------------------------------------------------------------------- if args.version: def format_version(v, n): # Returns a string with n numbers: 'X.Y.Z' return '.'.join( [str((v >> (8 * (n - i - 1))) & 0xff) for i in range(n)]) print('libwebp2 version: ', format_version(wp2.WP2GetVersion(), 3)) print('libwebp2 ABI version: ', format_version(wp2.WP2GetABIVersion(), 2)) exit(0) # ---------------------------------------------------------------------------- if not wp2.WP2CheckVersion(): print('error: version mismatch') exit(1) # ---------------------------------------------------------------------------- config = wp2.EncoderConfig() config.quality = args.quality config.speed = args.speed if not config.IsValid(): print('error: invalid configuration') exit(1) memory_writer = wp2.MemoryWriter() # ---------------------------------------------------------------------------- if args.frames: if args.input: print( 'error: \'input\' argument must not be specified with --frames' ) exit(1) if (len(args.frames) % 2) != 0: print('error: --frames requires an even number of values') exit(1) num_frames = len(args.frames) // 2 animation_encoder = wp2.AnimationEncoder() for f in range(num_frames): file_name = args.frames[f * 2] duration_ms = int(args.frames[f * 2 + 1]) buffer_argb = wp2.ArgbBuffer() wp2.ReadImage(file_name, buffer_argb) animation_encoder.AddFrame(buffer_argb, duration_ms) animation_encoder.Encode(memory_writer, config) else: if not args.input: print('error: the following arguments are required: input') exit(1) original_image = wp2.ArgbBuffer() image_reader = wp2.ImageReader(args.input, original_image) [_, is_last, duration_ms] = image_reader.ReadFrame() if duration_ms == wp2.ImageReader.kInfiniteDuration: wp2.Encode(original_image, memory_writer, config) else: animation_encoder = wp2.AnimationEncoder() animation_encoder.AddFrame(original_image, duration_ms) while not is_last: [_, is_last, duration_ms] = image_reader.ReadFrame() animation_encoder.AddFrame(original_image, duration_ms) animation_encoder.Encode(memory_writer, config) # ---------------------------------------------------------------------------- if args.output: wp2.IoUtilWriteFile(memory_writer.mem_, memory_writer.size_, args.output, True)
def main(): parser = argparse.ArgumentParser(description='Simple wp2 decoder.') parser.add_argument('-v', '--version', action='store_true', help='Print libwebp2 version.', dest='version') parser.add_argument('input', nargs='?', help='Path to the image to decode.') parser.add_argument('-o', '--output', help='Path to the decoded image.', dest='output') parser.add_argument( '-f', '--frames_folder', type=str, help='Path to the folder where frames will be saved. ' 'Outputs: frame0_[duration]ms.png frame1_[duration]ms.png ...', dest='frames_folder') args = parser.parse_args() # ---------------------------------------------------------------------------- if args.version: def format_version(v, n): # Returns a string with n numbers: 'X.Y.Z' return '.'.join( [str((v >> (8 * (n - i - 1))) & 0xff) for i in range(n)]) print('libwebp2 version: ' + format_version(wp2.WP2GetVersion(), 3)) print('libwebp2 ABI version: ' + format_version(wp2.WP2GetABIVersion(), 2)) exit(0) # ---------------------------------------------------------------------------- if not args.input: print('error: the following arguments are required: input') exit(1) buffer_argb = wp2.ArgbBuffer() wp2.ReadImage(args.input, buffer_argb) # ---------------------------------------------------------------------------- if args.output: wp2.SaveImage(buffer_argb, args.output, True) elif not args.frames_folder: print('Decoding success but no output file specified.') # ---------------------------------------------------------------------------- if args.frames_folder: with open(args.input, 'rb') as input_file: encoded_bytes = input_file.read() decoder = wp2.ArrayDecoder() decoder.SetInput(encoded_bytes) frame_index = 0 while decoder.ReadFrame(): frame_file_name = 'frame{}_{}ms.png'.format( frame_index, decoder.GetFrameDurationMs()) frame_file_path = os.path.join(args.frames_folder, frame_file_name) wp2.SaveImage(decoder.GetPixels(), frame_file_path, True) frame_index += 1 decoder.GetStatus() # Exception-triggering check
def testException(self): image = wp2.ArgbBuffer() self.assertRaises(RuntimeError, wp2.ReadImage, 'missing/file', image) self.assertRaises(RuntimeError, wp2.Decode, None, None)
def Decode(self, encoded_bytes): decoded_argb = wp2.ArgbBuffer() wp2.Decode(encoded_bytes, decoded_argb)