Search code examples
wikipedia-apimediawiki-api

How to get the default image of Wikipedia article?


Is it possible to get the (default) image associated with a Wikipedia article with an API call if only the URL of the article is known?

Is it possible to make constraints about the image size / resolution in the call (as an image is usually available in different resolutions)?

Is it possible to request the largest image version not larger than MAX_X px?

An example:

https://upload.wikimedia.org/wikipedia/commons/e/e9/Jaguar_%28Panthera_onca_palustris%29_female_Piquiri_River_2.JPG

is 4769x3179 large (1,5 ratio).

A request limiting the MAX_X size to 3000 would result in an image scaled to 3000x2000.

A request limiting the MAX_X size to 5000 would result in the image of the original size (4769x3179) because MAX_X is bigger than the image's x size.


Solution

  • Use MediaWiki API with pageimages. For example for Wikipedia article Jaguar and requested max image size 500 the query will be:

    https://en.wikipedia.org/w/api.php?action=query&prop=pageimages&titles=Jaguar&pithumbsize=500
    

    From the response you can get also the largest image version, just remove the thumb part and everything from the pixels to the end of the link.