裁剪掉额外的透明像素后无法保存 PIL 图像文件

Can't save a PIL image file after cropping out extra transparent pixels

我有这个功能,可以获取 svg 徽标,将它们转换为 png(第一个 for 循环),删除多余的冗余透明像素并将其保存到目标文件夹(第二个 for 循环)。

def convert_to_png(source_path, destination_path, output_w, output_h):
    if not os.path.exists(destination_path):
        os.mkdir(destination_path)

    # Turns SVG images to to PNG
    for i in os.listdir(source_path):
        new_file_name = i.split('.')
        new_file_name = new_file_name[0]
        cairosvg.svg2png(url=source_path + str(i), write_to=destination_path + str(new_file_name) + '.png',
                         output_width=output_w, output_height=output_h)

    for j in os.listdir(destination_path):

        img = cv2.imread(destination_path + j, cv2.IMREAD_UNCHANGED)
        img = cv2.cvtColor(img, cv2.COLOR_BGRA2RGBA)

        logo = Image.fromarray(np.uint8(img)).convert('RGBA').crop().getbbox()

        logo.save(destination_path + j, 'PNG')

我是这样调用函数的:

convert_to_png(current_dir + '/riot_sponsors/', current_dir + '/new_logos/', 2000, 1500)

出于某种原因,我收到此错误:

logo.save(destination_path + j, 'PNG')
AttributeError: 'tuple' object has no attribute 'save'

我的目标是将新裁剪出来的 png 文件保存到 destination_path

.getbbox() returns左右上下坐标的元组。这是返回给徽标变量的内容。

如果要去除图像的透明部分,请使用以下代码:

logo = Image.fromarray(np.uint8(img)).convert('RGBA')
logo = logo.crop(logo.getbbox())