【发布时间】:2021-01-29 22:38:55
【问题描述】:
我有这个函数,它采用 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
【问题讨论】:
标签: python tuples python-imaging-library attributeerror cv2