Home > Net >  How to create semi transparent pattern with python pil?
How to create semi transparent pattern with python pil?

Time:09-25

I have found some examples on this enter image description here

CodePudding user response:

In a standard grayscale image, black pixels are 0, gray pixels are 128, and white ones are 255:

import numpy as np
import matplotlib.pyplot as plt

# first create one 20 x 20 tile
a1 = np.zeros((20,20), dtype=int)
a1[10:20,0:10] = a1[0:10,10:20] = 128
a1[10:20,10:20] = 255

# fill the whole 100 x 100 area with the tiles
a = np.tile(a1, (5,5))

# plot and save
plt.imshow(a, 'Greys_r')
plt.savefig('pattern.png')

CodePudding user response:

You could do this:

from PIL import Image
import numpy as np

# Make grey 2x2 image
TwoByTwo = np.full((2,2), 128, np.uint8)

# Change top-left to black, bottom-right to white
TwoByTwo[0,0] = 0
TwoByTwo[1,1] = 255

# Tile it
tiled = np.tile(TwoByTwo, (5,5))

# Make into PIL Image, rescale in size and save
Image.fromarray(tiled).resize((100,100), Image.NEAREST).save('result.png')

enter image description here

  • Related