Giter Site home page Giter Site logo

Comments (8)

Gadgetoid avatar Gadgetoid commented on June 1, 2024

It looks like you've invoked one of Python's more irritating quirks by creating a file called calendar.py alongside calendar-phat.py.

When calendar-phat.py tries to import calendar - a library for... doing calendar stuff 😆 instead it finds calendar.py alongside the Python file and tries to import that instead.

Basically- never name a Python file after a library you want to import, or might want to import in another Python file in the same directory and you'll be fine.

from inky.

kompas1 avatar kompas1 commented on June 1, 2024

okay thanks, that actually solved the problem, makes sense actually, i don't know why i didn't came up with checking that on my own 🤦‍♂️ . At least the first one so now it's time for me to try the second part. 😄

from inky.

kompas1 avatar kompas1 commented on June 1, 2024

@Gadgetoid but another thing i noticed is, that I still can see some of the older things i put on the screen. Example from just now: I put on the weather-phat.py just to check it, worked easy. Now i put on the calendar-phat.py, also works fine but i can still see some data from the weather-phat.py in the 'background'. Will it stay like that or does it just take some time to adjust? Because I also wiped the whole screen with the clean.py but there is still some 'background' visible

from inky.

kompas1 avatar kompas1 commented on June 1, 2024

So heres my try of removing the backdrop.png from the calendar file:

#!/usr/bin/env python

-- coding: utf-8 --

import os
import datetime
import calendar
import argparse

from PIL import Image, ImageDraw

from inky import InkyPHAT

print("""Inky pHAT: Calendar
Draws a calendar for the current month to your Inky pHAT.
This example uses a sprite sheet of numbers and month names which are
composited over the background in a couple of different ways.
""")

Get the current path

PATH = os.path.dirname(file)

Command line arguments to set display type and colour

parser = argparse.ArgumentParser()
parser.add_argument('--colour', '-c', type=str, required=True, choices=["red", "black", "yellow"], help="ePaper display colour")
args = parser.parse_args()

colour = args.colour

Set up the display

inky_display = InkyPHAT(colour)
inky_display.set_border(inky_display.BLACK)

Uncomment the following if you want to rotate the display 180 degrees

inky_display.h_flip = True

inky_display.v_flip = True

def create_mask(source, mask=(inky_display.WHITE, inky_display.BLACK, inky_display.RED)):
"""Create a transparency mask.
Takes a paletized source image and converts it into a mask
permitting all the colours supported by Inky pHAT (0, 1, 2)
or an optional list of allowed colours.
:param mask: Optional list of Inky pHAT colours to allow.
"""
mask_image = Image.new("1", source.size)
w, h = source.size
for x in range(w):
for y in range(h):
p = source.getpixel((x, y))
if p in mask:
mask_image.putpixel((x, y), 255)

return mask_image

def print_digit(position, digit, colour):
"""Print a single digit using the sprite sheet.
Each number is grabbed from the masked sprite sheet,
and then used as a mask to paste the desired colour
onto Inky pHATs image buffer.
"""
o_x, o_y = position

num_margin = 2
num_width = 6
num_height = 7

s_y = 11
s_x = num_margin + (digit * (num_width + num_margin))

sprite = text_mask.crop((s_x, s_y, s_x + num_width, s_y + num_height))

img.paste(colour, (o_x, o_y), sprite)

def print_number(position, number, colour):
"""Print a number using the sprite sheet."""
for digit in str(number):
print_digit(position, int(digit), colour)
position = (position[0] + 8, position[1])

Load our sprite sheet and prepare a mask

text = Image.open(os.path.join(PATH, "resources/calendar.png"))
text_mask = create_mask(text, [inky_display.WHITE])

Note: The mask determines which pixels from our sprite sheet we want

to actually use when calling img.paste().

See: http://pillow.readthedocs.io/en/3.1.x/reference/Image.html?highlight=paste#PIL.Image.Image.paste

Load our backdrop image

draw = ImageDraw.Draw()

Grab the current date, and prepare our calendar

cal = calendar.Calendar()
now = datetime.datetime.now()
dates = cal.monthdatescalendar(now.year, now.month)

col_w = 20
col_h = 13

cols = 7
rows = len(dates) + 1

cal_w = 1 + ((col_w + 1) * cols)
cal_h = 1 + ((col_h + 1) * rows)

cal_x = inky_display.WIDTH - cal_w - 2
cal_y = 2

Paint out a black rectangle onto which we'll draw our canvas

draw.rectangle((cal_x, cal_y, cal_x + cal_w - 1, cal_y + cal_h - 1), fill=inky_display.BLACK, outline=inky_display.WHITE)

The starting position of the months in our spritesheet

