Hi,
I’m trying to learn python while at the same time make something that may actually have some practical application for me. To that end, I have a Pi Zero WH and an InkyPHAT black display, from which I’m trying to make a clock which tells the time with barcodes (I didn’t say it would have a practical application for anyone else!).
At first I tried using Inkybar but I think that uses the old Inky libraries, and I’m nowehere near competent enough to update it. So I’ve been googling around and come up with something which uses pyBarcode to generate the barcode PNG (encoding a string from datetime.now() ), then PIL to resize and reduce the bit depth of the image. Then it uses inky to display the final barcode image.
I have 2 problems:
- The barcode seems to be coming out inverted. I’ve attached a picture of the resultant screen.
- The barcode isn’t readable. I’ve tried 2 approaches, generating a barcode using pybarcode defaults and then resizing with PIL, and trying to pass params into pybarcode to generate a small enough barcode in the first place - neither of them seems to produce a readable barcode.
My code is below, any (gentle, please) advice would be much appreciated!
===================================
import barcode
from barcode.writer import ImageWriter
import PIL
from PIL import Image
from inky import InkyPHAT
from datetime import datetime
#get date time
now = datetime.now()
dt_string = now.strftime("%d/%m/%Y:%H%M")
#initialise display
inky_display = InkyPHAT(“black”)
inky_display.set_border(inky_display.BLACK)
generate barcode
bar_class = barcode.get_barcode_class(‘code128’)
barcode = dt_string
writer=ImageWriter()
code128 = bar_class(barcode, writer)
save barcode as png
code128.save(‘timec128’, {“module_width”:0.1, “module_height”:6, “font_size”: 0, “text_distance”: 0, “quiet_zone”: 3})
#code128.save(‘timec128’)
#resize png
to_be_resized = Image.open(‘timec128.png’)
resize image for inkyPHAT
newSize = (212,104)
resized = to_be_resized.resize(newSize, resample=PIL.Image.NEAREST)
resized.save(‘timec128_resized.png’)
convert to black and white (reduce bit depth)
b_w_image = resized.convert(“1”)
#save B&W image
b_w_image.save(‘timec128_resized_bw.png’)
#img = Image.open(‘timec128_resized.png’)
#send image to the inkyPHAT screen
inky_display.set_image(b_w_image)