用于调整大小和背景。使用以下内容,并注意您需要自己计算 300%。
from wand.image import Image
from wand.color import Color
with Image(filename="pic.png") as img:
# -resize 300%
scaler = 3
img.resize(img.width * scaler, img.height * scaler)
# -background white
img.background_color = Color("white")
img.save(filename="pic2.png")
不幸的是,c方法MagickMergeImageLayers尚未实现。您应该向开发团队提交增强请求。
更新
如果要删除透明度,只需禁用 alpha 通道
from wand.image import Image
with Image(filename="pic.png") as img:
# Remove alpha
img.alpha_channel = False
img.save(filename="pic2.png")
另一种方式
创建与第一个尺寸相同的新图像可能会更容易,然后将源图像合成到新图像上。
from wand.image import Image
from wand.color import Color
with Image(filename="pic.png") as img:
with Image(width=img.width, height=img.height, background=Color("white")) as bg:
bg.composite(img,0,0)
# -resize 300%
scaler = 3
bg.resize(img.width * scaler, img.height * scaler)
bg.save(filename="pic2.png")