【问题标题】:PIL fill background image repeatedlyPIL 反复填充背景图像
【发布时间】:2014-08-01 01:09:12
【问题描述】:

我有一个像这样的小背景图片:

它比我的图像尺寸小,所以我需要反复绘制它。 (想想css中的背景重复)

我搜索了很多,但找不到解决方案....非常感谢。

【问题讨论】:

  • 看看here。它展示了如何将小图像变成更大的图像。

标签: python python-imaging-library


【解决方案1】:

根据 Marcin 链接的代码,这会将背景图像平铺在较大的图像上:

from PIL import Image

# Opens an image
bg = Image.open("NOAHB.png")

# The width and height of the background tile
bg_w, bg_h = bg.size

# Creates a new empty image, RGB mode, and size 1000 by 1000
new_im = Image.new('RGB', (1000,1000))

# The width and height of the new image
w, h = new_im.size

# Iterate through a grid, to place the background tile
for i in xrange(0, w, bg_w):
    for j in xrange(0, h, bg_h):
        # Change brightness of the images, just to emphasise they are unique copies
        bg = Image.eval(bg, lambda x: x+(i+j)/1000)

        #paste the image at location i, j:
        new_im.paste(bg, (i, j))

new_im.show()

产生这个:

或删除Image.eval() 行:

【讨论】: