How to Create a Picture Background Remover PWA Using VueJs

How to Create a Picture Background Remover PWA Using VueJs
Photo by Natali Hordiiuk / Unsplash

Vue.js is a progressive framework for building user interfaces. It is designed to be simple and easy to use. The remove.bg API is a simple API that allows you to remove the background from an image. In this tutorial, we will show you how to use the remove.bg API to create a picture background remover Progressive Web App (PWA).

We will be using the Vue CLI to generate a new Vue.js project. If you don't have the Vue CLI installed, you can install it by running the following command:

npm install -g @vue/cli

Once the Vue CLI is installed, we can create a new Vue.js project by running the following command:

vue create picture-background-remover-pwa

This will create a new directory called picture-background-remover-pwa. In this directory, there will be a file called src/App.vue. This is the main Vue.js component for our app. We will be adding our code to this file.

The first thing we need to do is add the remove.bg API key to our project. You can sign up for a free API key at https://www.remove.bg/api. Once you have signed up, you will be given an API key. Add this API key to the src/App.vue file as follows:

<script>
export default {
  name: 'app',
  data() {
    return {
      apiKey: 'your-api-key-here'
    }
  }
}
</script>

Next, we need to add the HTML for our app. Add the following to the src/App.vue file:

<template>
  <div id="app">
    <div class="container">
      <div class="row">
        <div class="col-md-6 offset-md-3">
          <h1>Picture Background Remover</h1>
          <p>
            This app uses the <a href="<https://www.remove.bg/api>">remove.bg API</a> to remove the background from an image.
          </p>
          <div class="form-group">
            <label for="image-url">Image URL</label>
            <input type="text" class="form-control" id="image-url" placeholder="Enter image URL">
          </div>
          <button class="btn btn-primary" @click="removeBackground">Remove Background</button>
          <div class="output">
            <div class="image-container">
              <img :src="outputImage" alt="Output image">
            </div>
            <div class="progress">
              <div class="progress-bar progress-bar-striped progress-bar-animated" role="progressbar" :aria-valuenow="progress" :aria-valuemin="0" :aria-valuemax="100" :style="{ width: progress + '%' }"></div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

In the HTML, we have a form with an input for the image URL and a button to remove the background. We also have a div for the output image. The output image will be displayed in this div. We also have a progress bar to show the progress of the background removal.

Next, we need to add the CSS for our app. Add the following to the src/App.vue file:

<style>
#app {
  font-family: 'Avenir', Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}

.output {
  margin-top: 30px;
}

.image-container {
  text-align: center;
}

.image-container img {
  max-width: 100%;
}

.progress {
  height: 20px;
  margin-top: 30px;
}
</style>

Next, we need to add the JavaScript for our app. Add the following to the src/App.vue file:

<script>
export default {
  name: 'app',
  data() {
    return {
      apiKey: 'your-api-key-here',
      outputImage: '',
      progress: 0
    }
  },
  methods: {
    removeBackground() {
      const imageUrl = document.getElementById('image-url').value;

      if (!imageUrl) {
        return;
      }

      this.outputImage = '';
      this.progress = 0;

      fetch('<https://api.remove.bg/v1.0/removebg>', {
        method: 'POST',
        body: JSON.stringify({
          image_url: imageUrl,
          size: 'auto',
          output_format: 'png',
          api_key: this.apiKey
        }),
        headers: {
          'Content-Type': 'application/json'
        }
      })
      .then(response => {
        if (!response.ok) {
          throw new Error('Error');
        }

        return response.json();
      })
      .then(data => {
        if (!data.success) {
          throw new Error('Error');
        }

        this.outputImage = data.output_url;
      })
      .catch(error => {
        console.error(error);
      });
    }
  }
}
</script>

In the JavaScript, we have a method called removeBackground. This method is called when the button is clicked. This method will fetch the image from the URL specified in the input field. It will then call the remove.bg API to remove the background from the image. The output image will be displayed in the output div. We are also using the progress bar to show the progress of the background removal.

Finally, we need to build our app for production. We can do this by running the following command:

npm run build

This will build our app and put the built files in the dist directory. We can then deploy the built files to a web server you can purchase from any hosting service.

That's it! You should now have a working picture background remover PWA.

Subscribe to The Poor Coder | Algorithm Solutions

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
[email protected]
Subscribe