今天看到一个比较有意思的工具,可以瞬间换背景。
之前,要是想换背景,第一个想到的就是PS,还要抠图。对于新手小白来说真是有些措手不及,心想应该有现成的工具能做的吧?
最开始,是在学python的时候,有个能换证件照颜色的例子。
# encoding=utf-8
from PIL import Image
from removebg import RemoveBg
# removebg涉及到api_key,需要到其官网申请
api_key = 'tNB9HjNctFjKQeWbXXXXXXX'
def change_bgcolor(file_in, file_out, api_key, color):
''' #必须为png格式 '''
p, s = file_in.split(".")
rmbg = RemoveBg(api_key, 'error.log')
rmbg.remove_background_from_img_file(file_in)
file_no_bg = "{}.{}_no_bg.{}".format(p, s, s)
no_bg_image = Image.open(file_no_bg)
x, y = no_bg_image.size
new_image = Image.new('RGBA', no_bg_image.size, color=color)
new_image.paste(no_bg_image, (0, 0, x, y), no_bg_image)
new_image.save(file_out) # 修改照片尺寸
def change_size(file_in, file_out, width, height):
image = Image.open(file_in)
resized_image = image.resize((width, height), Image.ANTIALIAS)
resized_image.save(file_out)
if __name__ == "__main__":
file_in = 'C:UsersjlPicturesSaved Pictures1.png'
file_out = 'C:UsersjlPicturesSaved Picturesout.png' # 尺寸可按需求自修改 #
change_size(file_in, file_out, 100, 100) # 换背景色
color = (255, 0, 0) #rgb(255,0,0)红色 rgb(0,0,255) 蓝色
change_bgcolor(file_in, file_out, api_key, color)
因为removebg涉及到api_key,需要到其官网申请
然后去官网申请完后,看到就有现成的功能
先上传自己的照片上传完后就是去掉背景的照片了然后去选择自己喜欢的背景最后下载就可以了
好了,到这里大家下载就可以了。喜欢的可以留个关注,谢谢![微笑]
最新评论