PIL 重复填充背景图片 [英] PIL fill background image repeatedly

查看:65
本文介绍了PIL 重复填充背景图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a small background image like this:

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

it's a smaller than my image size, so I need to draw it repeatedly. (think about background-repeat in css)

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

I searched a lot and can't find a solution to this....thanks a lot.

推荐答案

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

Based on the code linked to by Marcin, this will tile a background image on a larger one:

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() 行:

这篇关于PIL 重复填充背景图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