ssd1306_pillow_shapes.py
1 # Copyright (c) 2014 Adafruit Industries 2 # Author: Tony DiCola 3 # 4 # Permission is hereby granted, free of charge, to any person obtaining a copy 5 # of this software and associated documentation files (the "Software"), to deal 6 # in the Software without restriction, including without limitation the rights 7 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 # copies of the Software, and to permit persons to whom the Software is 9 # furnished to do so, subject to the following conditions: 10 # 11 # The above copyright notice and this permission notice shall be included in 12 # all copies or substantial portions of the Software. 13 # 14 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 # THE SOFTWARE. 21 22 # This example is for use on (Linux) computers that are using CPython with 23 # Adafruit Blinka to support CircuitPython libraries. CircuitPython does 24 # not support PIL/pillow (python imaging library)! 25 26 from board import SCL, SDA 27 import busio 28 from PIL import Image, ImageDraw, ImageFont 29 import adafruit_ssd1306 30 31 # Create the I2C interface. 32 i2c = busio.I2C(SCL, SDA) 33 34 # Create the SSD1306 OLED class. 35 # The first two parameters are the pixel width and pixel height. Change these 36 # to the right size for your display! 37 disp = adafruit_ssd1306.SSD1306_I2C(128, 32, i2c) 38 39 # Note you can change the I2C address, or add a reset pin: 40 # disp = adafruit_ssd1306.SSD1306_I2C(128, 32, i2c, addr=0x3c, reset=reset_pin) 41 42 # Clear display. 43 disp.fill(0) 44 disp.show() 45 46 # Create blank image for drawing. 47 # Make sure to create image with mode '1' for 1-bit color. 48 width = disp.width 49 height = disp.height 50 image = Image.new("1", (width, height)) 51 52 # Get drawing object to draw on image. 53 draw = ImageDraw.Draw(image) 54 55 # Draw a black filled box to clear the image. 56 draw.rectangle((0, 0, width, height), outline=0, fill=0) 57 58 # Draw some shapes. 59 # First define some constants to allow easy resizing of shapes. 60 padding = 2 61 shape_width = 20 62 top = padding 63 bottom = height - padding 64 # Move left to right keeping track of the current x position for drawing shapes. 65 x = padding 66 # Draw an ellipse. 67 draw.ellipse((x, top, x + shape_width, bottom), outline=255, fill=0) 68 x += shape_width + padding 69 # Draw a rectangle. 70 draw.rectangle((x, top, x + shape_width, bottom), outline=255, fill=0) 71 x += shape_width + padding 72 # Draw a triangle. 73 draw.polygon( 74 [(x, bottom), (x + shape_width / 2, top), (x + shape_width, bottom)], 75 outline=255, 76 fill=0, 77 ) 78 x += shape_width + padding 79 # Draw an X. 80 draw.line((x, bottom, x + shape_width, top), fill=255) 81 draw.line((x, top, x + shape_width, bottom), fill=255) 82 x += shape_width + padding 83 84 # Load default font. 85 font = ImageFont.load_default() 86 87 # Alternatively load a TTF font. Make sure the .ttf font file is in the 88 # same directory as the python script! 89 # Some other nice fonts to try: http://www.dafont.com/bitmap.php 90 # font = ImageFont.truetype('Minecraftia.ttf', 8) 91 92 # Write two lines of text. 93 draw.text((x, top), "Hello", font=font, fill=255) 94 draw.text((x, top + 20), "World!", font=font, fill=255) 95 96 # Display image. 97 disp.image(image) 98 disp.show()