Skip to content

Instantly share code, notes, and snippets.

@TimSC
Created March 16, 2018 03:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TimSC/52190fca6b1fb223e952ebf3118bde97 to your computer and use it in GitHub Desktop.
Save TimSC/52190fca6b1fb223e952ebf3118bde97 to your computer and use it in GitHub Desktop.
Funky seamless texture generator
#Funky seamless texture generator
#By Tim Sheerman-Chase (c) 2018
#Released under CC0 license
from __future__ import print_function
from PIL import Image
import argparse
if __name__=="__main__":
parser = argparse.ArgumentParser(description='Process command line.')
parser.add_argument('img')
parser.add_argument('--margin', type=int, default=200)
parser.add_argument('--out', default="blend.jpg")
args = parser.parse_args()
img = Image.open(args.img)
img2 = img.convert("RGBA")
pix2 = img2.load()
blendMargin = args.margin
#Do horizontal repeat
imgc = img2.crop((0, 0, img.width-blendMargin, img.height))
imgcpix = imgc.load()
right = Image.new("RGBA", imgc.size)
rightpix = right.load()
for x in range(blendMargin):
for y in range(right.height):
pv = pix2[x+img2.width-blendMargin, y]
alphaNorm = max(1.0-float(x)/blendMargin, 0)
rightpix[x, y] = (pv[0], pv[1], pv[2], int(round(255.0 * alphaNorm)))
imgc = Image.alpha_composite(imgc, right)
#Do vertical repeat
srcImg = imgc
srcImgPix = srcImg.load()
imgc2 = imgc.crop((0, 0, imgc.width, imgc.height-blendMargin))
imgc2pix = imgc2.load()
bottom = Image.new("RGBA", imgc2.size)
bottompix = bottom.load()
for x in range(srcImg.width):
for y in range(blendMargin):
pv = srcImgPix[x, y+srcImg.height-blendMargin]
alphaNorm = max(1.0-float(y)/blendMargin, 0)
bottompix[x, y] = (pv[0], pv[1], pv[2], int(round(255.0 * alphaNorm)))
imgc3 = Image.alpha_composite(imgc2, bottom)
print ("Writing", args.out)
imgc3.convert("RGB").save(args.out)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment