El primer artículo buscado en Baidu puede convertir un fondo transparente en un fondo blanco, pero el defecto es muy grave, lo que provocará muchos píxeles en el fondo de la imagen y la eficiencia no es muy alta.
Después de otra búsqueda, encontré una mejor manera .
from PIL import Image
try:
imagePtah = 'your image file path'
img = Image.open(imagePtah)
if img.mode != 'RGBA':
image = img.convert('RGBA')
width = img.width
height = img.height
image = Image.new('RGB', size=(width, height), color=(255, 255, 255))
image.paste(img, (0, 0), mask=img)
image.show()
except Exception as e:
print(e)
El uso Image.paste
es tan simple y tan. La vida de Baidu es negra.
El método anterior, por supuesto, también se puede convertir a otros colores, simplemente elija el que necesita, simplemente reemplácelo color=(255, 255, 255)
.