大家想不想使用python完成批量图片压logo的操作,今天我就来分享一下压图片的教程,喜欢的同学,一键三连呦下面的分享给大家的代码
from PIL import Imageclass PressurePic:def __init__(self):passdef remove_logo_black_background(self, image_path, output_path):image = Image.open(image_path).convert("RGBA")width, height = image.size# 创建一个透明层,用于保存去除黑色背景后的图像transparent_image = Image.new("RGBA", (width, height))# 遍历每个像素点,如果不是黑色则将其复制到透明层上for x in range(width):for y in range(height):pixel = image.getpixel((x, y))if pixel[0] > 10 or pixel[1] > 10 or pixel[2] > 10:transparent_image.putpixel((x, y), pixel)# 保存结果图片transparent_image.save(output_path)def pressure_logo(self, background_path, logo_path, output_path, position):background = Image.open(background_path).convert("RGBA")logo = Image.open(logo_path).convert("RGBA")# 根据指定的位置计算 logo 的粘贴坐标x, y = positionwidth, height = logo.sizepaste_coords = (x, y, x + width, y + height)# 将 logo 图片粘贴到背景图片上background.paste(logo, paste_coords, logo)# 保存结果图片background.save(output_path)if __name__ == '__main__':press_obj = PressurePic()# press_obj.remove_logo_black_background("d:\\腾视界-角标.png", "d:\\腾视界white-角标.png")press_obj.pressure_logo("d:\\1.png", "d:\\腾视界white-角标.png", "d:\\2.png", position = (650, 40))