<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: yue xing</title>
    <description>The latest articles on DEV Community by yue xing (@token111).</description>
    <link>https://dev.to/token111</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4087986%2F86b82555-4629-45b8-9c3a-6743e1932cb9.png</url>
      <title>DEV Community: yue xing</title>
      <link>https://dev.to/token111</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/token111"/>
    <language>en</language>
    <item>
      <title>Photo or screenshot? Why a JPEG converter picks quality 80 or 88</title>
      <dc:creator>yue xing</dc:creator>
      <pubDate>Thu, 26 Nov 2026 08:17:01 +0000</pubDate>
      <link>https://dev.to/token111/photo-or-screenshot-why-a-jpeg-converter-picks-quality-80-or-88-1afn</link>
      <guid>https://dev.to/token111/photo-or-screenshot-why-a-jpeg-converter-picks-quality-80-or-88-1afn</guid>
      <description>&lt;p&gt;A converter that turns WebP into JPG has to pick a quality number, and the sensible number is not the same for a photo and for a screenshot. In the setup used here, a photo gets 80 and a screenshot gets 88. Give the photo 88 anyway and it grows by 36.3% to 39.2% while the similarity score barely moves. This post builds that idea from one photo and one screenshot, so a beginner can see why a converter bothers to look at what is inside the picture first.&lt;/p&gt;

&lt;h2&gt;
  
  
  What the quality number means
&lt;/h2&gt;

&lt;p&gt;JPEG throws away detail to save bytes, and the quality number (0 to 100) says how much to throw away. Higher means fewer bytes lost and a bigger file. The tricky part is that the same number does not look the same on every picture. The usual explanation is that a photo is full of soft noise and gradual shifts, so small errors hide in it, while a screenshot has flat colour and sharp letter edges, so the same errors collect next to the text. That explanation was not tested here; only bytes and SSIM were measured.&lt;/p&gt;

&lt;p&gt;So a default cannot be one number. Whoever picked it had to guess what kind of image would come in.&lt;/p&gt;

&lt;h2&gt;
  
  
  Two samples, two tiers
&lt;/h2&gt;

&lt;p&gt;The numbers below come from the lake photo and a self-made table screenshot, both re-encoded as JPEG at four qualities. Sizes are bytes. SSIM is a 0 to 1 similarity score against the lossless original; it is a luminance-only version implemented for this comparison, and nobody judged these images by eye.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Sample&lt;/th&gt;
&lt;th&gt;Quality 80&lt;/th&gt;
&lt;th&gt;Quality 88&lt;/th&gt;
&lt;th&gt;88 vs 80&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Lake photo (1600×1067)&lt;/td&gt;
&lt;td&gt;178,795 B, SSIM 0.9674&lt;/td&gt;
&lt;td&gt;248,925 B, SSIM 0.9764&lt;/td&gt;
&lt;td&gt;+39.2% bytes, +0.0090 SSIM&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Table screenshot (1440×900)&lt;/td&gt;
&lt;td&gt;118,280 B, SSIM 0.9942&lt;/td&gt;
&lt;td&gt;143,289 B, SSIM 0.9972&lt;/td&gt;
&lt;td&gt;+21.1% bytes, +0.0030 SSIM&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Read the photo row first. Going from 80 to 88 costs about 70 KB (counting 1 KB as 1,000 bytes, a subtraction of the two byte counts, not a separate measurement) and the score moves by under 0.01. On the airport photo with a big sky the cost was 36.3% for +0.0084, and across four photos the range was 36.3% to 39.2%.&lt;/p&gt;

&lt;p&gt;The screenshot row looks cheaper, and it is: 21.1% for +0.0030. But the score is the less useful number here. Zoom the text of the screenshot to six times and the difference is easy to see. At quality 50 there is a grey speckle around the letters, at 80 a faint ring is still there, and at 88 it looks close to the original. The extra bytes are a smaller share on a screenshot and the speckle is easy to spot, so 88 is the better place to stand in this setup.&lt;/p&gt;

&lt;h2&gt;
  
  
  What happens when the guess is wrong
&lt;/h2&gt;

&lt;p&gt;Because the screenshot tier is higher than the photo tier in this setup, the two mistakes are not symmetric.&lt;/p&gt;

&lt;p&gt;A photo treated as a screenshot gets 88 instead of 80. It grows by 36.3% to 39.2% and gains at most 0.0090 of SSIM. A screenshot treated as a photo gets 80 instead of 88. It shrinks, by about 17% for the table screenshot (1 − 118,280 / 143,289, arithmetic on two measured sizes), and across eight screenshots, text pages and illustrations the 88 versions were 21.1% to 29.6% larger than the 80 ones. The price is a slightly lower score, 0.0010 to 0.0067, and the extra ring around letters.&lt;/p&gt;

&lt;p&gt;So a wrong guess either wastes bytes or smudges text. Neither breaks anything, which is probably why a converter can get away with a rough classifier.&lt;/p&gt;

&lt;h2&gt;
  
  
  Where one converter's page shows the tiers
&lt;/h2&gt;

&lt;p&gt;The page at &lt;a href="https://imging.ai/" rel="noopener noreferrer"&gt;https://imging.ai/&lt;/a&gt; is ImgIng, a browser-based image tool, and it shows this decision openly. After you drop an image in, it prints a content label such as Photo detected or Screenshot/UI detected, and when the output is JPG it offers four default qualities: 80 for photos, 78 for smooth images, 84 for textured ones, 88 for graphics and text. The 80-versus-88 pair above is taken from those tiers.&lt;/p&gt;

&lt;p&gt;One real example from the same twelve samples: the airport photo, whose top half is mostly sky, was labelled Screenshot/UI detected, so its WebP-to-JPG default became 88. The result was 317,542 B, which is 2.00 times the original WebP of 158,934 B. The lake photo and the portrait got 80 and came out at 1.41 and 1.43 times.&lt;/p&gt;

&lt;h2&gt;
  
  
  What the numbers do not cover
&lt;/h2&gt;

&lt;p&gt;These are twelve sample images on one Apple silicon Mac, encoded once with the JPEG encoder inside Pillow (libjpeg-turbo, 4:2:0 chroma). Another encoder will give different byte counts. Between 78, 80, 84 and 88 there was no obvious knee: each extra 0.001 of SSIM cost somewhere between 2.9% and 7.5% more bytes, rising slowly. Costs got steep only above 88, for example 6.3% to 13.2% per 0.001 on the screenshots going from 88 to 92. The photos are CC0 from Wikimedia Commons, resized or cropped: Bonnie Moreland, "Lake Mountain Landscape"; William Stitt, "Face portrait"; and "Avions à l'aéroport de Madrid - 2015". The web-page screenshot sample is by Stefan Kühn (CC0), the scanned page is from the 1986 edition of the Table of Simplified Chinese Characters (public domain mark), and the illustrations are by Toby8991 and oksmith (CC0) plus one self-made. The table screenshot, the other screenshots and the text page are self-made with fictional data.&lt;/p&gt;

&lt;h2&gt;
  
  
  A small habit to try
&lt;/h2&gt;

&lt;p&gt;Next time a converter hands back a JPG far bigger than the source, look at two things: which quality it used, and whether the picture is a photo. If it is a photo and the quality is in the high 80s, dropping to around 80 is a reasonable first try. If it is a screenshot with small text, keep it high, or keep the WebP for that file.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
    </item>
    <item>
      <title>The halo around a blurred-background portrait is not a bad mask, it is the person's own pixels</title>
      <dc:creator>yue xing</dc:creator>
      <pubDate>Wed, 25 Nov 2026 08:12:16 +0000</pubDate>
      <link>https://dev.to/token111/the-halo-around-a-blurred-background-portrait-is-not-a-bad-mask-it-is-the-persons-own-pixels-41cd</link>
      <guid>https://dev.to/token111/the-halo-around-a-blurred-background-portrait-is-not-a-bad-mask-it-is-the-persons-own-pixels-41cd</guid>
      <description>&lt;p&gt;When you replace a video background with a blurred copy of the original, a smeared ring often shows up around the person. The usual first guess is that the matting mask is sloppy. In the run described here, the ring appears even when the mask is good, because the blur is applied to a picture that still contains the person.&lt;/p&gt;

&lt;p&gt;The numbers below come from one 3-second, 90-frame clip, a few synthetic test figures, and a single run on an Apple-silicon Mac with Python, NumPy and OpenCV. Treat them as readings from these samples, not as general constants. The real footage comes from Wikimedia Commons, VOA Learning English, public domain, cropped, keyed, and placed on a new background. The other test images are self-made.&lt;/p&gt;

&lt;h2&gt;
  
  
  Blur spreads the person into the wall
&lt;/h2&gt;

&lt;p&gt;Think of the frame as two layers. The bottom layer is the original picture, person and wall together. The top layer is the sharp person cut out with the mask. The common recipe blurs the bottom layer, then pastes the sharp person on top.&lt;/p&gt;

&lt;p&gt;Here is the catch. The bottom layer was blurred while the person was still in it, so the person's color has already been averaged into the wall pixels next to the outline. Pasting the sharp person on top hides the person-shaped area, but it cannot take back the color that leaked outward. That leaked color is the ring.&lt;/p&gt;

&lt;p&gt;The other order is to remove the person first, fill the hole from the surrounding wall, blur that, and only then paste the person on top. Same mask, same blur, different order.&lt;/p&gt;