months_x = 2
months_y = 20

Number of months per row

months_cols = 3

The width/height of each month in our spritesheet

month_w = 23
month_h = 9

Figure out where the month is in the spritesheet

month_col = (now.month - 1) % months_cols
month_row = (now.month - 1) // months_cols

Convert that location to usable X/Y coordinates

month_x = months_x + (month_col * month_w)
month_y = months_y + (month_row * month_h)

crop_region = (month_x, month_y, month_x + month_w, month_y + month_h)

month = text.crop(crop_region)
month_mask = text_mask.crop(crop_region)

monthyear_x = 28

Paste in the month name we grabbed from our sprite sheet

img.paste(inky_display.WHITE, (monthyear_x, cal_y + 4), month_mask)

Print the year right below the month

print_number((monthyear_x, cal_y + 5 + col_h), now.year, inky_display.WHITE)

Draw the vertical lines which separate the columns

and also draw the day names into the table header

for x in range(cols):
# Figure out the left edge of the column
o_x = (col_w + 1) * x
o_x += cal_x

crop_x = 2 + (16 * x)

# Crop the relevant day name from our text image
crop_region = ((crop_x, 0, crop_x + 16, 9))
day_mask = text_mask.crop(crop_region)
img.paste(inky_display.WHITE, (o_x + 4, cal_y + 2), day_mask)

# Offset to the right side of the column and draw the vertical line
o_x += col_w + 1
draw.line((o_x, cal_y, o_x, cal_h))

Draw the horizontal lines which separate the rows

for y in range(rows):
o_y = (col_h + 1) * y
o_y += cal_y + col_h + 1
draw.line((cal_x, o_y, cal_w + cal_x - 1, o_y))

Step through each week

for row, week in enumerate(dates):
y = (col_h + 1) * (row + 1)
y += cal_y + 1

# And each day in the week
for col, day in enumerate(week):
    x = (col_w + 1) * col
    x += cal_x + 1

    # Draw in the day name.
    # If it's the current day, invert the calendar background and text
    if (day.day, day.month) == (now.day, now.month):
        draw.rectangle((x, y, x + col_w - 1, y + col_h - 1), fill=inky_display.WHITE)
        print_number((x + 3, y + 3), day.day, inky_display.BLACK)

    # If it's any other day, paint in as white if it's in the current month
    # and red if it's in the previous or next month
    else:
        print_number((x + 3, y + 3), day.day, inky_display.WHITE if day.month == now.month else inky_display.RED)

Display the completed calendar on Inky pHAT

inky_display.show()

but the output then is:
Traceback (most recent call last):
File "cleancal.py", line 95, in
draw = ImageDraw.Draw()
TypeError: Draw() takes at least 1 argument (0 given)

why is this happening? I mean when the backdrop.png is used there is no error but if i remove the few lines where the backdrop.png is getting loaded it somehow doesn't work out and now i am confused and which arguments can i use to overcome this?

from inky.

Gadgetoid avatar Gadgetoid commented on June 1, 2024

You'll need to put that code paste in between two sets of ``` tags so I can make head or tail of it. Like this:

```
Your code here
```

That should prompt GitHub to format it as code rather than markdown.

Some after-image is expected on Inky and can vary depending on ambient temperature and what you're displaying. It ultimately uses the very messy practise of moving ink up and down in a fluid suspension so it's decidedly and unpredictably analog.

from inky.

kompas1 avatar kompas1 commented on June 1, 2024

@Gadgetoid I figured it out I just painted the png full black 😅 It worked out fine for me. But yes I will from now on change the layout from the code when i post it. Thanks for the tip

from inky.

kompas1 avatar kompas1 commented on June 1, 2024

Oh and I am not sure if i can still put that in but I tried to 'crontab' it for a specific time but if I use cron for executing the command i only get the following error in the log:

Traceback (most recent call last):
File "/home/pi/Pimoroni/inky/examples/phat/calendar-phat.py", line 83, in
text = Image.open("resources/calendar.png")
File "/usr/lib/python2.7/dist-packages/PIL/Image.py", line 2634, in open
fp = builtins.open(filename, "rb")
IOError: [Errno 2] No such file or directory: 'resources/calendar.png'

even though it works fine when i enter the directory and run it manually, but not as a cron job

from inky.

Gadgetoid avatar Gadgetoid commented on June 1, 2024

For crontab you must make sure you cd into the directory with calendar-phat.py before executing the code.

I usually do this by creating a start.sh and adding that to crontab instead.

The contents of start.sh would look something like:

#!/bin/bash
cd /home/pi/Pimoroni/inky/examples/phat/
python calendar-phat.py

Now the Python script will run in the proper directory and be able to find its resources.

from inky.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.