答案 0 :(得分:2)
您可以这样做...
#!/usr/bin/env python3
from PIL import Image
import numpy as np
# Open wavy image and make greyscale
i = Image.open('wavy.jpg').convert('L')
# Make Numpy version of input image and threshold at 128
i = np.array(i)
i = np.where(i>128, np.uint8(255), np.uint8(0))
# Make Numpy version of output image - all black initially
o = np.zeros_like(i)
h, w = i.shape
# Process each column, copying white pixels from input image
# ... to output image centred on horizontal centreline
centre = h//2
for col in range(w):
# Find top and bottom white pixel in this column
whites = np.nonzero(i[:,col])
top = whites[0][0] # top white pixel
bot = whites[0][-1] # bottom white pixel
thk = bot - top # thickness of sandwich filling
# Copy those pixels to output image
startrow = centre - thk//2
o[startrow:startrow+thk,col] = i[top:bot,col]
Image.fromarray(o).save('result.png')