&lt;h2&gt;
  
  
  One mask, two orders
&lt;/h2&gt;

&lt;p&gt;The readings use CIELAB color difference (ΔE) in a band 0 to 4 pixels outside the person, measured against the ideal result of a person pasted on a clean, blurred background. Blur strength is a Gaussian sigma of 18. On the real clip, averaged over 90 frames, blurring first gives 17.10 and filling the hole first gives 0.63. A ΔE around 2.3 is roughly where a difference starts to be noticeable, but no one looked at these frames in a viewing test, so that line is only a reference.&lt;/p&gt;

&lt;p&gt;Strength matters too. On a synthetic figure, sigma 6, 18 and 40 gave visible rings about 11, 30 and 66 pixels wide, with edge ΔE of 16.72, 21.53 and 24.23. Dividing width by sigma gives roughly 1.6 to 1.8, which is arithmetic on these three readings, not a rule.&lt;/p&gt;

&lt;p&gt;The color of the ring is the person's own color. In the synthetic figures, the RGB shift next to the shoulder was (−58, −63, −62) for black clothing, (+4, −60, −59) for red and (+20, +16, +15) for white. So dark clothes on a light wall give a dark edge, red clothes give a red tint, and only the white case is brighter than the wall. It does not have to look like a glow.&lt;/p&gt;

&lt;h2&gt;
  
  
  Mask tweaks barely touch it
&lt;/h2&gt;

&lt;p&gt;Feathering only softens the person layer. The wall already contains the leaked color, so feathering has nothing to remove. On the synthetic figure with a light wall, the readings were these.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Method&lt;/th&gt;
&lt;th&gt;Light wall&lt;/th&gt;
&lt;th&gt;High-contrast shelf&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Blur first&lt;/td&gt;
&lt;td&gt;21.53&lt;/td&gt;
&lt;td&gt;15.75&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Blur first, feather 3 px&lt;/td&gt;
&lt;td&gt;29.40&lt;/td&gt;
&lt;td&gt;20.29&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Fill hole, mask 3 px smaller than person&lt;/td&gt;
&lt;td&gt;17.98&lt;/td&gt;
&lt;td&gt;13.44&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Fill hole, mask on the outline&lt;/td&gt;
&lt;td&gt;0.34&lt;/td&gt;
&lt;td&gt;6.72&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Fill hole, mask grown 15 px&lt;/td&gt;
&lt;td&gt;1.94&lt;/td&gt;
&lt;td&gt;21.47&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Feathering made the number worse. Part of that is the metric, since feathering pushes person color into the measured band, so read it as "does not help" rather than "hurts by this much". A mask smaller than the person leaves edge pixels in the part that gets blurred, so filling the hole does almost nothing. Growing the mask too far is also a problem: on the shelf, 21.47 is worse than not fixing at all.&lt;/p&gt;

&lt;p&gt;The fill is a smooth guess from nearby pixels, not the real hidden background. That is why the shelf keeps a residue of 6.72 while the plain wall drops to 0.34. Only Telea, Navier-Stokes and normalized convolution from OpenCV were tried. Learned inpainting models were not.&lt;/p&gt;

&lt;p&gt;The fix costs time. At 1080p on a single CPU thread, blurring alone took 162.4 ms per frame and the Telea fill plus blur took 398.0 ms, about 2.5 times as much. That says nothing about GPU or in-browser speed. Flicker also differs: on this static-background clip the ring moved with the person, with a frame-to-frame difference of 0.322 against 0.016 to 0.052 after filling. Moving backgrounds were not tested.&lt;/p&gt;

&lt;h2&gt;
  
  
  Where ImgIng fits in this
&lt;/h2&gt;

&lt;p&gt;The mask for the real-person clip came from the video background tool in ImgIng &lt;a href="https://imging.ai/" rel="noopener noreferrer"&gt;https://imging.ai/&lt;/a&gt; , a Beta feature that is meant for desktop Chrome or Edge. Its "blur original scene" background option gave an edge that matched what blurring the whole original picture, person included, would predict, so the ring shows up there too. For a clean edge, pick the transparent background, which in that tool exports WebM only, and composite in the order above. Check that whoever receives the file can open WebM. Long videos, 4K, older devices, fast motion, semi-transparent objects and shot cuts may be slow or show edge errors, and none of those were measured here.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
    </item>
    <item>
      <title>A video labelled 60 fps averaged 30.68: timestamps decide when a frame shows</title>
      <dc:creator>yue xing</dc:creator>
      <pubDate>Tue, 24 Nov 2026 08:11:18 +0000</pubDate>
      <link>https://dev.to/token111/a-video-labelled-60-fps-averaged-3068-timestamps-decide-when-a-frame-shows-jeh</link>
      <guid>https://dev.to/token111/a-video-labelled-60-fps-averaged-3068-timestamps-decide-when-a-frame-shows-jeh</guid>
      <description>&lt;p&gt;A frame does not play at "frame number divided by fps". It plays at the timestamp stored with it, called the PTS. One clip makes the point: its label said 60 fps, its frames averaged 30.68 fps. Here is the mental model first, before any ffmpeg flag.&lt;/p&gt;

&lt;p&gt;The numbers come from two small test clips on one Apple-silicon Mac, one run each. The first is a synthetic 20-second clip generated with deliberately uneven frame times. The second is a canvas animation recorded with MediaRecorder in a desktop browser. They cover these two files only, so don't stretch them to phone videos, which were not tested.&lt;/p&gt;

&lt;h2&gt;
  
  
  What a file stores for each frame
&lt;/h2&gt;

&lt;p&gt;It is easy to picture a video as a pile of pictures plus one number, the fps. Real containers store a timestamp per frame, and the player just shows each picture when the clock reaches its timestamp. A constant frame rate (CFR) file is the special case where the gap between neighbouring timestamps is always the same. A variable frame rate (VFR) file is any file where it isn't.&lt;/p&gt;

&lt;p&gt;So "how many frames" and "when is each frame shown" are two different questions. The second one is what keeps audio and picture together.&lt;/p&gt;

&lt;h2&gt;
  
  
  A label of 60 that averaged 30.68
&lt;/h2&gt;

&lt;p&gt;The synthetic clip reported &lt;code&gt;r_frame_rate&lt;/code&gt; of 60/1. It holds 613 frames over about 20 seconds, which works out to 30.677 fps on average. The gaps between frames ran from 8.333 ms (60 fps territory) through a median of 16.667 ms up to 100 ms, and the 95th percentile already sat at 100 ms. Nothing here is broken. The file simply shows frames quickly in some stretches and slowly in others.&lt;/p&gt;

&lt;p&gt;The label is a container-level hint. Only the per-frame timestamps tell you what actually happens.&lt;/p&gt;

&lt;h2&gt;
  
  
  When the metadata tells you nothing
&lt;/h2&gt;

&lt;p&gt;The browser recording was stranger. Its &lt;code&gt;r_frame_rate&lt;/code&gt; reads 1000/1, which looks like 1000 fps until you see it is only a millisecond time base. The average rate field is 0/0 and the duration is N/A. The 444 per-frame timestamps give 18.69 fps on average, with gaps from 6 ms to 664 ms.&lt;/p&gt;

&lt;p&gt;How common this is for other recorders is not known from this run. The sample is one recording path, with no screen recorder and no phone.&lt;/p&gt;

&lt;h2&gt;
  
  
  The mistake I expected, and the one that actually hurt
&lt;/h2&gt;

&lt;p&gt;The usual suspect for broken audio sync is converting VFR to CFR. Converting to a fixed rate the normal way (&lt;code&gt;-fps_mode cfr -r 30&lt;/code&gt;) barely moved anything here. On the synthetic clip the worst audio-to-picture offset was 67 ms, about two frames. The cost was something else: ffmpeg had to duplicate 151 frames and drop 165, and 2 of the 10 white-flash sync markers in the clip disappeared. On the browser clip it duplicated 483, dropped 204, and lost 1 of 12 markers.&lt;/p&gt;

&lt;p&gt;What really broke sync was rewriting the timestamps as if the label were true, with &lt;code&gt;setpts=N/(fps*TB)&lt;/code&gt;. That line says "frame N is shown at N divided by the nominal fps", which throws the original PTS away. The synthetic clip's video shrank from 19.9 s to 10.2 s against 20 s of audio, and the end offset reached 8.58 s. The browser clip went to 14.8 s and ended 7.50 s off.&lt;/p&gt;

&lt;p&gt;Same CFR idea, two very different results. The difference is whether the timestamps are recomputed from the real frame times or invented from the label.&lt;/p&gt;

&lt;h2&gt;
  
  
  What to check before you touch a flag
&lt;/h2&gt;

&lt;p&gt;Three checks, in this order. First, look at the per-frame timestamps, not the header, and compute min, median and max gap. Second, if you want a fixed rate, let the tool resample (it will duplicate and drop frames, so count them). Third, never rebuild timestamps from &lt;code&gt;N/fps&lt;/code&gt; unless you know the file really is constant.&lt;/p&gt;

&lt;p&gt;One more place this matters is the encoder's bit budget. The label also matters when an encoder sizes its bit budget from the frame rate. Take 0.08 bit per pixel per frame: with the nominal 60 fps the synthetic clip got a 4.42 Mbps target and ended at 10.05 MB, while the real 30.68 fps gave 2.26 Mbps and 5.52 MB. SSIM was 0.9959 against 0.9913, so about 82% more bytes bought 0.0046 of SSIM on this clip. The browser clip was too simple to use its whole budget, and the two outputs differed by only about 16%.&lt;/p&gt;

