这是我的初步解决方案:from PIL import Image
import numpy as np
class Pic_Function():
def __init__(self, picture_path):
self.picture = Image.open(picture_path)
self.pixels = self.picture.load()
self.columns = []
# is there really no image method to get a numpy array of pixels?
for i in range(self.picture.size[0]):
self.columns.append([self.pixels[i,j] for j in range(self.picture.size[1])])
self.first_black = []
for i in self.columns:
try:
self.first_black.append(self.picture.size[0] - i.index((0,0,0)))
except ValueError:
self.first_black.append(None)
self.max, self.min = max(self.first_black), min([j for j in self.first_black if j != None])
def at(self, x):
upper_idx = int(math.ceil(x))
lower_idx = upper_idx - 1
try:
upper = self.first