DEV Community

CurlShot
CurlShot

Posted on

PNG or JPEG for a Playwright social-card screenshot: size, error and time measured on three backgrounds

I wanted to know what to put in page.screenshot() when the image is a 1200x630 social card: PNG or JPEG, which quality, and whether deviceScaleFactor: 2 is worth it. I rendered the same card three ways and measured file size, error against the PNG, and time.

The short version: the right format depends on the background. A solid background favours PNG. A gradient or photo makes PNG 6 to 9 times bigger than JPEG at quality 85 in my runs. I did not find a case where the default PNG is the best choice for a photo card.

What I ran

One script, Playwright 1.63.0 with Chromium 153.0.8010.12 on Node v24.22.3, Linux. It renders a 1200x630 card with the same headline, subtitle and button over three backgrounds:

  • solid: one flat colour.
  • gradient: a CSS linear gradient.
  • photo: a deterministic noise image drawn on a canvas, standing in for a photo.

For each page and each deviceScaleFactor (1 and 2) it takes a PNG, then JPEGs at quality 95, 85, 75 and 60. It decodes each JPEG and compares it to the PNG with pixelmatch (threshold 0.1) and the largest single-channel difference. Time is the median of 9 screenshots of the same page.

Install: npm i [email protected] [email protected] [email protected] [email protected].

const { chromium } = require('playwright');
const { PNG } = require('pngjs');
const jpeg = require('jpeg-js');

const CSS = `html,body{margin:0}body{width:1200px;height:630px;position:relative;overflow:hidden;
font-family:"DejaVu Sans",sans-serif;color:#fff}
h1{position:absolute;left:80px;top:150px;margin:0;font-size:68px;width:900px;line-height:1.15}
p{position:absolute;left:80px;top:430px;margin:0;font-size:30px;color:#cbd5e1}
.b{position:absolute;right:80px;bottom:60px;background:#22c55e;color:#052e16;
padding:14px 28px;border-radius:10px;font-size:28px;font-weight:700}`;
const TEXT = `<h1>Measuring screenshot formats for link previews</h1>
<p>PNG, JPEG quality and device scale factor</p><div class=b>Read the notes</div>`;
const PAGES = {
  solid: `<style>${CSS}body{background:#0f172a}</style>${TEXT}`,
  gradient: `<style>${CSS}body{background:linear-gradient(135deg,#0f172a,#1e3a8a)}</style>${TEXT}`,
  photo: `<style>${CSS}canvas{position:absolute;inset:0;width:1200px;height:630px}</style>
<canvas id=c width=1200 height=630></canvas>${TEXT}
<script>
let s=12345;const r=()=>(s=(s*1664525+1013904223)>>>0)/4294967296;
const g=[0,1,2,3].map(()=>Array.from({length:64*64*3},r));
const x=c.getContext('2d'),im=x.createImageData(1200,630),L=(a,b,t)=>a+(b-a)*t;
function vn(o,u,v,ch){const sc=[8,20,50,120][o],fx=u/sc,fy=v/sc,ix=Math.floor(fx),iy=Math.floor(fy),
tx=fx-ix,ty=fy-iy,q=(a,b)=>g[o][((b%64)*64+(a%64))*3+ch];
return L(L(q(ix,iy),q(ix+1,iy),tx),L(q(ix,iy+1),q(ix+1,iy+1),tx),ty)}
for(let y=0;y<630;y++)for(let u=0;u<1200;u++)for(let ch=0;ch<3;ch++){let v=0;
for(let o=0;o<4;o++)v+=vn(o,u,y,ch)*[.4,.3,.2,.1][o];im.data[(y*1200+u)*4+ch]=v*255;im.data[(y*1200+u)*4+3]=255}
x.putImageData(im,0,0);
</script>`,
};

(async () => {
  const pm = (await import('pixelmatch')).default;
  const browser = await chromium.launch();
  console.log(`chromium ${browser.version()}`);
  console.log('page | scale | format | KiB | px changed vs PNG | max channel error | median ms of 9');
  for (const [name, html] of Object.entries(PAGES)) {
    for (const scale of [1, 2]) {
      const ctx = await browser.newContext({ viewport: { width: 1200, height: 630 }, deviceScaleFactor: scale });
      const page = await ctx.newPage();
      await page.setContent(html);
      await page.evaluate(() => document.fonts.ready);
      const ref = PNG.sync.read(await page.screenshot({ type: 'png' }));
      const timeIt = async (opts) => {
        const t = [];
        let buf;
        for (let i = 0; i < 9; i++) { const a = performance.now(); buf = await page.screenshot(opts); t.push(performance.now() - a); }
        return { buf, ms: t.sort((p, q) => p - q)[4] };
      };
      const out = (label, buf, diff, max, ms) =>
        console.log(`${name} | ${scale}x | ${label} | ${(buf.length / 1024).toFixed(0)} | ${diff} | ${max} | ${ms === undefined ? '' : ms.toFixed(0)}`);
      const png = await timeIt({ type: 'png' });
      out('png', png.buf, 0, 0, png.ms);
      for (const quality of [95, 85, 75, 60]) {
        const j = await timeIt({ type: 'jpeg', quality });
        const d = jpeg.decode(j.buf, { useTArray: true });
        let max = 0;
        for (let i = 0; i < d.data.length; i++) if (i % 4 !== 3) max = Math.max(max, Math.abs(d.data[i] - ref.data[i]));
        const diff = pm(ref.data, d.data, null, ref.width, ref.height, { threshold: 0.1 });
        out('jpeg q' + quality, j.buf, diff, max, j.ms);
      }
      await ctx.close();
    }
  }
  const ctx = await browser.newContext();
  const page = await ctx.newPage();
  await page.setContent('<p>x</p>');
  try { await page.screenshot({ type: 'png', quality: 50 }); } catch (e) { console.log('png + quality:', e.message.split('\n')[0]); }
  await browser.close();
})();
Enter fullscreen mode Exit fullscreen mode

