TeCoEd (Teaching Computing Education)
  • Home
    • Freelance
    • Book
    • Downloading
  • Python
    • Learn Python >
      • Python Modules
    • PyGame Zero
    • Python Programs >
      • Higher or Lower
      • Magic Calculator
      • Password Checker
      • Python Pit
    • What's News App
    • Pixels to Cells
    • Python Mosaics
    • Python OCR
    • L-1-AM
    • Web Scraping >
      • Scraping Trains
    • Weather App
    • Snakes and Windows
    • Python Web Server >
      • Flask
    • Python Picks
  • Ras Pi
    • All About the Pi
    • Getting Started
    • Remote Desktop and VNC
    • Static IP Address
    • Sonic Pi >
      • 3.14
    • Twitter Feed >
      • Tweepy
    • Android & Pi >
      • Advanced Apps
      • Odds
    • A.I on the the Pi
    • CRON
    • Pick Your Own
  • Pi Hardware
    • Pi HATS >
      • Sense Hat Hacks
      • AstroPi HAT
      • Unicorn-HAT >
        • Unicorn Alphabet Disco
        • Uni Codes / Programs
      • Skywriter
      • Piano HAT
    • STS Pi
    • Pi Camera >
      • Pi-Cam, Python & Email >
        • Time Lapse
      • Pi Noir
    • Pipsta >
      • Flask, Input & Printers
    • Raspberry Pi Power >
      • Energenie IR power
    • Pibrella
    • Distance Sensor
    • LCD Screen
    • Pi-Tooth
    • Robot Arm
    • PiGlow
    • PiFM
    • Accelerometer
    • PiFace >
      • Installing PiFace >
        • Python Commands
  • Pi-Hacks
    • Drone Hacks
    • Pi Glue Gun Hack
    • Blinkt!
    • Sonic Pixels
    • R2D2
    • Get to the chopper
    • Astro Bird
    • Twitter Translator
    • Hacking a Robot
    • Nature_Box >
      • Best Nature Photos
    • Wearable Tech >
      • Project New York
      • P.N.Y Part 2 Health
      • P.N.Y Part 3 Games
      • P.N.Y Part 4 Translation
    • Dino-Tweet
    • Other Links
  • Pi-Hacks 2
    • The Joker
    • Hologram Machine
    • Google Vision: Camera Tell
    • Yoda Tweets
    • Pi Phone
    • Darth Beats
    • Twitter Keyword Finder
    • Crimbo Lights Hack
    • Xmas Elf
    • Halloween 2016
    • Halloween Hack 2015
    • Socrative Zombie
    • Voice Translation
    • The Blue-Who Finder
    • GPIO, Twitter
    • Pi Chat Bot >
      • Dictionary Definitions
    • PiGlow & Email
    • Pibrella Alarm System
    • SMS with Python >
      • Spooking a Mobile
  • Pi-Hacks 3
    • Ferminal
    • Crypto Tracker
    • David Bowie
    • Lamp Prank >
      • TEST
    • Yoda FM
    • Retro Player
    • LED Pixel Art
    • TARDIS
    • Battleships
    • LED Board
    • Night Vision
    • Enviro+ Weather
  • Minecraft
    • Minecraft API
    • Minecraft Sweeper
    • PiGlove: Minecraft Power Up
    • Minecraft Photo-booth
    • Rendering Pixels
    • Speed Cube
    • Lucky Dip
  • Computing
    • Why Computing?
    • Can You Compute
    • micro:bit
    • Coding Resources
    • Learn to Code >
      • Coding with iPads
      • Apps Creation Tools
      • sKratchInn
      • Sound Editing
    • Cheat Sheets
    • Theory
    • HOUR OF CODING
    • BEBRAS Computing Challange
    • Computer Facts
    • Free Software and Links
  • Contact Me
  • Random Hacks

LED Pixel Art


Images are made up of pixels which is short for picture element. Each one of these pixels consists of a red, a green and a blue value.  The values for each colour can range between 0 and 255, giving a combination of over 16 million different colours and shades. The Python Imaging Library (PIL) can be used to scan a picture, pull out each pixel and identify the RGB values for that pixel.  In this project I wrote a program to take a 10 X 10 image, identify the colour values for each pixel and then right these values to the LED display board.  It loads an image, scans it, and then displays the image on the LED board, all in real time.     

