From 0e56d3c7c34c7ff7b83979a73515e2b49bf0cb05 Mon Sep 17 00:00:00 2001 From: WIPocket Date: Mon, 4 Apr 2022 11:11:26 +0200 Subject: [PATCH] Convert pixels to valid on export --- lib.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/lib.py b/lib.py index b456db9..fc74b16 100644 --- a/lib.py +++ b/lib.py @@ -6,6 +6,12 @@ class Pixel(namedtuple('Pixel', ['r', 'g', 'b'])): raise ValueError('Some component too bright.') if min(self.r, self.g, self.b) < 0: raise ValueError('Some component negative.') + def _to_valid(self): + return Pixel( + int(max(0, min(255, self.r))), + int(max(0, min(255, self.g))), + int(max(0, min(255, self.b))), + ) def __add__(self, other): return Pixel( self.r + other.r, @@ -58,7 +64,7 @@ class Picture: seq = [] for line in self.pixels: for pix in line: - seq.extend(pix) + seq.extend(pix._to_valid()) # bytes… b = bytes(seq)