图片移除背景
是什么:将图片中的主体与背景分离,生成透明背景图片。
为什么:用于产品展示、证件照、设计素材等。
Python 示例
python
from rembg import remove
from PIL import Image
import io
def remove_background(input_path, output_path="no_bg.png"):
"""移除图片背景"""
# 读取图片
with open(input_path, 'rb') as input_file:
input_image = input_file.read()
# 移除背景
output_image = remove(input_image)
# 保存结果
with open(output_path, 'wb') as output_file:
output_file.write(output_image)
print(f"背景移除完成: {output_path}")
# 使用示例
remove_background("photo.jpg", "no_background.png")
安装依赖:
bash
pip install rembg pillow
一句话总结:使用rembg库一键移除图片背景,生成带透明通道的PNG图片,适合电商和设计用途。