tags: image blend pillow numpy This is a simple script used to generate the background of this page by blending the horizontal edges of an image. It is rough and ready, written primarily just for this particular image, but illustrates some basic Pillow and Numpy. I've left the comments in with my rough workings of the maths. (For those interested, the background image was taken [from here in pickpik.com](https://www.pickpik.com/brown-tree-purple-sky-golden-hour-purple-sunset-91149) — it was found by specifying Creative Commons in the Google image search tools, but I could find no license on the page, so I presume it's ok to use like this on a personal wiki.) ```py #!/usr/bin/env python3 from PIL import Image import numpy as np image = Image.open("pt_bg_001.png") data = np.asarray(image) height, width, _ = data.shape blend_width = 64 bw = blend_width bw2 = int(blend_width//2) blend_width = 2*bw2 # output width is input width - output_width = width - blend_width ow = output_width ow2 = ow - bw2 odata = np.zeros((height,output_width,3),dtype = data.dtype) for i in range(output_width): for j in range(height): if i < bw2: for k in range(3): # pixel 0 is 50/50 # pixel bw2 is 100% # when i==bw2, this==1.0 # when i==0, this==0.5 pc = 0.5*(1+(i/bw2)) pd = 1-pc b = data[j,i+bw2,k] a = data[j,width-bw2+i,k] c = int(a*pd+b*pc) odata[j,i,k] = c elif i > output_width - bw2: for k in range(3): # pixel 0 is 50/50 # pixel bw2 is 100% ii = i - ow2 # so ii now runs from 0 to bw2 a = data[j,i,k] b = data[j,i-ow2,k] pc = 0.5*(ii/bw2) pd = 1-pc c = int(a*pd+b*pc) odata[j,i,k] = c else: odata[j,i] = data[j,i+bw2] image2 = Image.fromarray(odata) image2.save("out.png") image2.save("pt2_bg1.jpg") ```