0

I am trying to make a boardgame map that uses hexagon tiles (similar to a Catan board). In the background of each hex, I want an image that takes up the whole hexagon. The problem is, I don't have images that are nice hexagon shapes, so I was hoping to take a retangular image, crop it into the hex I've drawn, and use that. I am new to programming anything graphical, and have tried searching significantly but cannot seem to find a working solution. I have tried implementing the first solution here, but the image does not have a transparent background around the hexagon (its white instead).

Simply put, I need to take a square image, make it hexagonal, then display it at a set position. Advice?

jk1324
  • 1
  • 1
  • Do you need hexagonal images? Can you display the board and then display your tile details over the top? – import random Mar 21 '18 at 05:19
  • I would think some kind of parenting or layering would be a solution but I dont know. Is the map static? in that case you can just create the entire board as one image. – Mercury Platinum Mar 22 '18 at 14:09

1 Answers1

0

When pasting one image onto another, you can supply a mask argument, that will only update the parts of the image specified - https://pillow.readthedocs.io/en/5.2.x/reference/Image.html#PIL.Image.Image.paste

from PIL import Image, ImageDraw

im = Image.open('test.png')

# I'm creating a random shape here
mask = Image.new('RGBA', im.size)
d = ImageDraw.Draw(mask)
d.polygon(((20, 20), (20, 50), (100, 100), (100, 20)), fill='#000')

out = Image.new('RGBA', im.size)
out.paste(im, (0, 0), mask)
out.show()
radarhere
  • 929
  • 8
  • 23