Skip to content

Determine if WebP is lossy or lossless #6716

Answered by radarhere
avalonv asked this question in Q&A
Discussion options

You must be logged in to vote

A built-in way? No.

However, I saw

_VP8_MODES_BY_IDENTIFIER = {
b"VP8 ": "RGB",
b"VP8X": "RGBA",
b"VP8L": "RGBA", # lossless
}
def _accept(prefix):
is_riff_file_format = prefix[:4] == b"RIFF"
is_webp_file = prefix[8:12] == b"WEBP"
is_valid_vp8_mode = prefix[12:16] in _VP8_MODES_BY_IDENTIFIER
if is_riff_file_format and is_webp_file and is_valid_vp8_mode:

and think that the following should be able to tell you if the WebP file is lossless or not.

with open("Tests/images/hopper.webp", "rb") as fp:
    lossless = fp.read(15)[-1:] == b"L"

Replies: 1 comment 6 replies

Comment options

You must be logged in to vote
6 replies
@avalonv
Comment options

@radarhere
Comment options

@avalonv
Comment options

@radarhere
Comment options

@avalonv
Comment options

Answer selected by avalonv
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
2 participants