wolfyflutter

removal.ai [ai] [background removal]

Jun 12th, 2025
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 4.06 KB | Source Code | 0 0
  1. /*
  2. base        : https://removal.ai/
  3. by          : wolep
  4. nama        : ai background removal
  5. fungsi      : buat remove background (iya iyalah)
  6. last update : 13 Juli 2025, 8:30 WITA
  7. note        : lu cuma perlu buffer image input, no denpendency install,
  8.               panggil function removal.removeBackground terus masukin
  9.               buffer image lu disana, return nya lu cuma di kasih resolusi
  10.               preview, kalau mau full resolusi lu musit bayar di situs nya :v
  11.               btw liat cara pakai di bawah :v
  12.  
  13. */
  14.  
  15. const removal = {
  16.     // fetch boilerplate lol
  17.     _hit: async (url, fetchName = "lu lupa isi fetch name", returnType = "text", opts = {}) => {
  18.         const r = await fetch(url, opts)
  19.         if (!r.ok) throw Error(`fetch fail\n${r.status} ${r.statusText}\nat: ${fetchName}\nnih text nya: ${await r.text() || null}`)
  20.         try {
  21.             if (returnType == "json") return await r.json()
  22.             return await r.text()
  23.         } catch (err) {
  24.             throw Error(`fetch berhasil tapi gagal convert ke json\n${err.message}\nat: ${fetchName}\nnih text nya :${await r.text() || null}`)
  25.         }
  26.     },
  27.  
  28.     // function buat bikin formdata gw hardcode >:v
  29.     _formData: (imageBuffer) => {
  30.         const randomBoundary = "----WebKitFormBoundary" + Math.random().toString(32).slice(2)
  31.         const buffers = []
  32.         buffers.push(Buffer.from("--" + randomBoundary + "\r\nContent-Disposition: form-data; name=\"image_file\"; filename=\"Untitled-1_magicstudio_" + Math.random().toString(32).slice(2) + ".png\"\r\nContent-Type: image/png\r\n\r\n"))
  33.         buffers.push(imageBuffer)
  34.         buffers.push(Buffer.from("\r\n--" + randomBoundary + "--\r\n"))
  35.         const body = Buffer.concat(buffers)
  36.         const formDataHeaders = { "content-type": "multipart/form-data; boundary=" + randomBoundary }
  37.         return { formDataHeaders, body }
  38.     },
  39.  
  40.     // function buat dapetin web token, kalau berhasil bakalan return string kayak b42622b7fe912a89fd27646476fe4eab
  41.     getWebToken: async () => {
  42.         // fetch home page
  43.         const html = await removal._hit("https://removal.ai/", "hit homepage")
  44.         const match = html.match(/var ajax_upload_object = (.*?);/)?.[1]
  45.         if (!match) throw Error(`tidak menemukan match pada homepage`)
  46.         const { webtoken_url, security } = JSON.parse(match)
  47.  
  48.         // second fetch to get Web-Token
  49.         const webTokenUrl = `${webtoken_url}?action=ajax_get_webtoken&security=${security}`
  50.         const json = await removal._hit(webTokenUrl, "mendapatkan web token", "json")
  51.         const webToken = json?.data?.webtoken
  52.         if (!webToken) throw Error(`berhasil hit url web token tapi gak ada token nya`)
  53.         return webToken
  54.     },
  55.  
  56.     // main function
  57.     removeBackground: async (imageBuffer) => {
  58.  
  59.         const { formDataHeaders, body } = removal._formData(imageBuffer)
  60.        
  61.         const headers = {
  62.             "web-token": await removal.getWebToken(),
  63.             ...formDataHeaders
  64.         }
  65.  
  66.         const opts = {
  67.             headers,
  68.             body,
  69.             "method": "POST"
  70.         }
  71.  
  72.         const result = await removal._hit("https://api.removal.ai/3.0/remove", "remove background", "json", opts)
  73.         return result
  74.     }
  75. }
  76.  
  77. // cara pakai
  78. const buffer = require("fs").readFileSync("./gambar2.jpg")
  79. removal.removeBackground(buffer)
  80.     .then(console.log)
  81.     .catch(err => console.log(err.message))
  82.  
  83. /* output
  84. {
  85.   status: 200,
  86.   demo: null,
  87.   base64: null,
  88.   original_width: 1059,
  89.   original_height: 1200,
  90.   preview_width: 529,
  91.   preview_height: 600,
  92.   extra: null,
  93.   url: 'https://api.removal.ai/download/g1/preview/5e0c8353-c9e0-46fe-a07d-75044d30ecec.png',
  94.   high_resolution: null,
  95.   low_resolution: 'https://api.removal.ai/download/g1/preview/5e0c8353-c9e0-46fe-a07d-75044d30ecec.png',
  96.   preview_demo: 'https://api.removal.ai/download/g1/preview/5e0c8353-c9e0-46fe-a07d-75044d30ecec.png',
  97.   original: 'https://api.removal.ai/uploads/guest/182.253.149.185/d62842dd-57a0-4ff5-a085-21630ea7f1f0-untitled-1_magicstudio_03ogk93tkc.png'
  98. }
  99. */
Add Comment
Please, Sign In to add comment