&lt;p&gt;ImgIng's September 2026 update notes say it sets the bitrate budget from the actual frame rate and keeps the original timestamps instead of forcing extra frames. That is its stated approach and nothing more: these two clips did not go through it, so how it behaves on them is not tested. It is at &lt;a href="https://imging.ai/" rel="noopener noreferrer"&gt;https://imging.ai/&lt;/a&gt; and runs in a desktop browser.&lt;/p&gt;

&lt;p&gt;How much of this carries over to long videos is open. The longest sample was 30 seconds.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Extract images from a PDF vs convert pages to images: why a 10-page PDF gave 0 files</title>
      <dc:creator>yue xing</dc:creator>
      <pubDate>Mon, 23 Nov 2026 08:13:51 +0000</pubDate>
      <link>https://dev.to/token111/extract-images-from-a-pdf-vs-convert-pages-to-images-why-a-10-page-pdf-gave-0-files-mm5</link>
      <guid>https://dev.to/token111/extract-images-from-a-pdf-vs-convert-pages-to-images-why-a-10-page-pdf-gave-0-files-mm5</guid>
      <description>&lt;p&gt;Some PDF toolboxes list "extract images" and "PDF to image" side by side, and the names sound like one job with two labels. A small test on 2026-11-20 put the same files through both on one Apple-silicon Mac, each tool run once. A 10-page PDF that contains only text (137,782 bytes) came back from extraction with 0 files. Rendered at 216 DPI, the same PDF produced 10 page images, 2,881,860 bytes as WebP. Neither run failed. They are two different operations that both end in image files.&lt;/p&gt;

&lt;h2&gt;
  
  
  A text-only PDF has nothing to extract
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;pdfimages -all&lt;/code&gt; from poppler 26.08.0 wrote 0 files for the text-only PDF. A browser tool, ImgIng at &lt;a href="https://imging.ai/" rel="noopener noreferrer"&gt;https://imging.ai/&lt;/a&gt; , reported 0 results and 0 KB for it too.&lt;/p&gt;

&lt;p&gt;The second sample is a home-made one-page PDF with a few rectangles, a circle, a polyline and one line of text, 1,303 bytes for the whole file. Extraction gave 0 files again, and &lt;code&gt;pdfimages&lt;/code&gt; printed nothing at all. Rendering at 216 DPI gave one 1785×2526 image: 41,388 bytes as WebP, 94,317 as JPG, 148,530 as PNG. &lt;code&gt;pdftoppm -r 216&lt;/code&gt; wrote a 44,563-byte PNG and a 109,413-byte JPEG, so the exact size moves with the encoder. A 1,303-byte file with no picture in it still becomes a full page of pixels.&lt;/p&gt;

&lt;h2&gt;
  
  
  Extraction copies objects and rendering paints the page
&lt;/h2&gt;

&lt;p&gt;A PDF page is a list of drawing instructions: put these characters here in this font, stroke this path, place image object number N inside this rectangle. Text and shapes exist only as instructions, while a photo is a separate object stored in the file with its own pixel size and compression.&lt;/p&gt;

&lt;p&gt;Extraction walks the file and copies those image objects out. When there are none, zero is the correct answer and not an error. Rendering runs every instruction against a blank canvas at the resolution you ask for and saves the canvas, so it returns one image per page, sized by page size and DPI. An A4 page at 216 DPI was 1785×2526 or 1786×2526 here, whether it held text or three shapes. Adobe's help page for Acrobat carries a note in the same direction (read on 2026-11-20): raster images can be exported, vector objects cannot. Acrobat itself was not part of this test.&lt;/p&gt;

&lt;h2&gt;
  
  
  File counts follow objects in one case and pages in the other
&lt;/h2&gt;

&lt;p&gt;Once pictures are present the two operations still disagree. The counts below come from the browser tool.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Sample&lt;/th&gt;
&lt;th&gt;Pages&lt;/th&gt;
&lt;th&gt;Extracted files&lt;/th&gt;
&lt;th&gt;Rendered files&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Home-made product catalogue, product images stored oversized on purpose&lt;/td&gt;
&lt;td&gt;6&lt;/td&gt;
&lt;td&gt;8&lt;/td&gt;
&lt;td&gt;6&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Three-page sheet with a text page, a chart page and a photo page&lt;/td&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Creative Commons newsletter&lt;/td&gt;
&lt;td&gt;10&lt;/td&gt;
&lt;td&gt;32&lt;/td&gt;
&lt;td&gt;10&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The catalogue has 8 image objects drawn 19 times across its 6 pages. Extraction returned 8 files, one per object, and its 7 JPEGs were byte-identical to the data inside the PDF (same SHA-256). Rendering returned 6 files because there are 6 pages. The photo inside is Bonnie Moreland's "Lake Mountain Landscape" from Wikimedia Commons, CC0; the catalogue around it is home-made and its content is fictional. The newsletter row is Creative Commons' ccNewsletter #12 (2009), CC BY 3.0.&lt;/p&gt;

&lt;h2&gt;
  
  
  A scanned page is an image even when it looks like text
&lt;/h2&gt;

&lt;p&gt;The reverse case exists too. One sample was a home-made two-page scan: each page was rendered as a 2481×3508 JPEG and embedded whole, with no text layer. On screen it looks like two ordinary pages. Extraction returned 2 files, and they are the full pages. Extraction reports what the file stores, not what the page looks like.&lt;/p&gt;

&lt;h2&gt;
  
  
  Extracted originals are not always the bigger files
&lt;/h2&gt;

&lt;p&gt;Extracted originals sound like they should outweigh page renders, and on these samples that held only sometimes. Against 216 DPI WebP pages, the catalogue's extracted files were 5.83 times the size (5,410,209 bytes against 928,416), which is the deliberately oversized sample, and the three-page sheet's were 1.32 times (897,884 against 679,406). The newsletter went the other way at 0.22 times. Against PNG page renders, extraction was smaller on every sample in the comparison, between 0.04 and 0.54 times.&lt;/p&gt;

&lt;h2&gt;
  
  
  Pick by what you want to keep
&lt;/h2&gt;

&lt;p&gt;For a photo at the resolution it was stored in, extraction can give it back unchanged; the JPEGs here came out byte-identical. For a chart, a formula or a whole slide, there may be no image object to copy, so rendering at a chosen DPI is what produces a file. A zero from extraction more likely says "this content is vector or text" than "the tool is broken". Two things stay open: JPEG 2000 and JBIG2 images were not in any sample, and Acrobat's own export was not tested.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Real-ESRGAN tile seams: the biggest pixel errors were on lines and text, not the sky</title>
      <dc:creator>yue xing</dc:creator>
      <pubDate>Sun, 22 Nov 2026 08:08:48 +0000</pubDate>
      <link>https://dev.to/token111/real-esrgan-tile-seams-the-biggest-pixel-errors-were-on-lines-and-text-not-the-sky-aja</link>
      <guid>https://dev.to/token111/real-esrgan-tile-seams-the-biggest-pixel-errors-were-on-lines-and-text-not-the-sky-aja</guid>
      <description>&lt;p&gt;Upscaling a big image in tiles keeps memory down, and pasting the tiles back with no overlap leaves seams. A natural guess is that those seams show up most on gradient sky, on flat colour, and on lines or text that cross a tile boundary. In a test on 2026-11-20, half of that held. By pixel difference, lines and text were far worse than anything else, and the sky barely registered. Yet the sky seam is the one a person can spot with no tooling. "Obvious" was doing two jobs in the guess.&lt;/p&gt;

&lt;p&gt;Real-ESRGAN General x4v3 ran through Python and onnxruntime on 4 CPU threads, with a 480×480 input and a 1920×1920 output. The home-made test image has four bands: a gradient sky, a flat colour, straight lines (one horizontal line placed exactly on a tile boundary, plus a few diagonals), and text at 13 to 20 px. It was cut into 25 tiles of 96×96 with no extra context, and the pasted result was compared with the same image sent through the model in one pass. Differences are in grey levels on the 0–255 scale, averaged over the three channels, inside a band of 4 output pixels on each side of every seam. Three runs gave identical numbers. This is one image and one model.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Band&lt;/th&gt;
&lt;th&gt;Mean diff&lt;/th&gt;
&lt;th&gt;Max diff&lt;/th&gt;
&lt;th&gt;Step ratio&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Gradient sky&lt;/td&gt;
&lt;td&gt;0.708&lt;/td&gt;
&lt;td&gt;19&lt;/td&gt;
&lt;td&gt;1.59&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Flat colour&lt;/td&gt;
&lt;td&gt;2.234&lt;/td&gt;
&lt;td&gt;17&lt;/td&gt;
&lt;td&gt;1.83&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Straight lines&lt;/td&gt;
&lt;td&gt;4.252&lt;/td&gt;
&lt;td&gt;249&lt;/td&gt;
&lt;td&gt;3.14&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Text&lt;/td&gt;
&lt;td&gt;4.534&lt;/td&gt;
&lt;td&gt;227&lt;/td&gt;
&lt;td&gt;2.74&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  Lines and text had the largest pixel error
&lt;/h2&gt;

