python - 扩展图像 (PIL/Pillow)

标签 python python-imaging-library pillow

我可以使用以下代码在基本图像上绘制黑色 strip (或矩形):

base_width, base_height = img.size
background = Image.new('RGBA', (base_width, base_height/3),(0,0,0,146))
offset = (0,base_height/2)
img.paste(background,offset,mask=background)

结果:

enter image description here

但我如何扩展图像的高度,使所述黑色 strip 出现在图像底部边框下方,在图像本身之外

如果我在上面的代码中移动 offset,黑色 strip 不能移动到基本图像的边界之外,所以这不是一个可行的解决方案。

最佳答案

这是一种方法:

  1. 创建一个new_img,其大小为(base_width, base_height + background.height)
  2. 将原来的 img 粘贴到 new_img(0, 0)
  3. 背景粘贴到new_img(0, base_height)

关于python - 扩展图像 (PIL/Pillow),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43831842/

相关文章:

python - 在 Python 中逼近一个未知值

python gir/Gtk.Image : why does it corrupt my image?

python - numpy 或 python 中的等效 matlab 函数 mod

python - Image.open() 无法识别图像文件 - Python?

django - 在模板中显示上传的图像 - Django

python - 使用 Pillow 和 Python 3 从 RGB 列表创建图像

python - stats.f_oneway Scipy Anova 返回 2 个包含 4 个值的数组

python - 如何在 Python/Django 中获取图片标题

python - 从抛物面镜检测图像中心?

python - 有没有办法用 PIL 来回退丢失的字形?