def _fix_alpha_channel(self):
# This is a fix for a bug where the Alpha channel was dropped.
colors3to4 = [(c[:3], c[3]) for c in self.names.keys()]
colors3to4 = dict(colors3to4)
assert(len(colors3to4) == len(self.names)) # Dropped alpha channel causes colors to collide :(
for lbl in self.labels:
if lbl is None:
continue # No label file created yet.
img = Image.open(lbl)
size = img.size
img = np.array(img)
if img.shape[2] == 4:
continue # Image has alpha channel, good.
elif img.shape[2] == 3:
# Lookup each (partial) color and find what its alpha should be.
alpha = np.apply_along_axis(lambda c: colors3to4[tuple(c)], 2, img)
data = np.dstack([img, np.array(alpha, dtype=np.uint8)])
new_img = Image.frombuffer("RGBA", size, data, "raw", "RGBA", 0, 1)
new_img.save(lbl)
print("FIXED", lbl)
评论列表
文章目录