Bavarian State Painting Collection
An example notebook retreiving a sample of nine paintings via Wikidata from the Bavarian State Painting Collection. Here is the SPARQL query used in the Code section below. https://w.wiki/6VCz.
The notebooks is a sample 9 paintings from the Baroque period.
The complete collection is here on Wikidata.
The below Python code uses SPARQLWrapper to retrieve data from Wikidata based on a SPARQL query.
Code
from SPARQLWrapper import SPARQLWrapper, JSON
from PIL import Image
import requests
# VARIABLES
= 'https://query.wikidata.org/bigdata/namespace/wdq/sparql'
sparql_endpoint_url = 'https://www.wikidata.org'
wikibase_url = '/w/api.php'
api_url
# Wikidata requires a user agent header to prevent spam requests
= 'Ex_Books_conference_bot/0.0 (https://github.com/SimonXIX/Experimental_Books_workshop; ad7588@coventry.ac.uk)'
user_agent
# SPARQL query
# see in Wikidata's Query Service GUI at:
# https://w.wiki/6VCz
= """
query #defaultView:ImageGrid
SELECT ?item ?itemLabel ?inceptionyear ?creator ?creatorLabel ?copyright ?copyrightLabel ?image
WHERE
{
# find items which:
# are instances of (wdt:P31) paintings (wd:Q3305213)
# have the property (wdt:P195) of being in collection wd:Q812285 (Bavarian State Painting Collections)
?item wdt:P31 wd:Q3305213 .
?item wdt:P195 wd:Q812285 .
# get the item's creator property (wdt:P170)
?item wdt:P170 ?creator .
# get the item's image property (wdt:P18)
?item wdt:P18 ?image .
# get the item's copyright status (wdt:P6216)
?item wdt:P6216 ?copyright .
{
?item wdt:P571 ?inception.
BIND(YEAR(?inception) AS ?inceptionyear)
}
# filter out all paintings not created between the years 1600 and 1700
FILTER((1600 <= ?inceptionyear) && (?inceptionyear < 1700 ))
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". } }
# limit to nine results
LIMIT 9
"""
# SUBROUTINES
def get_delay(date):
try:
= datetime.datetime.strptime(date, '%a, %d %b %Y %H:%M:%S GMT')
date = int((date - datetime.datetime.now()).total_seconds())
timeout except ValueError:
= int(date)
timeout return timeout
def get_image(url, headers):
= requests.get(url, headers=headers, stream=True)
r if r.status_code == 200:
= Image.open(r.raw)
im return im
if r.status_code == 500:
return None
if r.status_code == 403:
return None
if r.status_code == 429:
= get_delay(r.headers['retry-after'])
timeout print('Timeout {} m {} s'.format(timeout // 60, timeout % 60))
time.sleep(timeout)
get_image(url, headers)
# MAIN PROGRAM
# create SPARQL query
= SPARQLWrapper(sparql_endpoint_url, agent=user_agent)
sparql
# retrieve results and convert to JSON format
sparql.setQuery(query)
sparql.setReturnFormat(JSON)= sparql.query().convert()
result
# for each result, print various data fields
for item in result['results']['bindings']:
print('Wikidata link: ' + '[' + item['item']['value'] + ']' + '(' + item['item']['value'] + ')' + '\n')
print('Title: ' + item['itemLabel']['value'] + '\n')
print('Year: ' + item['inceptionyear']['value'] + '\n')
print('Creator: ' + item['creatorLabel']['value'] + '\n')
print('Copyright: ' + item['copyrightLabel']['value'] + '\n')
# get image from image URL and display resized version
=item['image']['value']
image_url= {'User-Agent': 'Ex_Books_conference_bot/0.0 (https://github.com/SimonXIX/Experimental_Books_workshop; ad7588@coventry.ac.uk)'}
headers = get_image(image_url, headers)
im 500, 500), Image.Resampling.LANCZOS)
im.thumbnail((
display(im)print('\n\n')
Wikidata link: http://www.wikidata.org/entity/Q29474642
Title: The Birth of Benjamin
Year: 1650
Creator: Francesco Furini
Copyright: public domain
Wikidata link: http://www.wikidata.org/entity/Q29474649
Title: A Cynical Philosopher
Year: 1650
Creator: Luca Giordano
Copyright: public domain
Wikidata link: http://www.wikidata.org/entity/Q29474651
Title: Solomon and the Queen of Sheba
Year: 1697
Creator: Luca Giordano
Copyright: public domain
Wikidata link: http://www.wikidata.org/entity/Q29477235
Title: Q29477235
Year: 1674
Creator: Antonio Triva
Copyright: public domain
Wikidata link: http://www.wikidata.org/entity/Q29477863
Title: Q29477863
Year: 1633
Creator: Guido Reni
Copyright: public domain
Wikidata link: http://www.wikidata.org/entity/Q29477898
Title: Still-Life with Books
Year: 1628
Creator: Jan Lievens
Copyright: public domain
Wikidata link: http://www.wikidata.org/entity/Q29480557
Title: Feast of Herod
Year: 1630
Creator: http://www.wikidata.org/.well-known/genid/3f945710e81609ba4bae458b2820460a
Copyright: public domain
Wikidata link: http://www.wikidata.org/entity/Q29480565
Title: Venus and Cupid
Year: 1625
Creator: Heinrich Bollandt
Copyright: public domain
Wikidata link: http://www.wikidata.org/entity/Q29480594
Title: Still-life with Parrot
Year: 1630
Creator: Georg Flegel
Copyright: public domain