最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to create a lossless WebP image using toDataURL? - Stack Overflow

programmeradmin3浏览0评论

I'm trying out the WebP image format in Chrome using the canvas element. On MDN I saw that toDataURL accepts a second argument representing the quality of the resulting image.

I'd like to generate a lossless WebP image of a canvas element. However, if I pass 1 for quality (meaning 100%), I don't get the exact same pixels back. It looks like it is generating a lossy image.

Here's a test case: /. When drawing the PNG image, a certain color of a certain pixel has a value of 40. After drawing a 100% quality WebP image of the canvas on itself, the same value has changed to 37. This does not seem lossless to me, even though I passed a quality of 100%.

Is it possible to have toDataURL create a lossless WebP image, and if so how?

I'm trying out the WebP image format in Chrome using the canvas element. On MDN I saw that toDataURL accepts a second argument representing the quality of the resulting image.

I'd like to generate a lossless WebP image of a canvas element. However, if I pass 1 for quality (meaning 100%), I don't get the exact same pixels back. It looks like it is generating a lossy image.

Here's a test case: http://jsfiddle/Nf5ve/1/. When drawing the PNG image, a certain color of a certain pixel has a value of 40. After drawing a 100% quality WebP image of the canvas on itself, the same value has changed to 37. This does not seem lossless to me, even though I passed a quality of 100%.

Is it possible to have toDataURL create a lossless WebP image, and if so how?

Share Improve this question edited Jul 16, 2012 at 3:27 Simon Sarris 64k13 gold badges149 silver badges174 bronze badges asked Jul 13, 2012 at 20:10 pimvdbpimvdb 155k80 gold badges311 silver badges356 bronze badges 2
  • Just to double-check, is there any transparency in the original PNG? – Jacob Foshee Commented Jul 13, 2012 at 22:21
  • @Jacob Foshee: No, it's a random photograph from lorempixel.. – pimvdb Commented Jul 14, 2012 at 10:41
Add a ment  | 

2 Answers 2

Reset to default 6

There are no official MIME types for webp so take this with a grain of salt for now.

Using "image/webp" on chrome in toDataURL will always result in a lossy webp image right now, even at quality "1"

There is seemingly no way to get a lossless webP image at this time

I tested and confirmed image/webp-lossless works as intended in Chrome these days. Just use it instead of image/webp

The issue appears to have been resolved in 2016.

The image I used to test would have incorrect colors with lossy webp, and the colors are now correct.

To be certain it's truly a webp and not a png I used the following on Linux, where the filename is webptest

❯ file webptest
webptest: RIFF (little-endian) data, Web/P image

Edit: Hmmmm... but the returned value from toDataURL is image/png so now I haven't a clue what to think. But the size of the file is identical to one I created from Sharp which is certainly webp...

发布评论

评论列表(0)

  1. 暂无评论