&lt;p&gt;A maximum of 249 out of 255 is close to a black pixel turning white. In the enlarged crops from this run, the horizontal line that sits on the boundary changes thickness and colour where it meets the seam, and the diagonals come out shifted on either side. A likely reason, which the test did not isolate: at a hard edge neighbouring pixels already differ a lot, so a slight disagreement between two tiles about where the edge sits becomes a very large per-pixel number. On a real 480×480 landscape photo whose top quarter is sky (same tile 96, no padding), the sky part of the seams had a mean of 0.693 and a maximum of 3, while the mountains, trees and water had 2.008 and 145.&lt;/p&gt;

&lt;h2&gt;
  
  
  The sky seam is small in numbers and still easy to see
&lt;/h2&gt;

&lt;p&gt;The sky band never differs from the reference by more than 19 levels, and its mean is under 1. Still, in the crops of the sky and flat bands a very faint line is visible without any contrast boost. The seam is one straight step running across the whole image, and a smooth area has no texture to hide it. In the text band a far larger error lands among strokes that already jump between dark and light. That part is inference too. None of the three columns ranks the sky first. A number that predicts "a person will notice this" is not in this table, and which metric would give one was not tested.&lt;/p&gt;

&lt;h2&gt;
  
  
  The two meanings of obvious need different checks
&lt;/h2&gt;

&lt;p&gt;Numerically large is a question about a reference, so the check is a diff against a whole-image run watching the maximum on hard edges. Easy to notice has to be checked on the output alone. Step ratio is the closest thing in this data: the brightness jump across the seam column divided by the jump across ordinary columns nearby, where about 1 means no step. The snippet is simplified from that definition; the table came from the full script.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;step_ratio&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;gray&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;seam_cols&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;g&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;np&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;abs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;np&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;diff&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;gray&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;axis&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# jump between neighbouring columns&lt;/span&gt;
    &lt;span class="n"&gt;seam&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;np&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;mean&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;g&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;mean&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;seam_cols&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;near&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;np&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;mean&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;g&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;mean&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;seam_cols&lt;/span&gt;
                    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="nb"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="nb"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;seam&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="n"&gt;near&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It reads 1.59 on a sky that visibly has a line, so it is only a hint.&lt;/p&gt;

&lt;h2&gt;
  
  
  With 12 pixels of padding the order by mean flips
&lt;/h2&gt;

&lt;p&gt;The fix is to give each tile extra pixels on every side and crop them off after inference. With 12 pixels the maximum drops to 5 on lines, 6 on text, 2 on sky and 4 on flat colour. The mean now puts flat colour on top at 0.505, above text at 0.142 and lines at 0.126, and why that happens was not investigated here. On the landscape photo with tile 96, 34 pixels of padding made the tiled output identical to the one-pass output pixel for pixel, a result for this purely convolutional model only.&lt;/p&gt;

&lt;p&gt;As an outside reference, the browser upscaler ImgIng, at &lt;a href="https://imging.ai/" rel="noopener noreferrer"&gt;https://imging.ai/&lt;/a&gt; , has a fast mode that its product notes describe as 160-pixel tiles with a 12-pixel overlap. Its fast 4× output for three 256×256 samples was subtracted from a one-pass CPU run of the same model file. The differences pile up around output column and row 640, which is input pixel 160, with a maximum of 2 levels on two samples and 16 on the one with thin white scratches drawn on it. Part of that is GPU-versus-CPU float difference, so only the position is solid. The three samples were aged by script from CC0 photos on Wikimedia Commons (portrait by William Stitt, landscape by Bonnie Moreland, the same source photo as the landscape above); the test image with four bands is self-made.&lt;/p&gt;

&lt;p&gt;For your own tiled pipeline two small crops cover both meanings: one with a line crossing a tile boundary, diffed against a one-pass run, and one of plain sky viewed at 100%.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
    </item>
    <item>
      <title>OCR returned 40 text boxes for a 34-cell table: text boxes are not cells</title>
      <dc:creator>yue xing</dc:creator>
      <pubDate>Sat, 21 Nov 2026 08:09:04 +0000</pubDate>
      <link>https://dev.to/token111/ocr-returned-40-text-boxes-for-a-34-cell-table-text-boxes-are-not-cells-10kk</link>
      <guid>https://dev.to/token111/ocr-returned-40-text-boxes-for-a-34-cell-table-text-boxes-are-not-cells-10kk</guid>
      <description>&lt;p&gt;The easy assumption is that OCR on a picture of a table returns a list of cells, so a spreadsheet is one split on Tab away, or on whitespace if the Tabs look odd. In this test the counts did not support it. A work-order table with 34 filled cells came back as 40 text boxes. A price list with 63 cells came back as 58. Neither equals the number of cells, and they miss in opposite directions.&lt;/p&gt;

&lt;p&gt;The text came from ImgIng &lt;a href="https://imging.ai/" rel="noopener noreferrer"&gt;https://imging.ai/&lt;/a&gt; on its default tier, in a desktop browser on an Apple-silicon Mac, with both the TXT and the JSON exported. The seven sample tables are self-made with made-up data, clean and not tilted, one per kind, each recognised once. "Pasting" below means a small program that splits each line on Tab and fills a grid, not a real spreadsheet app.&lt;/p&gt;

&lt;h2&gt;
  
  
  A text box is a run of ink
&lt;/h2&gt;

&lt;p&gt;Each entry in the JSON is a string plus four corner points. There is no row number and no column number in it, so nothing in the export says a grid exists. A cell is something a reader sees from ruling lines and alignment.&lt;/p&gt;

&lt;p&gt;On four of the seven tables the two counts agreed: 63 boxes for 63 cells twice, 45 for 45, 42 for 42. So "boxes are cells" can look right on a clean table. It is not a rule, and equal counts do not prove the columns are right either: the bordered grade table had 63 boxes for 63 cells and still 60 misplaced cells in the simulated paste.&lt;/p&gt;

&lt;h2&gt;
  
  
  Wrapped cells give more boxes than cells
&lt;/h2&gt;

&lt;p&gt;The work-order table is 7 rows by 5 columns with 34 non-empty cells, and six of them wrap onto a second line. The JSON had 40 boxes, which is 34 plus 6. Each text line is its own box, so a two-line cell is two boxes with nothing tying them together.&lt;/p&gt;

&lt;p&gt;The TXT export has 7 lines, matching the 7 rows. But each line split into only 2 pieces on Tab, for 5 columns, and 0 of the 34 cells landed in the right place.&lt;/p&gt;

&lt;h2&gt;
  
  
  Uneven row spacing gave fewer boxes than cells
&lt;/h2&gt;

&lt;p&gt;The price list is 9 rows by 7 columns, 63 cells. With even row heights the count was 63 boxes. With the same content and row heights varied between 44 and 96 px it was 58. The header labels 货号 (item number) and 品名 (product name) came back as one box reading 货号品名, and the three headers on the right came back as another one. Why vertical spacing would change which horizontal neighbours merge was not investigated, and there is only one table of this kind.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why splitting on whitespace cannot fix either case
&lt;/h2&gt;

&lt;p&gt;A merged header defeats a whitespace fallback directly, because inside 货号品名 there is no whitespace at all. The bordered grade table shows the same: its exported header is three Tab-separated labels followed by four headers written as one unbroken string. In the exports from this test a Tab appeared where the gap was wide, and a narrow gap became a space or nothing. Whether other table-OCR tools behave the same was not tried. On the borderless price list the nine lines split into 6, 2, 5, 4, 5, 3, 5, 4 and 5 pieces for a 7-column table.&lt;/p&gt;

&lt;p&gt;The wrapped case fails from the other side. Both lines of a wrapped cell end up on the same output line, and no character says which pieces belong together.&lt;/p&gt;

&lt;p&gt;The corner points are the way out. Boxes can be grouped into rows by centre y, and column gaps looked for where at least 80% of the rows are empty. The minimum gap width was picked after seeing results: 0.4 of the box height merged two columns on the borderless price list, 0.2 did not. With that, the bordered and borderless tables came out with 0 misplaced cells. The wrapped table still had 4 misplaced with borders and 11 without. The uneven one stayed at 63 of 63, because the merged box leaves a column gap of only 4 px. Cutting along ruling lines, which only works when lines exist, found 35 cells on the wrapped table and placed everything correctly. This test found no rule that joins wrapped lines correctly on the borderless version.&lt;/p&gt;

&lt;p&gt;A cheap check before pasting is to count the boxes against the cells the table should have. If the numbers differ, the columns have to come from lines or gaps in the image, not from whitespace.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
    </item>
    <item>
      <title>What a PDF xref table actually is, read through qpdf --show-xref before and after object streams</title>
      <dc:creator>yue xing</dc:creator>
      <pubDate>Fri, 20 Nov 2026 08:12:58 +0000</pubDate>
      <link>https://dev.to/token111/what-a-pdf-xref-table-actually-is-read-through-qpdf-show-xref-before-and-after-object-streams-4m97</link>
      <guid>https://dev.to/token111/what-a-pdf-xref-table-actually-is-read-through-qpdf-show-xref-before-and-after-object-streams-4m97</guid>
      <description>&lt;p&gt;Run &lt;code&gt;qpdf --show-xref&lt;/code&gt; on a plain text-only PDF and every line looks like &lt;code&gt;2/0: uncompressed; offset = 131&lt;/code&gt;. Rewrite the file with &lt;code&gt;--object-streams=generate&lt;/code&gt;, run it again, and the second line reads &lt;code&gt;2/0: compressed; stream = 1, index = 0&lt;/code&gt;. In this test the file went from 137,782 to 132,065 bytes, 4.15% smaller, same 10 pages, same text. I knew the words "xref table" and "object stream" but couldn't have explained that one-line change, so here is the beginner's model that made it readable, plus the part I didn't expect: 4% is close to the ceiling for this file. The test file is a 10-page text-only report with fictional content and an embedded Chinese font subset, run once on one Apple-silicon Mac with qpdf 12.4.0, so every number means "this file, this run".&lt;/p&gt;

