简体   繁体   English

如何使用javascript去除黑色区域或黑色区域到透明png图像?

[英]How to remove black area or black area to transparent png image using javascript?

How to remove black area or black area to transparent png image using javascript?如何使用javascript去除黑色区域或黑色区域到透明png图像?

在此处输入图像描述

I want remove black area or black area to transparent i used 'sharp' Library but I couldn't get what I want if you need more information what you need plz tell me我想删除黑色区域或黑色区域以透明我使用了“锐利”库但是如果您需要更多信息我无法得到我想要的东西你需要什么请告诉我

I have tried to do something similar in the past, but after checking out several resources, I realized that removing backgrounds from images is not possible solely with Sharp.我过去曾尝试做类似的事情,但在查看了几个资源后,我意识到仅使用 Sharp 无法从图像中删除背景。 You might have to use another package too, for instance, Rembg.您可能还必须使用另一个 package,例如 Rembg。 This is how you can do it:你可以这样做:

// Import the sharp and rembg library for image processing
const { Rembg } = require("rembg-node");
const sharp = require("sharp");

(async () => {
  const input = sharp("Path/to/your/image.png");  // Load the input image using Sharp

  // Create a new instance of the Rembg class (optional arguments can be provided)
  const rembg = new Rembg();

  // Apply background removal by calling the remove() method of the Rembg instance
  const output = await rembg.remove(input);

  await output.toFile("path/to/your/destination.png");  // Save the output image with the background removed

})();

Note: Rembg is not a very popular package, so you will need to use yarn for its installation.注意:Rembg 不是很流行的 package,所以你需要使用 yarn 来安装它。

Another easy solution I found is the freemium Cloudinary AI background removal service.我发现的另一个简单的解决方案是免费增值 Cloudinary AI 后台删除服务。 I played around with it a bit, and it was pretty consistent in removing backgrounds.我试了一下,它在删除背景方面非常一致。 For this, install Cloudinary Node.js SDK in your project.为此,在您的项目中安装 Cloudinary Node.js SDK Enable the plugin by going to Add-ons>Cloudinary AI Background Removal and subscribing to the free plan.通过转到附加组件>Cloudinary AI 背景删除并订阅免费计划来启用插件。 Then run this simple script in Node.js:然后在 Node.js 中运行这个简单的脚本:

// Import cloudinary
const cloudinary = require('cloudinary').v2;

// Configure cloudinary with your credentials
cloudinary.config({
  cloud_name: 'your_cloud_name',
  api_key: 'your_api_key',
  api_secret: 'your_api_secret'
});

// Specify the public ID for the image you want to upload
const publicId = "Your_Image_ID";

// Upload the image to Cloudinary with specified options
cloudinary.uploader.upload("Path/to/your/image.png", {
  resource_type: "image", // Specify the type of resource being uploaded
  public_id: publicId, // Set the public ID for the uploaded image
  background_removal: "cloudinary_ai" // Use background removal plugin
})
.then((result) => {
  console.log("success", JSON.stringify(result, null, 2)); // Log the success response
})
.catch((error) => {
  console.log("error", JSON.stringify(error, null, 2)); // Log the error response
});

Hope this helps you out.希望这可以帮助你。

PS.附言。 Referred to this blog post , in case it helps.参考这篇博文,以防有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM