Uhlhorn Posted March 1, 2019 at 11:23 PM Posted March 1, 2019 at 11:23 PM Moin, als Anfänger hat man es ziemlich schwer. Okay, ich habe das Script zum OLED 128x64 Bricklet 2.0 hergenommen um ein Bild auszugeben. Wenn ich in Photoshop stellte Bitmaps lade, klappt es prima. Nun erstelle ich aber die Bitmap in PIL. Und nun klappt es gar nicht mehr. :-( Also, beim Aufruf von image_data[0, 0] bekomme ich die Fehlermeldung TypeError: 'Image' object is not subscriptable Bei den geladenen Bitmaps macht der Aufruf keine Probleme, bei den selbst erstellten Bitmaps aber schon. Das funktioniert: … image_data = image.load() pixel = image_data[column, row] > 0 … Das funktioniert nicht: … image_data = Image.new('1', (128,64)) pixel = image_data[column, row] > 0 … Beides sind 1-Bit-Bilder in 128 x 64 Pixel. Hier ein Beispiel: from PIL import Image, ImageDraw, ImageFont Mode = "1" Size = (21 * 5, 64) # make a blank image for the text, initialized to transparent text color image_data = Image.new('1', (128,64)) # get a font fnt = ImageFont.truetype('Menlo.ttc', 28, 1) # get a drawing context d = ImageDraw.Draw(image_data) # draw text, half opacity d.text((15,15), "-42°C", font=fnt, fill=(255)) # draw text print(image_data.size[0]) print(image_data.size[1]) print(image_data[0, 0]) print() image_data.show() In dem Ordner mit dem Script muss die Schrift Menlo liegen (im Anhang). Menlo.ttc.zip Quote
borg Posted March 2, 2019 at 12:16 AM Posted March 2, 2019 at 12:16 AM Probier es mal so: image = Image.new('1', (128,64)) image_data = image.load() pixel = image_data[column, row] > 0 Quote
Uhlhorn Posted March 2, 2019 at 02:12 AM Author Posted March 2, 2019 at 02:12 AM Ah, danke! Ich glaube, dass ich auch verstehe, was da läuft. :-) Quote
Uhlhorn Posted March 2, 2019 at 02:26 AM Author Posted March 2, 2019 at 02:26 AM Das Testprogramm soll einen String mit großen Fonts anzeigen. Allerdings laufe ich jetzt in eine andere Fehlermeldung außerhalb meines eigenen Programms. in dem einen Programm funktioniert es, in dem anderen Programm bekomme ich komische Fehlermeldungen. :-( [size=9pt]Traceback (most recent call last): File "/Users/gerhard/PycharmProjects/RedBrick/tinkerforge/ip_connection.py", line 1164, in send_request response = device.response_queue.get(True, self.timeout) File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/queue.py", line 178, in get raise Empty _queue.Empty During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/Users/gerhard/PycharmProjects/RedBrick/OLED-Test.py", line 56, in <module> oled.write_pixels(0, 0, WIDTH, HEIGHT, pixels) File "/Users/gerhard/PycharmProjects/RedBrick/tinkerforge/bricklet_oled_128x64_v2.py", line 400, in write_pixels ret = self.write_pixels_low_level(x_start, y_start, x_end, y_end, pixels_length, pixels_chunk_offset, pixels_chunk_data) File "/Users/gerhard/PycharmProjects/RedBrick/tinkerforge/bricklet_oled_128x64_v2.py", line 130, in write_pixels_low_level self.ipcon.send_request(self, BrickletOLED128x64V2.FUNCTION_WRITE_PIXELS_LOW_LEVEL, (x_start, y_start, x_end, y_end, pixels_length, pixels_chunk_offset, pixels_chunk_data), 'B B B B H H 448!', '') File "/Users/gerhard/PycharmProjects/RedBrick/tinkerforge/ip_connection.py", line 1173, in send_request raise Error(Error.TIMEOUT, msg) tinkerforge.ip_connection.Error: Did not receive response for function 1 in time (-1)[/size] Hier das betroffene Programm: #!/usr/bin/env python # -*- coding: utf-8 -*- HOST = "localhost" PORT = 4223 UID = "XYZ" # Change to your UID WIDTH = 128 # Columns HEIGHT = 64 # Rows MODE = "1" import sys from PIL import Image, ImageDraw, ImageFont from tinkerforge.ip_connection import IPConnection from tinkerforge.bricklet_oled_128x64_v2 import BrickletOLED128x64V2 if __name__ == "__main__": ipcon = IPConnection() # Create IP connection oled = BrickletOLED128x64V2(UID, ipcon) # Create device object ipcon.connect(HOST, PORT) # Connect to brickd # Don't use device before ipcon is connected # Clear display oled.clear_display() image = Image.new(MODE,(WIDTH,HEIGHT)) font = ImageFont.truetype('Menlo.ttc', 28, 1) d = ImageDraw.Draw(image) # get a drawing context d.text((5, 15), "-42,0°C", font=font, fill=(255)) # draw text image_data = image.load() # Convert image to black/white pixels #image = Image.open(image_data) #print("size: "+str(image_data.size[0])) pixels = [] for row in range(HEIGHT): for column in range(WIDTH): print("row: " + str(row)) print("column: " + str(column)) # print(image.size[0]) # print(image.size[1]) # print(image_data[column, row]) if column < image.size[0] and row < image.size[1]: pixel = image_data[column, row] > 0 else: pixel = False pixels.append(pixel) print(pixels) print(len(pixels)) image.show() oled.write_pixels(0, 0, WIDTH, HEIGHT, pixels) input('Press key to exit\n') # Use input() in Python 3 ipcon.disconnect() Quote
Uhlhorn Posted March 2, 2019 at 08:41 PM Author Posted March 2, 2019 at 08:41 PM Oh, ich habe den Fehler gefunden! :-) Ich hatte irgendwie vergessen die UID zu setzen. Jetzt geht es. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.