框架:

frame

Avatar: avatar

需要做的是:

result

只是透明的

from PIL import Image

background = Image.open("avatar.png")
foreground = Image.open("border.png")

foreground = foreground.resize((512, 512))

image = Image.new("RGBA", (512, 512))
image.paste(foreground, (0, 0))

image.paste(background, (512, 512), background)
image.show()

当我在粘贴头像后粘贴帧时,它不起作用,因为它完全覆盖了头像,使其不可见.我try 了许多其他方法,也进行了搜索,但都不起作用.

推荐答案

我认为你需要这样做:

  • 打开并调整现有图像的大小
  • 创建透明背景
  • 首先粘贴头像,带偏移量
  • 粘贴帧秒

#!/usr/bin/env python3

from PIL import Image

# Set common size for everything
size = (512,512)

# Load frame and avatar, resizing to 512x512
avatar = Image.open("avatar.png").resize((180,180))
frame  = Image.open('frame.png').resize(size)

# Make transparent background for final image
image = Image.new("RGBA", size)
image.putalpha(0)

# Paste avatar onto background with positioning offset
image.paste(avatar, (170,170), avatar)
image.save('DEBUG-avatar-pasted.png')

# Paste frame over avatar and background retaining transarency
image.paste(frame, (0,0), frame)

# Save result
image.save('result.png')

enter image description here


这是一个黄色背景,所以你可以在StackOverflow令人讨厌的背景 colored颜色 上看到它:

enter image description here


现在我喝了更多的咖啡,我注意到:

image = Image.new("RGBA", size, color='#0000')

在创建透明背景方面比以下内容更简洁:

image = Image.new("RGBA", size)
image.putalpha(0)

Python相关问答推荐

使用Keras的线性回归参数估计

将特定列信息移动到当前行下的新行

Django mysql图标不适用于小 case

Gekko:Spring-Mass系统的参数识别

2D空间中的反旋算法

将图像拖到另一个图像

优化pytorch函数以消除for循环

如何在Python数据框架中加速序列的符号化

OR—Tools中CP—SAT求解器的IntVar设置值

如何使Matplotlib标题以图形为中心,而图例框则以图形为中心

使用Python从URL下载Excel文件

在www.example.com中使用`package_data`包含不包含__init__. py的非Python文件

如何在两列上groupBy,并使用pyspark计算每个分组列的平均总价值

在pandas/python中计数嵌套类别

在用于Python的Bokeh包中设置按钮的样式

TypeError:';Locator';对象无法在PlayWriter中使用.first()调用

文本溢出了Kivy的视区

如何在Python中创建仅包含完整天数的月份的列表

大型稀疏CSR二进制矩阵乘法结果中的错误

某些值的数值幂和**之间的差异