Skip to content

Transcode images

Transcode an image from Workers AI before uploading to R2

Last updated View as MarkdownAgent setup
const stream = await env.AI.run("@cf/bytedance/stable-diffusion-xl-lightning", {
	prompt: YOUR_PROMPT_HERE,
});

// Convert to AVIF
const image = (
	await env.IMAGES.input(stream).output({ format: "image/avif" })
).response();

const fileName = "image.avif";

// Upload to R2
await env.R2.put(fileName, image.body);

Was this helpful?