Skip to content

Instantly share code, notes, and snippets.

@davidedelpapa
Created May 15, 2020 08:46
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidedelpapa/a7eb8e9d269a2a65cd6ed8f6f62ddce8 to your computer and use it in GitHub Desktop.
Save davidedelpapa/a7eb8e9d269a2a65cd6ed8f6f62ddce8 to your computer and use it in GitHub Desktop.
First tutorial for gphotospy using Tk
import io
import base64
from tkinter import *
from urllib.request import urlopen
from gphotospy import authorize
from gphotospy.media import *
from gphotospy.album import *
from PIL import ImageTk, Image
# Select secrets file
CLIENT_SECRET_FILE = "gphoto_oauth.json"
# Get authorization and return a service object
service = authorize.init(CLIENT_SECRET_FILE)
# Construct the album manager
album_manager = Album(service)
# Get iterator over the list of albums
album_iterator = album_manager.list()
# Let's get the first album in the list
first_album = next(album_iterator)
# Let's get this album's id
first_album_id = first_album.get("id")
# Construct the media manager
media_manager = Media(service)
# Let's get a list of all the album's content
album_media_list = list(media_manager.search_album(first_album_id))
# Let's get the baseUrl of the first item
base_url = album_media_list[0].get("baseUrl")
# Let's format the baseUrl with parameters
# and prepare it for display within the Canvas
image_url = "{}=w300-h300".format(base_url)
# Let's open the image from the URL thus created
image_bytes = urlopen(image_url).read()
# Boilerplate for a Tk window with a canvas
root = Tk()
# giving some border to the image
canvas = Canvas(root, width=320, height=320, bg='white')
canvas.pack(side='top', fill='both', expand='yes')
# We create a PIL Image with our raw image
img = Image.open(io.BytesIO(image_bytes))
# Now we prapare the image to be displayed,
# then we display it to the Tk window
photo = ImageTk.PhotoImage(img)
canvas.create_image(10, 10, image=photo, anchor='nw')
# In a script it needs to be finalized with the main loop
root.mainloop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment