MediaWiki REST API docs
  Home | Discuss | Fork this guide on PAWS

Reusing free images from Wikimedia Commons

This guide uses the MediaWiki REST API to explore media files on Wikimedia Commons. Wikimedia Commons is a collection of over 60,000,000 freely usable media files, many of which are used in Wikipedia articles.

Wikimedia Commons is a great resource for freely reusable images, including photos, illustrations, and diagrams. To retreive an image using the REST API, first we'll seach for pages on Wikimedia Commons. Once we have the name of the file, we'll request the file and license information.

Let's say we want to find images of different types of birds. We can use the search endpoint to find images of a flamingo, limiting our search to the top result.

The search endpoint includes a thumbnail for each result, which we can use to preview the image.

To get more information about an image, we can use the file endpoint. This gives us the URL for the file in its preferred format along with the preferred dimensions.

When reusing files from Wikimedia Commons (or any freely licensed content), it's important to include the applicable license. An easy way to do this is to link to the file's page on Wikimedia Commons, provided in the file_description_url property.

Now that we can search for and retireve images from Wikimedia Commons, we can generate a set of images for a list of birds.

Note that getting the first search results from Wikimedia Commons won't always work. Search results may include gallery pages that represent a collection of files and don't have the same properties as file pages. To account for this in the example below, we've used a try and except block.

You should now be able to use the REST API search and file endpoints to reuse media files from Wikimedia Commons.

To fork, edit, and re-run this Jupyter Notebook, download the source, and upload to PAWS using your Wikimedia account.

For more information about these endpoints, see the API reference. To share your feedback on this tutorial, post a comment to the REST API discussion page.

Image credits:

This tutorial is licensed under the Creative Commons Attribution-ShareAlike License.