This year for Mother’s Day, I decided to take an old digital photo frame we had lying around the house, and throw in some improvements. Originally, the frame had a small amount of internal memory, and could store low-resolution images loaded in from a flash drive. The process of moving files over was tedious, and I wanted anyone in my family to be able to easily contribute to the rotation of photos. Going away from school, I thought it would be neat to be able to easily update the frame with new photos, no matter where my siblings were in the world.

Solution ● Github

Inside now is a Raspberry Pi running OSMC on an HD display, with a python script running in the background to download new photos from a dedicated Gmail account. From there, OSMC transitions through them all to display. The frame is also enabled with SSH at home, so while I’m at school, I can perform remote updates and mass file transfers to update large amounts of photos manually.

What I learned + potential improvements

This being one of my first personal side projects, interacting with the Gmail API for the first time resulted in a really satisfying final result. The ability of anyone to be able to either email the address, or even send a photo through SMS to it, and know that it would be displayed within the next 15 minutes, felt very impressive for the little python work required. Ideally, I would have preferred to run a more appropriate OS, and just manually create a GUI solution to iterating through the photos, as opposed to having to use everything that comes with OSMC, just to use its wallpaper feature. That being said, it’s funny to send YouTube videos to it, including active streams of live events. There’s also more ideas for improvements of the script located in the issues on my GitHub repo, and I’m always happy to view updates and requests.

Script

dlAttachments.py
  • python
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# Referenced http://stackoverflow.com/questions/348630/how-can-i-download-all-emails-with-attachments-from-gmail
# Make sure you have IMAP enabled in your gmail settings.
# See / create issues on github.com/bmitchinson/raspberry-pi-photo-frame

import sys
import imaplib
import email
import email.header
import datetime
import os

# All pictures downloaded to "/Attachments" by default
detach_dir = '.'
if 'Attachments' not in os.listdir(detach_dir):
os.mkdir('Attachments')

# Replace *username* and *password*
EMAIL_ACCOUNT = "*USERNAME*@gmail.com"
EMAIL_PASSWORD = "*PASSWORD*"
# Use a dedicated folder if desired, otherwise leave as "INBOX" to search inbox
EMAIL_FOLDER = "INBOX"

try:
imapSession = imaplib.IMAP4_SSL('imap.gmail.com')
typ, accountDetails = imapSession.login(EMAIL_ACCOUNT, EMAIL_PASSWORD)
if typ != 'OK':
print('Not able to sign in!')
raise
imapSession.select('INBOX')
typ, data = imapSession.search(None, 'ALL')
if typ != 'OK':
print('Error searching Inbox.')
raise
# Iterating over all emails
for msgId in data[0].split():
typ, messageParts = imapSession.fetch(msgId, '(RFC822)')
if typ != 'OK':
print('Error fetching mail.')
raise

emailBody = messageParts[0][1]
mail = email.message_from_bytes(emailBody)
for part in mail.walk():
if part.get_content_maintype() == 'multipart':
continue
if part.get('Content-Disposition') is None:
continue
fileName = part.get_filename()

if bool(fileName):
filePath = os.path.join(detach_dir, 'Attachments', fileName)
if not os.path.isfile(filePath) :
print(fileName)
fp = open(filePath, 'wb')
fp.write(part.get_payload(decode=True))
fp.close()
imapSession.close()
imapSession.logout()

except:
print('Not able to download all attachments.')

Gallery