How the Program Works part 1


The PIL makes it really easy to scan the image and retrieve the pixel values. The current Raspberry Pi OS comes pre-installed with PIL so you are ready to go.  Simply import the required modules and then use the programme code below to open an image file and then measure the width and height of the file before using the code to create a list to store the pixel values. I then stored the total number of pixels, in this case 100, in a variable called total. This is useful for ensuring that the image has the correct number of required pixels for the display board.

file = input("Please enter the name of the file ")
im = Image.open(file + ".jpg", "r") # name of file to scan
width, height = im.size
pixel_values = list(im.getdata())
print (pixel_values)
total = (len(pixel_values))
#pixel_list = pixel_values
print ("Found ", total, "Pixels")

Wiring up the LEDs



How the Program Works part 2


The 'total' variable is used in the first line of code to check if the image contains 100 pixels, if it does then it sets the first pixel to green.  This indicates to the user that the image has been successfully scanned and will be displayed. Now, you may have noticed that in this line of code the 255 for the green is first however in normal LED values red appears first. This was the a problem with my the hardware.  The LEDs are not wired in the normal RGB set up, instead they are GRB!! In order to solve this I had to create a new list called npc, and then use a for loop to parse over each of the pixel values, extract the green, red and blue values and then recombine them into the correct order, storing them into a variable called RGB_correct.  Next, the code writes these correct values back to the npc list.

​if total == 100:
    unicorn.set_pixel(9, 0, 255, 0, 0)
    unicorn.show()
    sleep(3)
    npc = [] # list to stored new RGB values New Pixel Colours

    # Sorts GRB values into RGB values
    for i in range(0, 100): 
        pos = pixel_values[i] # pull first RGB values from list
        green = pos[0] # pull out green value
        red = pos[1] # pull out red value
        blue = pos[2] # pull out blue value
        RGB_correct = (red, green, blue) 
        npc.append(RGB_correct) # adds correct RGB value to new pixel colour list
        #print (npc) #uncomment for testing

The 10 x 10 Images



How the Program Works part 3


The final section of the code uses an array to store each of the new pixel values in the correct position for the LED display. For example, the first set of RGB values stored in the npc_list are located in position 0.  In the array named, pic, this new pixel value is added to position npc[0], displaying the colour on this specific LED.  The array consists of the specific location of each of the 100 LEDs and includes the colour value.  The code then writes this data directly back to the LED board using the code unicorn.set_pixels(pic).   If the image is greater than 100 pixels then the ELSE statement displays an error message and the first pixel is set to the colour red to indicate the error.

​
​    pic =  [[npc[9],npc[8],npc[7],npc[6],npc[5],npc[4],npc[3],npc[2],npc[1],npc[0]],
            [npc[19],npc[18],npc[17],npc[16],npc[15],npc[14],npc[13],npc[12],npc[11],npc[10]],
            [npc[29],npc[28],npc[27],npc[26],npc[25],npc[24],npc[23],npc[22],npc[21],npc[20]],
            [npc[39],npc[38],npc[37],npc[36],npc[35],npc[34],npc[33],npc[32],npc[31],npc[30]],
            [npc[49],npc[48],npc[47],npc[46],npc[45],npc[44],npc[43],npc[42],npc[41],npc[40]],
            [npc[59],npc[58],npc[57],npc[56],npc[55],npc[54],npc[53],npc[52],npc[51],npc[50]],
            [npc[69],npc[68],npc[67],npc[66],npc[65],npc[64],npc[63],npc[62],npc[61],npc[60]],
            [npc[79],npc[78],npc[77],npc[76],npc[75],npc[74],npc[73],npc[72],npc[71],npc[70]],
            [npc[89],npc[88],npc[87],npc[86],npc[85],npc[84],npc[83],npc[82],npc[81],npc[80]],
            [npc[99],npc[98],npc[97],npc[96],npc[95],npc[94],npc[93],npc[92],npc[91],npc[90]]]
           
    unicorn.set_pixels(pic)
    unicorn.show()

else:
    print ("Image Too Large")
    unicorn.set_pixel(9, 0, 0, 255, 0)
    unicorn.show()
Picture

Download the Code


Picture
Powered by Create your own unique website with customizable templates.