PIL重复填充背景图像

4

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

enter image description here

它比我的图片尺寸还要小,所以我需要重复绘制它。(思考在css中的background-repeat)

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


1
请看这里,它展示了如何使用Python PIL库将小图像合并成大图像。 - Marcin
1个回答

13

根据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()

产生这样的结果:

1.png

或者删除 Image.eval() 行:

2.png


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接