Skip to main content

Flagpy - World Flag Identifier

Flagpy is a python library that allows the user to identify the flags of countries from all over the world. It also has support for seeing how similar two different flags are and finding which two countries have the most/least similar flag.

Installation

Flagpy can be installed simply using pip.

$ pip install flagpy

Usage

Identify

Flagpy can take in a url which links to an image and then determine which flag is being displayed in the image. It is most accurate when the flag takes up the whole image. The type of method to find the closest flag can be specified, but the default (and most accurate) method is "mse". Supported methods as of right now are "mse", "ssim", and "hash".

>>> import flagpy as fp
>>> fp.identify('https://upload.wikimedia.org/wikipedia/commons/thumb/d/d9/Flag_of_Canada_%28Pantone%29.svg/1200px-Flag_of_Canada_%28Pantone%29.svg.png')
'Canada'
>>> fp.identify('https://upload.wikimedia.org/wikipedia/commons/thumb/d/d9/Flag_of_Canada_%28Pantone%29.svg/1200px-Flag_of_Canada_%28Pantone%29.svg.png', method = 'ssim')
'Canada'
>>> fp.identify('https://upload.wikimedia.org/wikipedia/commons/thumb/d/d9/Flag_of_Canada_%28Pantone%29.svg/1200px-Flag_of_Canada_%28Pantone%29.svg.png', method = 'hash')
'Canada'

Closest/Farthest Flag

Flagpy can take in a name of a country and return which country's flag is most/least similar to the given country's flag. Once again, the method to find the most/least similar flag can be specified, but the default is "mse". The name of the country must match one of the items in flagpy's list of countries, which can be acquired with get_country_list().

>>> fp.get_country_list()
['Afghanistan', 'Albania', ... , 'Zambia', 'Zimbabwe']
>>> fp.closest_flag("India")
'Niger'
>>> fp.closest_flag('India', method = 'ssim')
'Luxembourg'
>>> fp.farthest_flag('India')
'Estonia'
>>> fp.farthest_flag('India', method = 'hash')
'Armenia'

Displaying Flags

Flagpy has support for both displaying a country's flag and storing it as an Image object.

>>> fp.display('Ivory Coast')

alt text

>>> img = fp.get_flag_img('Yemen')
>>> img.show()

alt text

Difference Between Flags

Flagpy allows the user to find the quantitative difference between two countries' flags. There are three possible methods to calculate this difference: mean-squared error (the default), structural similarity index measurement, and hash difference. For both mse and hash, smaller values indicate higher similarity between the two flags, and a difference of 0 means the images are identical. However, the hash difference will always be an integer. For ssim, the value must be between -1 and 1, so higher values indicate a higher similarity.

>>> fp.flag_dist('Denmark', 'Germany')
57595.77333333333
>>> fp.flag_dist('Denmark', 'Germany', method = 'ssim')
0.25852880532772604
>>> fp.flag_dist('Denmark', 'Germany', method = 'hash')
40

Flag DataFrame

Flagpy can supply the user with a pandas DataFrame of all the countries and their flag image (scraped from Wikipedia).

>>> df = fp.get_flag_df()
>>> df.shape
(195, 1)
>>> df["flag"].loc["The United States"]
array([[[ 51,  51, 102],
        [ 51,  51, 102],
        [ 51,  51, 102],
        ...
        [153,  51,  51],
        [153,  51,  51],
        [153,  51,  51]]]

Metadata

Release files for flagpy 1.0.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for flagpy 1.0.3
File Size Uploaded
flagpy-1.0.3.tar.gz 6.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for flagpy 1.0.3
File Interpreter ABI Platform
flagpy-1.0.3-py3-none-any.whl Python 3 none any Details

Total release size: 229.4 kB

Release files / flagpy-1.0.3.tar.gz

Download URL flagpy-1.0.3.tar.gz
Size 6.9 kB
Tags Source
SHA-256 checksum
How to use checksums
4888480241ade6a0e42b8192572034f370b9514675385a33583a57eb4fe26e97
BLAKE2b-256 checksum
How to use checksums
798d4cf508fdb9ead90266295356dde9d8834391bfbdbed0933bf1e351be8205
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.8.1

Release files / flagpy-1.0.3-py3-none-any.whl

Download URL flagpy-1.0.3-py3-none-any.whl
Size 222.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4dc9d1bf5d641188a1ab33607311041436102aa346999f1c6b6c30395342ab7f
BLAKE2b-256 checksum
How to use checksums
9055f97efd9128251a62de26b996b3470a5e00c15d4db38ab3382ed2d45e7c42
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.8.1

Release history Release notifications | RSS feed

This release

1.0.3 This release

2 release files

1.0.2

2 release files

1.0.1

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page