Sizes and error

The pages are rendered at 1200x630, so 1x is 756,000 pixels. I ran the script twice. Sizes and pixel counts were identical in both runs. Times were not (see below).

page scale format KiB px changed vs PNG max channel error
solid 1x png 44 0 0
solid 1x jpeg q95 72 140 60
solid 1x jpeg q85 50 845 82
solid 1x jpeg q75 41 1421 88
solid 1x jpeg q60 34 2674 98
solid 2x png 91 0 0
solid 2x jpeg q95 151 75 64
solid 2x jpeg q85 109 158 65
solid 2x jpeg q75 91 468 67
solid 2x jpeg q60 77 1676 71
gradient 1x png 333 0 0
gradient 1x jpeg q95 84 205 74
gradient 1x jpeg q85 52 694 83
gradient 1x jpeg q75 42 1285 85
gradient 1x jpeg q60 35 2649 94
gradient 2x png 1039 0 0
gradient 2x jpeg q95 186 74 79
gradient 2x jpeg q85 115 151 78
gradient 2x jpeg q75 95 500 79
gradient 2x jpeg q60 80 1997 77
photo 1x png 927 0 0
photo 1x jpeg q95 198 556 73
photo 1x jpeg q85 125 651 83
photo 1x jpeg q75 98 772 87
photo 1x jpeg q60 78 1244 108
photo 2x png 2522 0 0
photo 2x jpeg q95 535 93 71
photo 2x jpeg q85 311 195 86
photo 2x jpeg q75 241 388 87
photo 2x jpeg q60 192 965 105

What the table shows:

  • Solid background: PNG is 44 KiB at 1x, smaller than JPEG q95 (72) and q85 (50), and exact. At 2x it is 91 KiB against 109 for q85 and 91 for q75. JPEG buys almost nothing here and adds error.
  • Gradient: PNG is 333 KiB at 1x and 1039 KiB at 2x. JPEG q85 is 52 and 115. The gradient is the whole difference, since the text and button are the same as in the solid card.
  • Photo-like: PNG is 927 KiB at 1x and 2522 KiB at 2x. JPEG q85 is 125 and 311.
  • 2x multiplies size by about 2 to 3 for every format. My noise image is drawn at 1200x630 and stretched at 2x, so a real 2x photo will compress worse than my 2x photo row.
  • JPEG is never pixel-exact. Even q95 changes between 74 and 556 pixels by the pixelmatch threshold, with a worst single-channel difference of 60 to 79 at q95. Quality 60 changes 1244 to 2674 pixels at 1x.

I did not look at where the differences sit, so I can't say how visible q75 or q60 is on text. Open the file at the size a reader sees it before choosing. The numbers only say how much changes.

Time

PNG encoding is the slow part when there is a lot of detail. Across the two runs, the median for the gradient card at 1x was 264 and 267 ms for PNG against 56 to 72 ms for every JPEG quality. The photo card at 2x took 1600 and 1628 ms for PNG and 161 to 323 ms for JPEG. On the solid card the formats were close (72 and 113 ms for PNG at 1x against 51 to 77 ms for JPEG), and the single run-to-run differences there are noise: the same JPEG q95 2x row read 163 ms in one run and 371 ms in the other. The host was also running other services, so treat these as ratios, not absolute numbers.

One option, one error

quality only applies to JPEG. The Playwright docs say it is "not applicable to png images", and passing it with a PNG fails instead of being ignored:

page.screenshot: options.quality is unsupported for the png screenshots
Enter fullscreen mode Exit fullscreen mode

That is useful: if you switch the type in one place and forget the other option, you find out at once. The type option defaults to PNG, so a call without it is a PNG.

What I would use

  • Flat or mostly flat card: keep PNG. Check the size; mine was 44 KiB.
  • Gradient or photo background: JPEG at 85. I would compare q75 on your own card first, because the saving over q85 is 17 to 23 percent in my table, while the changed-pixel count at 1x rises by 19 to 85 percent.
  • Do not use 2x just to be safe. Decide from where the image is shown. If the consumer shows it at 1200 pixels wide, a 2x file is mostly extra bytes.
  • If a gradient is only decoration, replace it with a solid colour and keep PNG. In my test this cut the PNG from 333 KiB to 44 KiB.

Limits of this test

  • One machine, one Chromium build, one font (DejaVu Sans).
  • A synthetic photo. Real photos have more detail in some places and less in others, so the exact ratios will differ.
  • I only tested PNG and JPEG. I did not try converting the PNG afterwards with an optimiser, or WebP/AVIF through another tool.
  • I did not test what any social network does to the image after upload, or its size limits. Check the current rules of the platforms you care about.

Written with AI assistance. Every code sample was run on 2026-11-20 with Playwright 1.63.0 (Chromium 153.0.8010.12) and Node v24.22.3; the output above is from that run.

Top comments (0)