""" inkycal_today unittest """ import logging import unittest from inkycal.modules import Today from inkycal.modules.inky_image import Inkyimage, image_to_palette from tests import Config merge = Inkyimage.merge sample_url = Config.SAMPLE_ICAL_URL logger = logging.getLogger(__name__) logging.basicConfig(level=logging.DEBUG) # /workspaces/Inkycal/venv/bin/python3 -m pytest tests/test_inkycal_today.py::TestToday::test_generate_image -v -s tests = [ { "name": "Today", "config": { "size": [528, 343], "week_starts_on": "Monday", "show_events": True, "ical_urls": sample_url, "ical_files": None, "date_format": "D MMM", "time_format": "HH:mm", "padding_x": 10, "padding_y": 10, "fontsize": 14, "language": "zh", "font": "NotoSansCJKsc-Regular", } }, ] class TestToday(unittest.TestCase): def test_generate_image(self): output_dir = Config.OUTPUT_DIR import os from PIL import Image, ImageDraw import numpy as np os.makedirs(output_dir, exist_ok=True) test_num = 0 for test in tests: print(f'test {tests.index(test) + 1} generating image..', end="") module = Today(test) im_black, im_colour = module.generate_image() print('OK') # 创建最终的三色图像 # 在 E-Paper 上: im_black 的黑色像素显示黑色, im_colour 的黑色像素显示红色 result = Image.new('RGB', im_black.size, 'white') result_array = np.array(result) black_array = np.array(im_black) colour_array = np.array(im_colour) # 使用阈值处理抗锯齿:灰度值 < 128 的视为"黑色" # im_black 上的深色区域 -> 黑色 (0, 0, 0) black_gray = black_array[:,:,0] # 取灰度值(RGB相同) black_mask = black_gray < 128 result_array[black_mask] = [0, 0, 0] # im_colour 上的深色区域 -> 红色 (255, 0, 0) colour_gray = colour_array[:,:,0] colour_mask = colour_gray < 128 result_array[colour_mask] = [255, 0, 0] # 保存最终图像 final_image = Image.fromarray(result_array) output_path = os.path.join(output_dir, f"today_test_{test_num}.png") final_image.save(output_path) print(f' -> Saved to {output_path}') # 统计颜色 red_count = np.sum(colour_mask) black_count = np.sum(black_mask) print(f' 🔴 Red pixels: {red_count}, ⚫ Black pixels: {black_count}') test_num += 1 if Config.USE_PREVIEW: final_image.show()