魔杖 - 创建带背景的空图像
在Python中,我们可以使用 Wand 创建纯色背景。我们可以使用这些背景在图像中进一步使用。我们可以在无背景图像中使用这些背景,使其更具吸引力。这可以通过简单地使用 Image()函数并设置宽度、高度和背景参数来完成。
句法 :
Python3
with Image(width=image_width ,
height=image_height ,
background ='color') as img:
# other image manipulation code
Python3
# import Image from wand.image module
from wand.image import Image
# create image using Image() function
with Image(width = 400, height = 300) as img:
# Save image with a valid filename
img.save(filename ='transparent.png')
Python3
# import Color from wand.color module
from wand.color import Color
# import Image from wand.image module
from wand.image import Image
clr = Color('green')
with Image(width = 400, height = 300, background = clr) as img:
img.save(filename ='green.png')
现在让我们看看创建背景的代码。
示例 1:
Python3
# import Image from wand.image module
from wand.image import Image
# create image using Image() function
with Image(width = 400, height = 300) as img:
# Save image with a valid filename
img.save(filename ='transparent.png')
输出 :
示例 2:使用纯绿色创建背景
Python3
# import Color from wand.color module
from wand.color import Color
# import Image from wand.image module
from wand.image import Image
clr = Color('green')
with Image(width = 400, height = 300, background = clr) as img:
img.save(filename ='green.png')
输出 :