原 使用Python代码将图片背景修改为透明背景
代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | cat > /tmp/a.py <<"EOF" from PIL import Image # 打开图片 image = Image.open("input_image.png") # 确保图片有透明通道 image = image.convert("RGBA") # 获取数据 data = image.getdata() # 将白色背景设为透明 new_data = [] for item in data: if item[:3] == (255, 255, 255): # 判断是否为白色 new_data.append((255, 255, 255, 0)) # 设为透明 else: new_data.append(item) # 更新图片数据 image.putdata(new_data) # 保存为 PNG image.save("output_image.png", "PNG") EOF pip3 install pillow python3 a.py |