&lt;h2&gt;
  
  
  The xref table is a list of byte offsets
&lt;/h2&gt;

&lt;p&gt;A PDF is a pile of numbered objects: pages, fonts, content streams, and a catalog pointing at the page tree. A reader doesn't scan the whole file to find object 2. At the end of a classic PDF sits the cross-reference table, the xref, with one entry per object number giving the byte where that object starts. The trailer after it points at the root, and a &lt;code&gt;startxref&lt;/code&gt; line says where the table itself is. So opening a PDF is roughly: jump to the end, find the xref, read the offsets, seek straight to the object you need.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ qpdf --show-xref notes.pdf | head -3
1/0: uncompressed; offset = 61
2/0: uncompressed; offset = 131
3/0: uncompressed; offset = 238
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;2/0&lt;/code&gt; is object 2, generation 0, starting at byte 131. Object 1 starts at byte 61 because the first 61 bytes are the file header. This file has 37 top-level objects, and the table plus trailer take 977 bytes, 0.71% of the file.&lt;/p&gt;

&lt;h2&gt;
  
  
  After object streams the offsets point into a stream
&lt;/h2&gt;

&lt;p&gt;PDF 1.5 added two features that travel together. An object stream is one stream object holding many small non-stream objects (dictionaries, arrays, numbers) back to back, Flate-compressed as a whole. An object inside a compressed stream has no byte offset of its own, so a plain-text table can't describe it. That's why the cross-reference stream exists: the xref becomes a compressed stream whose entries say either "starts at byte N" or "item i inside object stream S".&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;$ qpdf --object-streams=generate notes.pdf notes-os.pdf &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; wc -c notes.pdf notes-os.pdf
  137782 notes.pdf
  132065 notes-os.pdf
  269847 total

$ qpdf --show-xref notes-os.pdf &lt;span class="o"&gt;|&lt;/span&gt; head -3
1/0: uncompressed&lt;span class="o"&gt;;&lt;/span&gt; offset &lt;span class="o"&gt;=&lt;/span&gt; 15
2/0: compressed&lt;span class="o"&gt;;&lt;/span&gt; stream &lt;span class="o"&gt;=&lt;/span&gt; 1, index &lt;span class="o"&gt;=&lt;/span&gt; 0
3/0: compressed&lt;span class="o"&gt;;&lt;/span&gt; stream &lt;span class="o"&gt;=&lt;/span&gt; 1, index &lt;span class="o"&gt;=&lt;/span&gt; 1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With that model the new lines make sense. Object 1 is a normal object at byte 15, and &lt;code&gt;stream = 1&lt;/code&gt; says objects 2 and 3 live inside it at positions 0 and 1, so object 1 is the object stream. This run didn't check whether the new object 2 is the same object as the old one, since qpdf writes a whole new file, so I read it only as "small objects moved into object 1". Stream objects such as fonts and page content can't go inside an object stream at all, so they keep showing "uncompressed" with an offset even though their data is Flate-compressed. Here "uncompressed" means "not inside an object stream", and that's the word that is easy to misread.&lt;/p&gt;

&lt;h2&gt;
  
  
  The saving is bigger than the bookkeeping
&lt;/h2&gt;

&lt;p&gt;The first half of this is subtraction. A plain qpdf rewrite with no options already gave 137,382 bytes, so object streams account for 5,317 of the 5,717. Page and structure objects took 2,685 bytes and the xref plus trailer 977. Even if both vanished, that's 3,662, short of 5,317. The rest turns up when the original's bytes are split into stream and non-stream objects: 3,512 of the font bytes are non-stream objects such as font dictionaries and /W width arrays, which an object stream can pack and compress. After the rewrite 3,536 bytes had left the font column and moved into the 1,206-byte object stream along with the page objects. A constructed file that stores one font five times shows the same effect more loudly, its font bytes going from 266,381 to 134,332 with object streams on.&lt;/p&gt;

&lt;h2&gt;
  
  
  Fonts are 88% of this file so the ceiling is low
&lt;/h2&gt;

&lt;p&gt;Fonts take 121,442 bytes (88.14%) and content streams 12,617 (9.16%), and object streams leave the font data itself alone. Stacking &lt;code&gt;--recompress-flate --compression-level=9&lt;/code&gt; on top reached 130,933 bytes, −4.97%. The reverse check shows why little is left: writing the file with every stream decompressed made it 309,649 bytes, 2.25 times the original. Image-heavy files gain even less. Object streams alone made a scanned PDF 0.02% smaller and a text-plus-photo PDF 0.74% smaller, since images are 99.84% and 95.93% of their bytes. The photo in that second sample comes from Wikimedia Commons (CC0).&lt;/p&gt;

&lt;p&gt;For compressing PDFs I use ImgIng &lt;a href="https://imging.ai/" rel="noopener noreferrer"&gt;https://imging.ai/&lt;/a&gt; , which processes the file locally in the desktop browser. Its lossless structure mode took the same text file to 131,156 bytes, −4.8%, the same range as qpdf with everything on, and left the scanned file almost unchanged at −0.03%, which matches the breakdown.&lt;/p&gt;

&lt;p&gt;To check your own file, &lt;code&gt;qpdf --show-xref yourfile.pdf | head&lt;/code&gt; is enough. If every line says "uncompressed; offset", the file uses a classic xref table and &lt;code&gt;--object-streams=generate&lt;/code&gt; may save a few percent. If lines already say "compressed; stream", it was probably written with object streams and there's little structural slack left. Compare &lt;code&gt;wc -c&lt;/code&gt; before and after, and when a text-only PDF barely moves, the bytes are most likely in the fonts.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Does re-saving a JPEG keep making it worse? At quality 75 mine stopped changing at generation 15</title>
      <dc:creator>yue xing</dc:creator>
      <pubDate>Thu, 19 Nov 2026 08:12:14 +0000</pubDate>
      <link>https://dev.to/token111/does-re-saving-a-jpeg-keep-making-it-worse-at-quality-75-mine-stopped-changing-at-generation-15-bdb</link>
      <guid>https://dev.to/token111/does-re-saving-a-jpeg-keep-making-it-worse-at-quality-75-mine-stopped-changing-at-generation-15-bdb</guid>
      <description>&lt;p&gt;"Never re-save a JPEG, every save makes it worse" was my working rule for image formats. The picture behind it was a photocopy of a photocopy, each generation a bit blurrier and a bit smaller. Then came a test: one photo saved 20 times in a row, with every generation measured. With the same encoder and the same quality number, the photo lost almost everything it was going to lose in the first save. After that it barely moved, and then it stopped.&lt;/p&gt;

&lt;h2&gt;
  
  
  Almost all the loss was in the first save
&lt;/h2&gt;

&lt;p&gt;The setup is small. One 1200×672 countryside photo (trees, a stone wall, grass), kept as a PNG to serve as the clean original. It was saved as JPEG with Pillow, which uses libjpeg-turbo underneath, with 4:2:0 chroma subsampling, then reopened and re-saved at the same quality for 20 generations. Each one was compared with the PNG by PSNR, where higher means closer to the original. It ran once, on one Apple-silicon Mac, so every number is "this photo, this run".&lt;/p&gt;

&lt;p&gt;At quality 75 the first save landed at 29.70 dB. Generation 5 was 29.58 and generation 20 was 29.55. The next 19 saves together cost 0.15 dB. The file did not shrink either. Generation 1 was 209,453 bytes and generation 20 was 209,415, 38 bytes apart. At every quality in the test the size moved by less than 2%, so the "smaller every time" half of my old picture was simply wrong.&lt;/p&gt;

&lt;h2&gt;
  
  
  The file stopped changing at generation 15
&lt;/h2&gt;

