I needed a few hundred photos of houses that I could use to train a model shipped inside a commercial app. Wikimedia Commons has tens of thousands of them. Most are licensed in a way I could not use, and one I kept had « CC-BY-SA 4.0 » written in its own file name.
TL;DR — Read the licence from the API’s extmetadata.LicenseShortName, allow only public domain, CC0 and CC BY with a named author, and reject everything else by default. Never trust the file name. Then deduplicate with a 64-bit dHash and review thumbnails on numbered contact sheets: the visual pass is where most candidates die.
The setup
The model is a small semantic segmentation network: for each pixel of a facade photo it predicts background, wall, roof, window, door or shutter. It ships in an Android app, so the training photos must allow commercial use and derivative works. That rules out ShareAlike (the trained weights could arguably be a derivative), NonCommercial and NoDerivatives.
I collected two rounds from Commons. The first gave 291 general house photos. The second targeted houses with visible swing shutters, because shutters were only 0.5 % of the pixels in the first round and the model was bad at them. Everything below comes from the second round unless stated.
What I expected
I expected that searching for « house with shutters » and filtering on a licence category would be enough. And I half-expected the file name to be a decent hint: plenty of uploaders put the licence in the title.
What actually happens
Plain text searches return mostly ShareAlike files and museum objects. The French word volets (shutters) gets stemmed to vol, so the search returned plane flights and dragonflies. Out of 35,752 unique files seen, the licence filter alone rejected 11,727 as SA, NC, ND or GFDL, 170 as missing or unknown, and 93 as CC BY with no machine-readable author.
The file name trap showed up in the first round. One kept photo, a 2023 picture of a listed house in northern Germany, has « CC-BY-SA 4.0 » at the end of its file name. The API says otherwise:
"LicenseShortName": {"value": "CC BY 4.0", "source": "commons-desc-page"}
and the raw description page carries a single licence template, {{cc-by-4.0}}. The template on the page is what Commons publishes as the licence; the file name is free text typed at upload. I kept the file but flagged it in the manifest. If you want to be stricter, drop any file whose name and template disagree; it costs almost nothing. In the second round, 0 of the 12,544 files that passed the filters had this mismatch.
The fix
Ask the API for the licence fields directly. This runs as is:
import json, urllib.parse, urllib.request
API = "https://commons.wikimedia.org/w/api.php"
p = {"action": "query", "format": "json", "formatversion": "2",
"prop": "imageinfo", "iiprop": "url|size|mime|extmetadata",
"iiextmetadatafilter": "LicenseShortName|LicenseUrl|Artist",
"generator": "categorymembers", "gcmtitle": "Category:Window shutters in France",
"gcmtype": "file", "gcmlimit": "5"}
req = urllib.request.Request(API + "?" + urllib.parse.urlencode(p),
headers={"User-Agent": "DatasetBuilder/0.1 (research)"})
for page in json.load(urllib.request.urlopen(req))["query"]["pages"]:
em = page["imageinfo"][0]["extmetadata"]
print(em.get("LicenseShortName", {}).get("value"), "|", page["title"])
Then classify with an allow-list, not a deny-list. Anything that is not clearly PD, CC0 or CC BY x.y falls through to a reject:
import re
def licence_kind(short, author):
low = short.lower()
if not short:
return "unknown"
if re.search(r"\b(sa|nc|nd)\b|share ?alike|non-?commercial|no ?deriv|gfdl|fair use", low):
return "forbidden"
if low.startswith("cc0") or low in ("cc-zero", "cc zero"):
return "cc0"
if low in ("public domain", "pd") or low.startswith(("pd-", "public domain")):
return "pd"
if re.fullmatch(r"cc[ -]by[ -]([1-4]\.[05])(?:[ -][a-z-]+)?", low):
return "ccby" if author else "by_no_author"
return "forbidden"
Two things made the candidate pool useful. First, cross every keyword with a licence template using CirrusSearch’s hastemplate: (for example maison volets hastemplate:Cc-zero); 59 keywords × 7 templates gave most of the good candidates. The template only targets the search; the LicenseShortName filter still decides. Second, cheap automatic filters before any download: JPEG only, at least 1000×700, aspect ratio between 1:1.6 and 2.2:1, a keyword blacklist (drawing, postcard, church, interior…), and at most two files per name series.
For duplicates, a difference hash on a 9×8 grayscale thumbnail, compared by Hamming distance:
def dhash(img, size=8):
g = np.asarray(img.convert("L").resize((size + 1, size), Image.LANCZOS), dtype=np.int16)
bits = (g[:, 1:] > g[:, :-1]).flatten()
return int("".join("1" if b else "0" for b in bits), 2)
def ham(a, b):
return bin(a ^ b).count("1") # duplicate if ham(...) <= 6
It caught 19 near-duplicates against the first round and among candidates, plus 30 files that were literally the same Commons page.

The last step was a human-style visual review: 16 thumbnails of 500 px per numbered sheet, keeping only whole facades with clearly visible shutters. 1,980 of 2,256 reviewed thumbnails were rejected there. A cap of 4 daytime photos per author then removed 41 more, so that a few prolific photographers do not dominate. Final count: 235 photos (108 PD or CC0, 127 CC BY). The first round ended at 291 from about 10,600 unique candidates.
Why it works
LicenseShortName is generated from the licence template on the file description page, which is the actual licence statement. File names, categories and search hits are all hints written by people. An allow-list also fails safe: a new or odd licence string (« No restrictions » from Flickr Commons, for example) lands in the reject bucket instead of slipping through. Keep the Artist and LicenseUrl fields in a manifest; you need them for the attribution screen.
Be polite to the servers. upload.wikimedia.org answered HTTP 429 several times, once with Retry-After: 600. Sequential requests, a descriptive User-Agent and at most one image request per second kept the job going.
What I did not test
This is a reading of licence metadata, not legal advice. I did not check whether « CC BY with an author assumed by Commons » (four files where Commons fell back to the uploader’s name) satisfies attribution in every jurisdiction. Night photos stayed rare: 5 of 235, against a target of 15 %. Under permissive licences, night shots on Commons are mostly lit monuments. The dataset leans heavily towards Western Europe and the United States.
The segmentation models trained on these photos are being released under CC BY-NC 4.0 on a public model page that is being set up now. One caveat: the network’s backbone starts from torchvision’s ImageNet weights, and ImageNet’s terms are for non-commercial research, so check that before reusing the weights commercially.
Facts
context: Collecting Wikimedia Commons photos to train a segmentation model shipped in a commercial app
problem: Most results are ShareAlike or non-commercial, and file names can state a different licence than the page
solution: Allow-list on extmetadata.LicenseShortName (PD, CC0, CC BY with author), hastemplate cross-searches, dHash dedup at Hamming <= 6, contact-sheet review
verified_on: 2026-10-03
applies_to: [MediaWiki Action API on commons.wikimedia.org, Python 3]
does_not_apply_to: [legal advice, images whose licence is only stated outside the file page]