4

I have a small background image like this:

enter image description here

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.

wong2
  • 34,358
  • 48
  • 134
  • 179
  • 1
    Have a look [here](http://stackoverflow.com/questions/10647311/how-to-merge-images-using-python-pil-library). It shows how to marge small images into bigger one. – Marcin Jun 11 '14 at 05:22

1 Answers1

12

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

Produces this:

1.png

Or removing the Image.eval() line:

2.png

Hugo
  • 27,885
  • 8
  • 82
  • 98