&lt;p&gt;What surprised me most is that the curve doesn't just flatten, it ends. From generation 15 onward the decoded pixels were identical to the previous generation, and so were the bytes. You can see it without any metric, using the libjpeg-turbo command-line tools and a checksum (&lt;code&gt;md5 -q&lt;/code&gt; is the macOS one), with &lt;code&gt;photo.ppm&lt;/code&gt; as the original:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;$ cjpeg -quality 75 -outfile g01.jpg photo.ppm&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; i &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="nv"&gt;$(&lt;/span&gt;seq 2 20&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt; djpeg -ppm g&lt;span class="nv"&gt;$(&lt;/span&gt;&lt;span class="nb"&gt;printf&lt;/span&gt; %02d &lt;span class="nv"&gt;$(&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;i-1&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;.jpg &lt;span class="o"&gt;|&lt;/span&gt; cjpeg -quality 75 -outfile g&lt;span class="nv"&gt;$(&lt;/span&gt;&lt;span class="nb"&gt;printf&lt;/span&gt; %02d &lt;span class="nv"&gt;$i&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;.jpg&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="k"&gt;done&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; md5 -q g01.jpg g02.jpg g14.jpg g15.jpg g16.jpg g20.jpg&lt;span class="o"&gt;;&lt;/span&gt; ls -l g01.jpg g20.jpg &lt;span class="o"&gt;|&lt;/span&gt; awk &lt;span class="s1"&gt;'{print $5, $9}'&lt;/span&gt;
9c8682676c38b3355fc8a6622ed471df
63cf1b8d26ed75c7e63e0f0eb3a209cc
025f8fa26b289855a0ca096bafa99258
025f8fa26b289855a0ca096bafa99258
025f8fa26b289855a0ca096bafa99258
025f8fa26b289855a0ca096bafa99258
209453 g01.jpg
209415 g20.jpg
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Generations 1 and 2 differ, so the second save did change something. Generations 14, 15, 16 and 20 share one checksum: saving again produces the file you already had. My way of explaining it to myself: a JPEG save rounds the image's frequency data to a grid of allowed values, and the quality number sets how coarse the grid is. After the first save the data already sits on that grid, so a second pass has little left to round, and each further pass leaves fewer values near enough to an edge to flip. Nothing in this test confirms it, since no coefficients were dumped, so it is a working model and not a proof.&lt;/p&gt;

&lt;p&gt;Generation 15 is not a property of JPEG, though. A second photo, a lake with smooth water and sky, went from 34.69 to 34.65 dB at quality 75 and stopped changing at generation 10.&lt;/p&gt;

&lt;h2&gt;
  
  
  Quality 90 and above had not settled in 20 saves
&lt;/h2&gt;

&lt;p&gt;Against my guess, higher quality was worse at settling. The bridge photo at quality 90 started at 34.21 dB and was at 33.08 by generation 20, and it was still changing: 4,198 pixels differed between generations 19 and 20. At quality 95 it fell from 36.81 to 33.75, a drop of 3.06 dB against 0.15 at quality 75. By generation 20 the quality 95 chain was only 0.67 dB ahead of the quality 90 chain while its file was 36% larger. There is no tested explanation for this here, and where these chains end is unknown because the test stopped at 20. A self-made UI screenshot did worse still: at quality 90 it lost 6.13 dB over 20 generations (37.99 to 31.86) and had not settled by generation 20.&lt;/p&gt;

&lt;h2&gt;
  
  
  Changing the quality between saves kept the damage going
&lt;/h2&gt;

&lt;p&gt;So my mental model is different now. This photo at quality 75, re-saved with identical settings, paid once and then idled. What kept the damage going was the settings moving between saves. In the same test, a chain that rotated through quality 90, 75 and 85 ended at 28.12 dB, 1.43 dB below the chain that used 75 every time, though two thirds of its saves used a higher number. My reading, again untested: a different quality means a different grid, so every save has fresh rounding to do.&lt;/p&gt;

&lt;p&gt;The browser tool I use for one-off compression is ImgIng &lt;a href="https://imging.ai/" rel="noopener noreferrer"&gt;https://imging.ai/&lt;/a&gt; , and the same check was done there with a JPEG it had exported itself: fed back in at the same setting, 316,478 bytes came out as 316,443, the page noted that the size had not gone down, and PSNR against the first pass was 52.63 dB. Little lost, nothing gained, like the Pillow chain.&lt;/p&gt;

&lt;p&gt;One extra save at the same setting no longer worries me, and a higher quality number no longer looks like insurance for a file that gets re-saved a lot, since the high-quality chains were the ones still drifting at generation 20. For any particular image the checksum loop above answers the question: where two neighbouring generations share a checksum, that image at that quality in that encoder has nothing left to lose.&lt;/p&gt;

&lt;p&gt;Photo sample: Wikimedia Commons, "2013 Cogden Bridge" by Kreuzschnabel, CC BY-SA 3.0, downscaled, cropped and repeatedly recompressed. The second photo is "Inle Lake, Myanmar" by Jakub Hałun, CC BY-SA 4.0. The screenshot sample is a self-made UI.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Keyed but still green: keying and despill are two different jobs</title>
      <dc:creator>yue xing</dc:creator>
      <pubDate>Wed, 18 Nov 2026 08:09:07 +0000</pubDate>
      <link>https://dev.to/token111/keyed-but-still-green-keying-and-despill-are-two-different-jobs-49pl</link>
      <guid>https://dev.to/token111/keyed-but-still-green-keying-and-despill-are-two-different-jobs-49pl</guid>
      <description>&lt;p&gt;The tempting fix for a green screen that is "keyed but still green" is to push the key harder. A key and a despill answer two different questions about every pixel. The key decides how transparent the pixel is, and the despill decides what colour it keeps. If you only ask the first question, the edge stays green however good the alpha is. If you push the key to fix the colour, it starts eating hair instead.&lt;/p&gt;

&lt;p&gt;The test card is synthetic, not real footage: 1280×720, a green screen at RGB(20,175,65), and ten patches including hair strands, a white shirt, light and dark skin, a yellow object and a grey veil at α=0.45. Because it was generated, the clean foreground and true alpha are known, which is the only reason anything here can be scored. The spill is not real light. It was added with a model that peaks at the outline and in semi-transparent areas, so every number here is about this card, on one Apple-silicon Mac, in one run. Colour error is ΔE76, and the new background is a cartoonish sky, grass and yellow stripe.&lt;/p&gt;

&lt;h2&gt;
  
  
  The key only decides transparency
&lt;/h2&gt;

&lt;p&gt;On this card the green lives at the edges. Hair strands composited on grey come out at (82,115,86) against a true (90,82,77), ΔE 24.0. The veil's green sits about 41 above its red and blue. The white shirt is ΔE 16.6 in a 6-pixel edge band and 7.8 inside. Edges are exactly where alpha is fractional: a pixel at α=0.45 is part shirt and part screen, and the screen's share is still baked into its colour.&lt;/p&gt;

&lt;p&gt;The key is a plain colour-difference matte, a = 1 − clip((G − max(R,B) − 20) / 70). The matte was decent, with mean absolute alpha error 0.019 over the frame and 0.192 in the edge band. The composite still had an edge-band ΔE of 23.1. More keying would not fix that, because the transparency was roughly right. The colour of those edge pixels still had the screen mixed in.&lt;/p&gt;

&lt;h2&gt;
  
  
  Despill is a separate second step
&lt;/h2&gt;

&lt;p&gt;Adding one despill line after the key, the average limit G = min(G, (R+B)/2) on the foreground only, took the edge band from 23.1 to 13.2 and the whole frame from 5.41 to 3.58. Same matte, only the colour changed.&lt;/p&gt;

&lt;p&gt;The opposite order shows it from the other side. With despill first, the screen itself got pressed to roughly (20,42,65), a dark blue-grey, and the key stopped seeing it as screen. Every alpha came out as 1, mean 1.000 against a true 0.330, the edge band jumped to ΔE 39.9, and the new grass was off by 80.7. Despill removes the colour the key is looking for, so it waits until the key is done.&lt;/p&gt;

&lt;p&gt;Despill also has a price. The average limit took the yellow object's green from 205 to 143 and its hue from 49° to 30°, so yellow turned orange. A slower version that first subtracts the screen colour, F = (C − (1−α)·screen)/α, and then despills got the edge band to 11.7, the best of the five orders compared on this card. How the remaining 11.7 splits between the key and the formula was not separated in this test.&lt;/p&gt;

&lt;h2&gt;
  
  
  An AI matte leaves the colour alone too
&lt;/h2&gt;

&lt;p&gt;For the AI side, the comparison used ImgIng &lt;a href="https://imging.ai/" rel="noopener noreferrer"&gt;https://imging.ai/&lt;/a&gt; video matting (Beta) in a desktop Chromium browser on the same Mac, on a three-second real green-screen clip, exported as transparent WebM, the only format it offers once the background is transparent. That footage is public domain, published by VOA Learning English on Wikimedia Commons, and was trimmed and re-encoded for this. On frame 45 over grey, green-leaning pixels (G more than 10 above max(R,B)) were 2.7% at the hair tips and 4.9% on the hand with its lighter model, 7.7% and 14.8% with its quality model. After one average-limit pass, each of those four read 0.0%.&lt;/p&gt;

&lt;p&gt;My reading, an inference, is that an AI matte usually produces alpha only and keeps the source colour, so the despill step still belongs after it. A plain chroma key on the same frame looked cleaner at 0.1% and 0.8%, but by thinning the hair: 14,748 semi-transparent pixels at the hair tips against 5,963 for the quality model, about 2.5 times as many. That is "push the key harder" again, paid for with the subject. The AI matte's error against true colour was not measured: the model doesn't treat the card's patches as a person, so the card can't stand in for it.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Limited range video has 220 grey levels, not 256: what one flag does to the same pixels</title>
      <dc:creator>yue xing</dc:creator>
      <pubDate>Tue, 17 Nov 2026 08:19:40 +0000</pubDate>
      <link>https://dev.to/token111/limited-range-video-has-220-grey-levels-not-256-what-one-flag-does-to-the-same-pixels-1clh</link>
      <guid>https://dev.to/token111/limited-range-video-has-220-grey-levels-not-256-what-one-flag-does-to-the-same-pixels-1clh</guid>
      <description>&lt;p&gt;Limited range is usually summed up as "luma runs from 16 to 235". That line alone doesn't say what changes in a real picture, so this run used a file whose steps can actually be counted. The grey test pattern is homemade: 1280×720, a ramp across the top with every value from 0 to 255 at 5 px each, and nine flat patches below at 0, 8, 16, 64, 128, 192, 235, 247 and 255. It was encoded as 3-second 8-bit H.264 (CRF 10, BT.709) in four versions: real range full or limited, flagged full or limited. Each one was then decoded back to RGB with ffmpeg and the centre of every patch was read. Everything ran once on one Apple-silicon Mac, so read the numbers as "this file on this machine".&lt;/p&gt;

