From b246a1d120c1c8cc58350b07acb37ef0fb1841d7 Mon Sep 17 00:00:00 2001 From: Ben Date: Mon, 3 Apr 2017 14:12:41 +0000 Subject: [PATCH 1/5] First attempt at inverted text colours This should allow white text on a black background, if the "invert" parameter is set to True. Also updated WriteAll() to allow partial or full updates on the PaPiRus screen. Partial updates for text being a lot quicker than a full one. --- papirus/textpos.py | 48 +++++++++++++++++++++++++++++++++------------- 1 file changed, 35 insertions(+), 13 deletions(-) diff --git a/papirus/textpos.py b/papirus/textpos.py index 59ae98e..8619892 100644 --- a/papirus/textpos.py +++ b/papirus/textpos.py @@ -11,13 +11,14 @@ # Class for holding the details of the text class DispText(): - def __init__(self, text, x, y, size): + def __init__(self, text, x, y, size, invert): self.text = text self.x = x self.y = y self.size = size self.endx = 0 self.endy = 0 + self.invert = invert class PapirusTextPos(): @@ -28,17 +29,17 @@ def __init__(self, autoUpdate = True): self.image = Image.new('1', self.papirus.size, WHITE) self.autoUpdate = autoUpdate - def AddText(self, text, x=0, y=0, size = 20, Id = None): + def AddText(self, text, x=0, y=0, size = 20, Id = None, invert=False): # Create a new Id if none is supplied if Id == None: Id = str(uuid.uuid4()) # If the Id doesn't exist, add it to the dictionary if Id not in self.allText: - self.allText[Id] = DispText(text.decode('string_escape'), x, y, size) + self.allText[Id] = DispText(text.decode('string_escape'), x, y, size, invert) # add the text to the image self.addToImageText(Id) - #Automatically show? + # Automatically show? if self.autoUpdate: self.WriteAll() @@ -78,6 +79,12 @@ def addToImageText(self, Id): size = self.allText[Id].size x = self.allText[Id].x y = self.allText[Id].y + font_col = BLACK + back_col = WHITE + + if self.allText[Id].invert: + font_col = WHITE + back_col = BLACK # prepare for drawing draw = ImageDraw.Draw(self.image) @@ -104,7 +111,10 @@ def addToImageText(self, Id): for word in line.split(): # If there is space on line add the word to it if (len(text_lines[current_line]) + len(word)) < line_size: - text_lines[current_line] += " " + word + # Only add a space if there`s something on the line + if len(text_lines[current_line]) > 0: + text_lines[current_line] += " " + text_lines[current_line] += word else: # No space left on line so move to next one text_lines.append("") @@ -119,32 +129,44 @@ def addToImageText(self, Id): self.allText[Id].endy = y self.allText[Id].endx = x - # Start at the beginning + # Start at the beginning, calc all the end locations current_line = 0 for l in text_lines: current_line += 1 # Find out the size of the line to be drawn textSize = draw.textsize(l, font=font) # Adjust the x end point if needed - if textSize[0]+x> self.allText[Id].endx: + if textSize[0]+x > self.allText[Id].endx: self.allText[Id].endx = textSize[0] + x # Add on the y end point self.allText[Id].endy += textSize[1] - # Draw the text to the image - draw.text( (x, ((size*current_line)-size) + y) , l, font=font, fill=BLACK) # Little adjustment to make sure the text gets covered self.allText[Id].endy += 3 - def WriteAll(self): + # If the text is wanted inverted, put a rectangle down first + if self.allText[Id].invert: + draw.rectangle([self.allText[Id].x, self.allText[Id].y, self.allText[Id].endx, self.allText[Id].endy], fill=back_col) + + # Start at the beginning, add all the lines to the image + current_line = 0 + for l in text_lines: + current_line += 1 + # Draw the text to the image + draw.text( (x, ((size*current_line)-size) + y) , l, font=font, fill=font_col) + + def WriteAll(self, full_update=False): # Push the image to the PaPiRus device, and update only what's needed + # (unless asked to do a full update) self.papirus.display(self.image) - self.papirus.update() + if full_update: + self.papirus.update() + else: + self.papirus.partial_update() def Clear(self): # Clear the image, clear the text items, do a full update to the screen self.image = Image.new('1', self.papirus.size, WHITE) self.allText = dict() - self.papirus.display(self.image) - self.papirus.update() + self.papirus.clear() From 381ef4fe58fc725e11aa5d7584304f20d4bad37e Mon Sep 17 00:00:00 2001 From: Ben Date: Mon, 3 Apr 2017 14:21:51 +0000 Subject: [PATCH 2/5] Updated README.md with invert example Added a third Positional Text example with the inverted text option being used --- README.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/README.md b/README.md index ebcd134..495457c 100644 --- a/README.md +++ b/README.md @@ -147,6 +147,18 @@ text.RemoveText("Top") text.WriteAll() ``` +#### The Positional Text API (example 3) +```python +from papirus import PapirusTextPos + +# Same as calling "PapirusTextPos(True)" +text = PapirusTextPos() + +# Write text to the screen at selected point, with an Id +# This will write "hello world" to the screen with white text and a black background +text.AddText("hello world", 10, 10, Id="Start", invert=True) +``` + #### The Image API ```python from papirus import PapirusImage From 609ddbc8cceee08133d9905bda2f87c6cb3739e3 Mon Sep 17 00:00:00 2001 From: Ben Date: Tue, 4 Apr 2017 19:17:23 +0100 Subject: [PATCH 3/5] Updated WriteAll() to default to a full update Made so that partial updates can be called singly or setting "partial_update" on the instantiated class will make all "WriteAll()" calls be partial updates. The whole class defaults to full updates. --- papirus/textpos.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/papirus/textpos.py b/papirus/textpos.py index 8619892..65fa8d1 100644 --- a/papirus/textpos.py +++ b/papirus/textpos.py @@ -28,6 +28,7 @@ def __init__(self, autoUpdate = True): self.allText = dict() self.image = Image.new('1', self.papirus.size, WHITE) self.autoUpdate = autoUpdate + self.partial_updates = False def AddText(self, text, x=0, y=0, size = 20, Id = None, invert=False): # Create a new Id if none is supplied @@ -155,14 +156,14 @@ def addToImageText(self, Id): # Draw the text to the image draw.text( (x, ((size*current_line)-size) + y) , l, font=font, fill=font_col) - def WriteAll(self, full_update=False): + def WriteAll(self, partial_update=False): # Push the image to the PaPiRus device, and update only what's needed # (unless asked to do a full update) self.papirus.display(self.image) - if full_update: - self.papirus.update() - else: + if partial_update or self.partial_updates: self.papirus.partial_update() + else: + self.papirus.update() def Clear(self): # Clear the image, clear the text items, do a full update to the screen From 5c31e96ef0b9dca7e1500f3d3fba79b7e7eca28f Mon Sep 17 00:00:00 2001 From: Ben Date: Tue, 4 Apr 2017 19:27:56 +0100 Subject: [PATCH 4/5] Updated README.md Updated README.md with information about partial updates and how to activate them. --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index 495457c..e00a083 100644 --- a/README.md +++ b/README.md @@ -177,6 +177,10 @@ image.write('/path/to/image', 20, (10, 10) ) PaPiRusTextPos will take in to account \n as a line break (or multiple line breaks) Meaning text will be aligned to the X position given, it will not return to x=0 for the start of the next line. +When using the PapirusTextPos, in either mode, setting the "partial_updates" property to True will cause partial updates to be done, meaning only the section of the PaPiRus screen that has been changed will be updated. These can be vastly quicker than a full update for each piece of text. + +If not using the "partial_updates" property, calling "WriteAll(True)" will do the same thing on a one off basis. + # Command Line ```bash From c86941eb4c5a081148bad2d6ccea6b703dd55c3d Mon Sep 17 00:00:00 2001 From: Ben Date: Fri, 16 Jun 2017 22:18:46 +0100 Subject: [PATCH 5/5] Added invert functionality Added in the parameter to allow the inverting of text being displayed. --- bin/papirus-write | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/bin/papirus-write b/bin/papirus-write index a8dfb4d..9d9ab7e 100644 --- a/bin/papirus-write +++ b/bin/papirus-write @@ -31,13 +31,14 @@ def main(): p.add_argument('--posY', '-y', type=int, default=0, help="Y position of the start of the text") p.add_argument('--fsize', '-s',type=int , default=20, help="Font size to use for the text") p.add_argument('--rotation', '-r',type=int , default=0, help="Rotation one of 0, 90, 180, 270") + p.add_argument('--invert', '-i', type=bool, default=False, help="Invert the display of the text") args = p.parse_args() if args.content: text = PapirusTextPos(rotation=args.rotation) print("Writing to Papirus.......") - text.AddText(args.content, args.posX, args.posY, args.fsize) + text.AddText(args.content, args.posX, args.posY, args.fsize, invert=args.invert) print("Finished!") if __name__ == '__main__':