Photoroom Background Removal Integration

I am sure many people would like this process added, so we can remove backgrounds automatically from images that are uploaded (great for ecommerce sites).
Photoroom already have the API available, but it is way too complicated to get this working in Wappler for an average person.

Would be great if the team could add something in to Wappler to enable us just to add the API key and everything is ready to go.

I have a custom module I paid someone to create I can post tomorrow.

I banged my head against the wall for many many weeks so I feel your pain

If you could that would be awesome. Yes its defiantly painful lol

    const Axios = require('axios'); // you will need to install this
    const { toSystemPath } = require('../../../lib/core/path');
    const fs = require('fs')

    exports.removeBg = async function (options) {

        if (!options.path) {    // hoping i am right
            throw 'Image path is required';
        }
        const srcImgPath = options.path;  //image path on the server

        const srcImgName = this.parse(options.img_name);

        const destinationImagePath = `uploads/background_removed/${srcImgName}`;    //where new image will be saved


        var dir = 'uploads/background_removed'; // checking if directory exists, otherwise create new
        if (!fs.existsSync(dir)) {
            fs.mkdirSync(dir);
        }

        let path = toSystemPath(this.parseRequired(this.parse(options.path), 'string', 'fs.exists: path is required.'))
        const contents = fs.readFileSync(path, { encoding: 'base64' });

        const response = await Axios({
            url: "https://sdk.photoroom.com/v1/segment",
            method: 'POST',
            responseType: 'stream',
            data: { image_file_b64: contents },
            headers: { "x-api-key": "XXXXXXXXXXXXXXX" },
        });

        response.data.pipe(fs.createWriteStream(destinationImagePath))
            .on('error', (err) => { return err })
            .once('close', () => { return true; });

        function getRandom(length) {
            var result = '';
            var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
            var charactersLength = characters.length;
            for (var i = 0; i < length; i++) {
                result += characters.charAt(Math.floor(Math.random() * charactersLength));
            }
            return result;
        }
    }
  {
    type: 'removeBackground_removeBg',
    module : 'removeBackground',
    action : 'removeBg',
    groupTitle : 'My Modules',
    groupIcon : 'fas fa-lg fa-project-diagram comp-images',
    title : 'Remove Background',
    icon : 'fas fa-lg fa-file-alt comp-images',
    dataPickObject: true,
    properties : [
      {
        group: 'Image Path',
        variables: [
          { name: 'path', optionName: 'path', title: 'Path', type: 'text', required: true, defaultValue: '', serverDataBindings: true}
          { name: 'name', optionName: 'img_name', title: 'File Name', type: 'text', required: true, defaultValue: '', serverDataBindings: true, allowPaste: true}
        ]
      }
    ]
  }

Named removeBackround.js and removeBackground.hjson

1 Like