&lt;h2&gt;
  
  
  Limited range has 220 luma codes
&lt;/h2&gt;

&lt;p&gt;Full range gives luma all 256 codes of an 8-bit byte. Limited range keeps 16 as black and 235 as white, which leaves 235 − 16 + 1 = 220 codes for the whole trip from black to white. The usual conversion squeezes each 0–255 value with roughly Y = 16 + 219 × v / 255 and rounds the result. In this file the stored codes matched that: mid grey 128 sat at 126, and white 255 at 235.&lt;/p&gt;

&lt;h2&gt;
  
  
  The ramp kept 220 of 256 steps
&lt;/h2&gt;

&lt;p&gt;In the version that was really limited and correctly tagged limited, all nine patches decoded back to exactly 0, 8, 16, 64, 128, 192, 235, 247 and 255, the same as the full-range file. Judging by the patches alone, the two files would pass as identical. The ramp disagreed. Counting distinct steps across it, the full-range file had all 256 and the limited one had 220. That is 36 steps fewer. By the arithmetic (not a separate measurement), 36 neighbouring greys must have been rounded onto codes they share with a neighbour, and a decoder has no way to split them again. So in 8-bit, a correctly handled limited range costs fine gradation, not contrast. My guess is that this shows up as banding on slow gradients, but real footage wasn't tested this time.&lt;/p&gt;

&lt;h2&gt;
  
  
  One flag decides where black sits
&lt;/h2&gt;

&lt;p&gt;Now the washed-out case. The correctly made limited file had only its range flag changed to full, without touching a single pixel. Decoded, the patches read 16, 23, 30, 71, 126, 181, 218, 228 and 235. Black came out as 16 and white as 235. The distance from black to white shrank from 255 code steps to 219, which works out to roughly 86%. That's the grey veil. Each reading lines up with the stored code from the formula above (these figures are my own arithmetic, not extra measurements): 8 becomes 22.87 and rounds to 23, 64 becomes 70.97 and rounds to 71, 192 becomes 180.9 and rounds to 181. The decoder was told the codes already covered 0–255, so it showed them as they were and skipped the stretch back.&lt;/p&gt;

&lt;p&gt;The opposite mistake is not just "less grey". Full-range pixels tagged as limited read 0, 0, 0, 56, 130, 205, 255, 255 and 255: the 17 darkest levels (0–16) all became 0, the 21 brightest (235–255) all became 255, and the midtones were stretched apart. The shadows clip to pure black, the highlights clip to pure white, and the result is the reverse of washed out. So "my video looks grey" points at only one of the two possible mislabels.&lt;/p&gt;

&lt;h2&gt;
  
  
  An MP4 stores the flag twice
&lt;/h2&gt;

&lt;p&gt;A second detail: an H.264 MP4 carries the range in the bitstream (the VUI) and again in the container (the colr box). Editing only the bitstream with the h264_metadata filter left the container saying the old thing, and ffprobe then gave two answers: -show_streams reported the container value, -show_frames the bitstream one. With two deliberately mismatched files, both ffmpeg's decoder and a desktop Chromium video element drawn onto a canvas followed the bitstream and ignored the container, and the two paths agreed within 1 on every patch. QuickTime, VLC, Safari, phone galleries and chat apps weren't tested, so whether players differ is still open.&lt;/p&gt;

&lt;h2&gt;
  
  
  A tool that converts on purpose
&lt;/h2&gt;

&lt;p&gt;For a conversion done on purpose rather than by accident, the full-range file also went through the video compressor in ImgIng &lt;a href="https://imging.ai/" rel="noopener noreferrer"&gt;https://imging.ai/&lt;/a&gt; on the same Mac in the same desktop Chromium, at the "visually near-lossless" quality level. The 3-second still pattern came out larger than it went in, so the tool didn't adopt the result by default; the candidate was saved manually. Both the H.264 and H.265 outputs came back as limited range and were tagged tv, the correct label. Decoded, black and white read 0 and 255 for H.265, and 2 and 254 for H.264. There was no 16-and-235 veil and no clipping, but full range wasn't preserved either: the pixels were converted, not passed through. One 3-second synthetic file says nothing about other footage, though.&lt;/p&gt;

&lt;h2&gt;
  
  
  What I check first now
&lt;/h2&gt;

&lt;p&gt;When a clip looks grey, I now ask two things before re-encoding anything: which range the pixels are really in, and what the bitstream flag claims. If the codes sit between 16 and 235 and the flag says full, the picture is fine and only the label needs fixing. What I still can't tell you is what phones or screen recorders write by default. There was no original phone footage on hand, only re-processed files. HEVC, 10-bit and HDR are untested as well, so all of the above is 8-bit H.264 with BT.709.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Canvas width attribute vs CSS width: how much blur a stretch adds at DPR 1, 2 and 3</title>
      <dc:creator>yue xing</dc:creator>
      <pubDate>Mon, 16 Nov 2026 08:22:04 +0000</pubDate>
      <link>https://dev.to/token111/canvas-width-attribute-vs-css-width-how-much-blur-a-stretch-adds-at-dpr-1-2-and-3-19dd</link>
      <guid>https://dev.to/token111/canvas-width-attribute-vs-css-width-how-much-blur-a-stretch-adds-at-dpr-1-2-and-3-19dd</guid>
      <description>&lt;p&gt;A canvas has two sizes, and only one of them holds pixels. The &lt;code&gt;width&lt;/code&gt; and &lt;code&gt;height&lt;/code&gt; attributes set the backing store, the bitmap that &lt;code&gt;fillText&lt;/code&gt; actually draws into. The CSS &lt;code&gt;width&lt;/code&gt; and &lt;code&gt;height&lt;/code&gt; only decide how large that bitmap is shown on the page. Give a canvas the attributes 400×100 and the CSS 800×200, and the browser takes 40,000 drawn pixels and spreads them over a box with four times the area. On a 1× screen that is a 2× upscale per side. On a 3× screen it is 6× per side. Knowing the two sizes are separate is the easy part; how much blur the stretch costs, and how that changes from one screen to the next, is what this run put a number on.&lt;/p&gt;

&lt;h2&gt;
  
  
  The test page and the edge metric
&lt;/h2&gt;

&lt;p&gt;The text sample is homemade: the line 周末市集 Canvas 11月22日 (a weekend market notice mixing Chinese, Latin letters and digits), bold 28px, black on white, in the system PingFang SC font, drawn into a canvas with a logical size of 400×100. One version of the page keeps the attributes at 400×100 and sets the CSS to 800×200. The other sets the attributes to CSS size × devicePixelRatio, so 800×200, 1600×400 and 2400×600, and scales the context to match. Both pages were rendered at DPR 1, 2 and 3 in desktop Chromium on one Apple-silicon Mac, and the canvas element was captured at device resolution. Each configuration ran once.&lt;/p&gt;

&lt;p&gt;Blur needs a number, and the one used here is homemade too. Edge transition width is the average length, row by row, of runs of pixels whose gray value sits between 32 and 223, so a crisp edge gives a short run and a smeared edge gives a long one. Error is the mean absolute gray difference, on a 0 to 255 scale, against a reference drawn at 8× and averaged down. For scale, that reference measured 1.768 px of edge at 800×200.&lt;/p&gt;

&lt;h2&gt;
  
  
  Stretched edges got wider at every DPR step
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;DPR&lt;/th&gt;
&lt;th&gt;Attributes 400×100, CSS 800×200&lt;/th&gt;
&lt;th&gt;Backing store = CSS size × DPR&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;captured 800×200, edge 5.99 px, error 4.758&lt;/td&gt;
&lt;td&gt;800×200, edge 1.641 px, error 1.603&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;captured 1600×400, edge 11.262 px, error 5.251&lt;/td&gt;
&lt;td&gt;1600×400, edge 1.873 px, error 1.078&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;captured 2400×600, edge 16.756 px, error 5.413&lt;/td&gt;
&lt;td&gt;2400×600, edge 1.639 px, error 1.01&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Edges are counted in device pixels. The right column barely moves: 1.6 to 1.9 at every ratio, sitting around the 1.768 reference. The left column climbs with every step, and the stretched edge works out to 3.65 times the matched one at DPR 1, 6.01 times at DPR 2 and 10.22 times at DPR 3. The DPR 1 row deserves a second look, because nothing about the screen is high-density there. All the blur in that row comes from the attribute being half the CSS size. So there are two multipliers stacked on top of each other, CSS size over attribute size and then the device pixel ratio, and the backing store has to cover both.&lt;/p&gt;

&lt;h2&gt;
  
  
  The rasterizer was not the problem
&lt;/h2&gt;

&lt;p&gt;The other suspect is &lt;code&gt;fillText&lt;/code&gt; antialiasing itself getting worse on high-DPI screens. This run doesn't support that. On the export side of the same test, a canvas with a fixed pixel size gave pixel-identical files at DPR 1, 2 and 3, so the drawn text did not depend on the screen here. What a denser screen changes is how far the browser has to stretch a bitmap that was already too small, which is why the left column looks like a rendering problem when it is a sizing one.&lt;/p&gt;

