python - PIL 重复填充背景图片

标签 python python-imaging-library

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

enter image description here

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

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

最佳答案

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

关于python - PIL 重复填充背景图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24155087/

相关文章:

python - 如何通过图形化的 sudo 在 python 中获得 root 权限?

python - 我无法使用 fastai.text 中的 pretrained_model=URLs.WT103

python - 在python中使用递归函数和结构类型?

Python Selenium WebDriver Chrome click() 不起作用

python - 如何在 Tkinter 中更改图像的分辨率?

python - 尝试从图像中移动像素

python - 对同色像素 block 的边界进行着色

python - IOError : "decoder zip not available" using matplotlib PNG in ReportLab on Linux, 适用于 Windows

python - 为什么 pygettext 翻译元组中的字符串而不是元组列表中的字符串?

python - 如何使用 PIL 将 python 图像放在一起?