美文网首页
极客编程日历2018桌面壁纸

极客编程日历2018桌面壁纸

作者: Q9LyMwam81H4 | 来源:发表于2018-01-31 15:34 被阅读0次

    图灵社区出版了一本极客编程日历“Happy Hacking 2018” ,实体已经售罄,但是提供了PDF电子版下载。我们可以编写脚本,把日历和桌面壁纸结合在一起。最终效果如图:

    编程日历桌面壁纸

    Python源代码:

    import datetime
    
    from wand.image import Image #pip install Wand
    
    PDF_SOURCE = '/home/pjheng/文档/code_calendar_2018.pdf[{}]' #下载的PDF日历路径
    BACKGROUND_SOURCE = '/usr/share/backgrounds/adapta/tealized.jpg' #使用的壁纸
    OUTPUT = '/home/pjheng/图片/turing.jpg' #生成的壁纸路径
    
    PAGE_OFFSET = 6 #周历从PDF文档的第7页开始
    MARGIN_LEFT = 200 #周历的左边距
    MARGIN_TOP = 200 #周历的上边距
    
    current_week = datetime.datetime.now().isocalendar()[1] #获取当前是第几周
    page = PAGE_OFFSET + current_week #获取周历在PDF文档中的页号
    
    with Image(filename=PDF_SOURCE.format(page), resolution=200) as calendar:
        with Image(filename=BACKGROUND_SOURCE) as background:
            background.composite_channel('default_channels', calendar, 'blend', MARGIN_LEFT, MARGIN_TOP)
            background.save(filename=OUTPUT)
    

    结合crontab(或者Windows的任务计划程序),将脚本设定为每周一零点执行即可。
    另外,有朋友遇到了Wand抛异常的问题,我分别在Python 3.6.3 + Wand 0.4.4 和 Python 3.4.3 + Wand 0.4.3 中测试,没有重现。
    还可以做一些有趣的扩展。比如绘制一个方框,框选下方的当天日期等等。

    相关文章

      网友评论

          本文标题:极客编程日历2018桌面壁纸

          本文链接:https://www.haomeiwen.com/subject/fywkzxtx.html