# Defold-WebP (decode and encode external WebP image files)

**URL:** <https://forum.defold.com/t/defold-webp-decode-and-encode-external-webp-image-files/82515>\
**Category:** The Defoldmine\
**Created:** [March 11, 2026, 4:06pm UTC](https://forum.defold.com/t/defold-webp-decode-and-encode-external-webp-image-files/82515 "2026-03-11T16:06:58Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Halfstar](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/halfstar/32/26587_2.png) [@Halfstar](https://forum.defold.com/u/Halfstar)\
**Post date:** [March 11, 2026, 4:06pm UTC](https://forum.defold.com/t/defold-webp-decode-and-encode-external-webp-image-files/82515/1 "2026-03-11T16:06:58Z")

</div>

[![banner](https://forum-defold-uploads.b-cdn.net/original/3X/2/6/26a61a3a6b05e2718ba73cef175bc0edb19a65ea.jpeg)](https://github.com/HalfstarDev/defold-webp)

> **[GitHub - HalfstarDev/defold-webp: Defold native extension for WebP images](https://github.com/HalfstarDev/defold-webp)**
>
> Defold native extension for WebP images

I made an extension to encode and decode external WebP image files.

WebP can use both lossless and lossy images, and supports alpha channels, so you can use images with transparency like a PNG, but also compress them like a JPEG.

#### webp.decode(image [, options])

Decode a WebP image into an image buffer. Use optional Lua table with [WebPDecoderOptions](https://developers.google.com/speed/webp/docs/api#advanced_decoding_api) as keys. Returns buffer, width, and height.

```lua
local buf, w, h = webp.decode(sys.load_resource("/assets/image_1.webp"))
resource.set_texture(go.get("#sprite", "texture0"), {
    width = w,
    height = h,
    type = resource.TEXTURE_TYPE_2D,
    format = resource.TEXTURE_FORMAT_RGBA,
    num_mip_maps = 1
}, buf)

```

#### webp.encode(buffer, width, height [, options])

Encode a image buffer into a WebP image. Use optional Lua table with [WebPConfig](https://developers.google.com/speed/webp/docs/api#advanced_encoding_api) fields as keys. Returns string with WebP image.

```lua
local buf, w, h = webp.decode(image)
local webp_image = webp.encode(buf, w, h, {quality = 50})
local f = io.open("out.webp", "wb")
if f then
    f:write(webp_image)
    f:close()
end

```

#### webp.get\_info(image)

Get information about a WebP image. Returns table with following fields: width, height, has\_alpha, has\_animation, lossy.

```lua
local info = webp.get_info(image)
print(info.width, info.height)

```
