<?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: Share Space</title>
    <description>The latest articles on DEV Community by Share Space (@share_space_9abba2c73dce6).</description>
    <link>https://dev.to/share_space_9abba2c73dce6</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%2F4245168%2F43cbc478-31a8-4fcf-b6b8-d0bb4194bc3b.png</url>
      <title>DEV Community: Share Space</title>
      <link>https://dev.to/share_space_9abba2c73dce6</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/share_space_9abba2c73dce6"/>
    <language>en</language>
    <item>
      <title>Removing image backgrounds fully in the browser with ONNX Runtime Web: models, licences, and what bit me</title>
      <dc:creator>Share Space</dc:creator>
      <pubDate>Sat, 14 Nov 2026 22:52:56 +0000</pubDate>
      <link>https://dev.to/share_space_9abba2c73dce6/removing-image-backgrounds-fully-in-the-browser-with-onnx-runtime-web-models-licences-and-what-3kb9</link>
      <guid>https://dev.to/share_space_9abba2c73dce6/removing-image-backgrounds-fully-in-the-browser-with-onnx-runtime-web-models-licences-and-what-3kb9</guid>
      <description>&lt;p&gt;I'm building &lt;a href="https://hdresizer.com" rel="noopener noreferrer"&gt;HD Resizer&lt;/a&gt;, a free set of image tools that run entirely in the browser: resize, compress, convert, PDF, background removal and &lt;br&gt;
  a few more. Your photos never leave your device. This post is about the background remover, because that's where I learned the most.                                 &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1&lt;/strong&gt;: pick a model you're allowed to use                                                                                                                        &lt;/p&gt;

&lt;p&gt;Most "client-side background removal" libraries I found came with a catch:                                                                                           &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;@imgly/background-removal&lt;/code&gt; is AGPL-3.0.
&lt;/li&gt;
&lt;li&gt;Bria RMBG-2.0 is non-commercial.
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I want to be able to monetize the site later, so I used two Apache-2.0 models instead:                                                                               &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Best mode&lt;/strong&gt; (products, pets, general photos): IS-Net (DIS "general use"), int8-quantised, &lt;strong&gt;44 MB&lt;/strong&gt;.
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;People mode&lt;/strong&gt; (portraits): MODNet, &lt;strong&gt;26 MB&lt;/strong&gt;.
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you ship something similar, check the licence of the &lt;em&gt;weights&lt;/em&gt; and the &lt;em&gt;upstream project&lt;/em&gt;, not only the code that loads them.                                     &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2&lt;/strong&gt;: run it with ONNX Runtime Web                                                                                                                              &lt;/p&gt;

&lt;p&gt;I use &lt;code&gt;onnxruntime-web&lt;/code&gt; with the WebAssembly backend. It runs single-threaded, because multi-threaded WASM needs cross-origin isolation headers (COOP/COEP), and&lt;br&gt;&lt;br&gt;
  those can break embeds and third-party scripts. I didn't want to pay that price.                                                                                     &lt;/p&gt;

&lt;p&gt;The models are plain static files on my own hosting, and they are loaded only when someone first uses the tool. A SHA-256 check in the build script makes sure the&lt;br&gt;&lt;br&gt;
  files I deploy are the ones I tested. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 3&lt;/strong&gt;: pre- and post-processing                                                                                                                                  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;IS-Net:&lt;/strong&gt; the photo is resized to 1024×1024 (that's how the model was trained), normalised, and run through the model. The output is rescaled to the range 0 to
&lt;/li&gt;
&lt;li&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;MODNet:&lt;/strong&gt; shorter side 512, both sides rounded to a multiple of 32, long side capped at 1024.
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Applying the mask:&lt;/strong&gt; I don't cut the small image. I scale the mask up and apply it to the &lt;em&gt;full-resolution original&lt;/em&gt; with &lt;code&gt;globalCompositeOperation =            
'destination-in'&lt;/code&gt;. So a 12 MP photo gives a 12 MP cutout, even though the model only saw 1024 px.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;  &lt;span class="n"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;drawImage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;original&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;                                                                                                                                       
  &lt;span class="n"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;globalCompositeOperation&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'destination-in'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;                                                                                                                     
  &lt;span class="n"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;imageSmoothingQuality&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'high'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;                                                                                                                                  
  &lt;span class="n"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;drawImage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;mask&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;mask&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;width&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;mask&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;height&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;original&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;width&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;original&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;height&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;                                                                           
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;What bit me&lt;/strong&gt;                                                                                                                                                      &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Download size is the real UX cost.&lt;/strong&gt; 44 MB is fine on Wi-Fi and painful on mobile data. I show progress, and offer the smaller 26 MB person-only mode for
portraits.
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Hair and fur.&lt;/strong&gt; Every automatic model struggles with fine edges. I added an Erase and Restore brush that works on a full-size copy of the cutout. It fixed most
of the complaints.
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;canvas.toBlob&lt;/code&gt; on Chrome for Android&lt;/strong&gt; sometimes waited about 4 seconds before returning in my tests. Using &lt;code&gt;toDataURL&lt;/code&gt; there was much faster. I only measured
this on my own devices, so test yours.
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Memory on phones.&lt;/strong&gt; Big photos plus a model plus a canvas can run a phone out of memory. Keeping the model input small and doing the full-size work in one step
helped.
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;What I'd do differently&lt;/strong&gt;                                                                                                                                           &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Try WebGPU as an optional faster path, with WASM as the fallback.
&lt;/li&gt;
&lt;li&gt;Look at smaller distilled models for low-end phones.
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;** Try it**                                                                                                                                                            &lt;/p&gt;

&lt;p&gt;It's free, with no sign-up and no watermark: &lt;a href="https://hdresizer.com/remove-background" rel="noopener noreferrer"&gt;hdresizer.com/remove-background&lt;/a&gt;.                                             &lt;/p&gt;

&lt;p&gt;I'd like to hear how it does on your photos, especially hard ones (hair, glass, shadows), and whether you've found a good permissively licensed model I missed.   &lt;/p&gt;

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