&lt;p&gt;The display fix is the familiar one. Below is a sketch of the idea, not the measured page itself:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;card&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;querySelector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'#notice'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;     &lt;span class="c1"&gt;// the visible canvas&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;box&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;card&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getBoundingClientRect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;           &lt;span class="c1"&gt;// CSS size, 800×200 here&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;devicePixelRatio&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;card&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;width&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;round&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;box&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;width&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;              &lt;span class="c1"&gt;// bitmap = CSS size × DPR&lt;/span&gt;
&lt;span class="n"&gt;card&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;height&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;round&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;box&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;height&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;card&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getContext&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'2d'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;scale&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;                   &lt;span class="c1"&gt;// keep drawing in CSS units&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That handles what the page shows. A downloaded file is a different question, since the same multiplication at DPR 1 hands back a 1× bitmap, and an export probably wants its size from the target instead of from whoever's screen happens to run it. One browser editor that writes this split down is ImgIng &lt;a href="https://imging.ai/" rel="noopener noreferrer"&gt;https://imging.ai/&lt;/a&gt; , whose site says the preview renders at an adaptive resolution and the export is recomposed at the original canvas size. How it does that internally, I don't know.&lt;/p&gt;

&lt;h2&gt;
  
  
  What this run did not cover
&lt;/h2&gt;

&lt;p&gt;One thing I still can't explain is the matched DPR 2 row. Its edge is 1.873 px while DPR 1 and DPR 3 both land near 1.64, and its error, 1.078, is between the other two, so it isn't simply worse. The cause wasn't looked into. Beyond that, only Chromium was measured. Safari, Firefox and Windows ClearType were not tested, and neither were &lt;code&gt;imageSmoothingQuality&lt;/code&gt;, &lt;code&gt;OffscreenCanvas&lt;/code&gt; or the &lt;code&gt;scale&lt;/code&gt; option in html2canvas, so anything said about them would be a guess. It is also one string, one machine, one run per setting, with a metric defined just for this test.&lt;/p&gt;

&lt;p&gt;If canvas text on a page looks soft, the quickest check is in the console: compare &lt;code&gt;canvas.width&lt;/code&gt; with &lt;code&gt;getBoundingClientRect().width * devicePixelRatio&lt;/code&gt;. When the two numbers differ, the bitmap is being stretched, and in this run that stretch alone made edges between 3.65 and 10.22 times wider than the matched version.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
    </item>
    <item>
      <title>PNG vs JPEG for PDF pages: the chart page agreed, the text page didn't</title>
      <dc:creator>yue xing</dc:creator>
      <pubDate>Sun, 15 Nov 2026 08:09:24 +0000</pubDate>
      <link>https://dev.to/token111/png-vs-jpeg-for-pdf-pages-the-chart-page-agreed-the-text-page-didnt-591k</link>
      <guid>https://dev.to/token111/png-vs-jpeg-for-pdf-pages-the-chart-page-agreed-the-text-page-didnt-591k</guid>
      <description>&lt;p&gt;"Save text as PNG, it's smaller than JPEG" is a rule of thumb for storing PDF pages as images, whether that's lecture slides for revision notes or a paper's figures. On a three-page sample, it held for the chart page only. That page, with flat colours and thin lines, came out at 127,051 bytes as PNG against 178,232 bytes as JPEG at quality 75. The dense small-text page went the other way: 838,404 bytes as PNG, about 5% larger than JPEG at quality 92 (796,608 bytes). Lossless WebP was the smallest file on both of those pages.&lt;/p&gt;

&lt;p&gt;The sample is one 3-page A4 PDF. Page 1 has Chinese body text in 10.5, 9, 7.5, 6 and 5 pt, two small English lines and a table drawn with 0.3 pt rules. Page 2 has vector bar, line and pie charts labelled at 6 to 7.5 pt. Page 3 has a large photo, a cropped detail and some 8 pt text. Text and charts were made for the test; only the page-3 photograph is borrowed, a CC0 image by Bonnie Moreland from Wikimedia Commons. Every page went through poppler at 216 DPI onto a white canvas (1786×2526 pixels), and Pillow and pngquant then saved the exact same pixels in several formats, a single pass per setting on one Apple-silicon Mac. No scans, handwriting or print-ready files are in it, so every number below means "on this sample".&lt;/p&gt;

&lt;h2&gt;
  
  
  The bytes for each page
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Format&lt;/th&gt;
&lt;th&gt;Text page&lt;/th&gt;
&lt;th&gt;Chart page&lt;/th&gt;
&lt;th&gt;Photo page&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;PNG, truecolour&lt;/td&gt;
&lt;td&gt;838,404&lt;/td&gt;
&lt;td&gt;127,051&lt;/td&gt;
&lt;td&gt;2,431,138&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;JPEG q75&lt;/td&gt;
&lt;td&gt;535,729 (37.86 dB)&lt;/td&gt;
&lt;td&gt;178,232 (38.80 dB)&lt;/td&gt;
&lt;td&gt;302,318 (40.22 dB)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;JPEG q85&lt;/td&gt;
&lt;td&gt;647,884 (41.54 dB)&lt;/td&gt;
&lt;td&gt;201,943 (39.73 dB)&lt;/td&gt;
&lt;td&gt;395,240 (42.42 dB)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;JPEG q92&lt;/td&gt;
&lt;td&gt;796,608 (45.92 dB)&lt;/td&gt;
&lt;td&gt;233,599 (40.21 dB)&lt;/td&gt;
&lt;td&gt;550,440 (45.02 dB)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;WebP lossless&lt;/td&gt;
&lt;td&gt;161,212&lt;/td&gt;
&lt;td&gt;37,390&lt;/td&gt;
&lt;td&gt;1,612,642&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;PNG, 256-colour palette&lt;/td&gt;
&lt;td&gt;359,391 (66.86 dB)&lt;/td&gt;
&lt;td&gt;49,019 (68.63 dB)&lt;/td&gt;
&lt;td&gt;1,105,849 (45.88 dB)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The figures in brackets are PSNR against the original render; the lossless rows have none because they match it exactly. On the chart page PNG wins outright: about 71% of the JPEG q75 size and about 54% of q92, while that q92 JPEG only reaches 40.21 dB. The text page flips it. PNG is 1.56 times the size of JPEG q75 there, and even against q92 it's the bigger file by 41,796 bytes.&lt;/p&gt;

&lt;p&gt;Lossless WebP stands out. On the text page it's 161,212 bytes, roughly a fifth of the PNG, and on the chart page 37,390 bytes, under 30% of the PNG. It also comes in under the 256-colour palette PNG on both pages, and that palette version is lossy. The photo page has no surprise: PNG there is 8.0 times the size of JPEG q75, and lossless WebP (1,612,642 bytes) loses to every JPEG level as well.&lt;/p&gt;

&lt;h2&gt;
  
  
  Both pages are about 80% white
&lt;/h2&gt;

&lt;p&gt;"The chart page has more white space" doesn't explain it. Counted on a quarter-size copy, the text page is 81% white and the chart page 82%. The chart page even has more distinct colours, about 2117 against about 447. So neither "more background" nor "fewer colours" says which page PNG wins.&lt;/p&gt;

&lt;p&gt;What does differ is how the non-white pixels are spread out. Cut each page into the 8×8 blocks JPEG works on, and 93.0% of the chart page's blocks are a single flat colour, against 75.5% on the text page. So about a quarter of the text page's blocks contain some edge, compared with 7.0% on the chart page. My reading, and it's only a reading, is that PNG has to keep every one of those edge pixels exactly while JPEG q92 is allowed to approximate them, so the more scattered edges a page has, the more the lossless format pays. Nothing beyond these three pages backs that up, and I honestly don't understand yet why lossless WebP copes with the same edges so much better than PNG.&lt;/p&gt;

&lt;h2&gt;
  
  
  One browser export
&lt;/h2&gt;

&lt;p&gt;Changing the export tool changed the picture. ImgIng at &lt;a href="https://imging.ai/" rel="noopener noreferrer"&gt;https://imging.ai/&lt;/a&gt; works in a desktop browser with three DPI choices (144, 216, 300; default 216), and during this comparison no request carrying the file showed up while it processed the PDF. At 216 DPI its text page came out at 881,244 bytes as PNG against 818,045 as JPEG, and its chart page at 228,983 as PNG against 194,664 as JPEG. With that export, PNG was the bigger file on the chart page too, so the encoder behind a tool is a variable of its own.&lt;/p&gt;

&lt;h2&gt;
  
  
  What I'd take from this for notes
&lt;/h2&gt;

&lt;p&gt;For slides that are mostly charts, diagrams and flat colour, PNG was the smaller lossless choice on this sample, and lossless WebP was smaller again. For dense text pages lossless WebP was the clear winner, but only if wherever the images end up accepts WebP; no note app was checked for that. Failing that, JPEG q92 was not bigger than PNG on the text page. Photos stay JPEG.&lt;/p&gt;

&lt;p&gt;The cheapest check is on your own pages: render one page of each kind at the DPI you need, save the same pixels as PNG, JPEG q92 and lossless WebP, and compare the byte counts. Then zoom in on the smallest text and make sure the letters still look like the original, whatever tool or format you end up with.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
