これは私の予備的なハックソリューションです:
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_black[upper_idx]
lower = self.first_black[lower_idx]
except IndexError:
return 0
if None in [upper, lower]:
return 0
up_weight, low_weight = abs(upper-x), abs(lower-x)
return (up_weight*upper + low_weight*lower)/(up_weight + low_weight)
def norm_at(self, x, length):
un_normed = self.at(x*self.picture.size[0]/length)
return (un_normed - self.min)/self.max