{"id":2273,"date":"2020-06-21T18:40:37","date_gmt":"2020-06-21T17:40:37","guid":{"rendered":"http:\/\/www.dbonline.se\/?p=2273"},"modified":"2020-06-21T18:40:38","modified_gmt":"2020-06-21T17:40:38","slug":"raspberry-pi-digital-fotoram","status":"publish","type":"post","link":"https:\/\/www.dbonline.se\/index.php\/2020\/06\/21\/raspberry-pi-digital-fotoram\/","title":{"rendered":"Raspberry Pi Digital fotoram"},"content":{"rendered":"\n<p>Nedan presenteras min spaghettikod i python f\u00f6r att precentera bilder som ligger i \/boot\/foto\/ p\u00e5 en hdmi sk\u00e4rm.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import sys, os\nif sys.version_info&#91;0] == 2:\n    import Tkinter\n    tkinter = Tkinter\nelse:\n    import tkinter\nfrom PIL import Image, ImageTk\nimport time\nimport datetime\nimport touchphat\ntouchphat.all_off()\n\nif os.environ.get('DISPLAY','') == '':\n    print('no display found. Using :0.0')\n    os.environ.__setitem__('DISPLAY', ':0.0')\n\nroot = tkinter.Tk()\nw, h = root.winfo_screenwidth(), root.winfo_screenheight()\nroot.overrideredirect(1)\nroot.geometry(\"%dx%d+0+0\" % (w, h))\nroot.focus_set()\ncanvas = tkinter.Canvas(root,width=w,height=h)\ncanvas.pack()\ncanvas.configure(background='black')\n\ntextid = 0\nimageid = 0\nadd24h = 0\nadd1h = 0\nadd10m = 0\nadd1m = 0\nthen = 0\n\n@touchphat.on_touch(\"A\")\ndef handle_touch():\n    print(\"Button A pressed!\")\n    global add24h\n    add24h = add24h + 1\n\n@touchphat.on_touch(\"B\")\ndef handle_touch():\n    print(\"Button B pressed!\")\n    global add1h\n    add1h = add1h + 1\n\n@touchphat.on_touch(\"C\")\ndef handle_touch():\n    print(\"Button C pressed!\")\n    global add10m\n    add10m = add10m + 1\n\n@touchphat.on_touch(\"D\")\ndef handle_touch():\n    print(\"Button D pressed!\")\n    global add1m\n    add1m = add1m + 1\n\ndef showPIL(pilImage):\n    global textid\n    global imageid\n    global add24h\n    global add1h\n    global add10m\n    global add1m\n    if textid != 0:\n        print (\"removing old picture\")\n        canvas.delete(imageid)\n    else:\n        print (\"not removing old picture, no old picture\")\n\n    print (\"Calculating sceen for photo\")\n\n    imgWidth, imgHeight = pilImage.size\n#   resize photo to full screen\n    ratio = min(w\/imgWidth, h\/imgHeight)\n    imgWidth = int(imgWidth*ratio)\n    imgHeight = int(imgHeight*ratio)\n    pilImage = pilImage.resize((imgWidth,imgHeight), Image.ANTIALIAS)\n    image = ImageTk.PhotoImage(pilImage)\n    imagesprite = canvas.create_image((w\/2)+35,h\/2,image=image)\n\n    imageid = imagesprite\n\n    print (\"Sprite nr\",imagesprite)\n\n    t0 = time.time() # now (in seconds)\n    t1 = t0 + 60*60*10*add24h  # now + 12 houres\n    t1 = t1 + 60*60*add1h  # now + 60 minutes\n    t1 = t1 + 60*10*add10m # now + 10 min\n    t1 = t1 + 60*add1m # now + 1 min\n\n    print (\"Calculated time\")\n\n    current_time = time.strftime(\"%H:%M\",time.localtime(t1))\n    if textid != 0:\n        print (\"removing old clock\")\n        canvas.delete(textid)\n    else:\n        print (\"not removing old time, no old time\")\n    textid = canvas.create_text(w-180, h-40, font=(\"Purisa\", 80), text=current_time, fill=\"red\")\n    print (\"updating screen\")\n    root.update_idletasks()\n    root.update()\n#   root.bind(\"&lt;Escape>\", lambda e: (e.widget.withdraw(), e.widget.quit()))\n\nnames = os.listdir(\"\/boot\/foto\/\")\ni = 0\nwhile i &lt;= len(names):\n    now = time.time()\n    if now >= then+10:\n        if names&#91;i]&#91;-4:] == \".jpg\":\n            print(i)\n            print (len(names))\n            print (names&#91;i])\n            file=Image.open(\"\/boot\/foto\/\"+names&#91;i])\n            showPIL(file)\n            then = time.time()\n        i += 1\n        if i == len(names):\n            i = 0<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Nedan presenteras min spaghettikod i python f\u00f6r att precentera bilder som ligger i \/boot\/foto\/ p\u00e5 en hdmi sk\u00e4rm.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"enabled":false},"version":2}},"categories":[1],"tags":[],"class_list":["post-2273","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p8vDvB-AF","jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/www.dbonline.se\/index.php\/wp-json\/wp\/v2\/posts\/2273","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.dbonline.se\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.dbonline.se\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.dbonline.se\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.dbonline.se\/index.php\/wp-json\/wp\/v2\/comments?post=2273"}],"version-history":[{"count":1,"href":"https:\/\/www.dbonline.se\/index.php\/wp-json\/wp\/v2\/posts\/2273\/revisions"}],"predecessor-version":[{"id":2274,"href":"https:\/\/www.dbonline.se\/index.php\/wp-json\/wp\/v2\/posts\/2273\/revisions\/2274"}],"wp:attachment":[{"href":"https:\/\/www.dbonline.se\/index.php\/wp-json\/wp\/v2\/media?parent=2273"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dbonline.se\/index.php\/wp-json\/wp\/v2\/categories?post=2273"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dbonline.se\/index.php\/wp-json\/wp\/v2\/tags?post=2273"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}