Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This question is related to data analysis introduced here

Why do some flag images from the CountryData dataset render at very low resolution both in Front End and when exported in PDF? (Here Philippines and UnitedKingdom as examples: flag images were not searched exhaustively).

enter image description here

enter image description here

To reproduce the flag images, use the following code:

# -> Image[CountryData[#, "Flag"], ImageSize -> 20] & /@ CountryData[]

I observe this in MMA 8.0.4 on OSX 10.7.3

share|improve this question
possibly relevant (although, I don't know how): Sources for CountryData… – R. M. Sep 25 '12 at 3:52
@ℛ.ℳ +1 for (possibly I don't know how) – Dr. belisarius Sep 25 '12 at 4:03
I think this must be some kind of caching or access problem here. Perhaps you can flush the packlet that holds the CountryData and try again? – Mr.Wizard Sep 25 '12 at 4:38
@Mr.Wizard, how? – alancalvitti Sep 25 '12 at 4:40
it's 2012, who watches tv.. . but ok, will do – alancalvitti Sep 25 '12 at 5:36
up vote 21 down vote accepted


Your (paraphrased) question was "Why do some of the CountryData flags render so badly?". I take the liberty to answer a much wider question, namely "How can the rendering of all flags be improved?".

The main problem is that all bitmaps are so measly small (and they are bitmaps). What I would like to have is that CountryData[country, "Flag"] would yield vector graphics flags that look good at any scale.

You don't only need nice flags as small tags and symbols like in your example, they're cool for graphics backdrops too, but they shouldn't look like this (with the default CountryData flags):

Mathematica graphics

but like this (the end result of my little exercise):

Mathematica graphics

The search

So I went looking for flags in a vector format that Mathematica could read and that are freely distributable. You end up with EPS, AI, and SVG formats. Mathematica imports EPS and AI (which is close to EPS; the ability to import this format is not listed AFAIK). SVG files can only be exported not imported.

There are lots of sites offering free flags (such as, but they are mostly incomplete (my target is to cover all 240 countries in CountryData["countries"]), and have inconsistent flag drawing qualities (some are waving flags, 3D flags, or are plain wrong) and/or have a naming scheme that makes automated harvesting impossible.

I then turned to Wikipedia whose media is usually freely distributable. It offers flags of all the 240 countries I was looking for, but they are in SVG format. Another problem is the naming scheme. From the country page it takes three clicks to finally get at the actual SVG file. This took quite some html importing and string matching magic, but in the end I had my list of 240 countries and the links to the Wikipedia flags. It's a large list (named allCountryWikiFlags) and I'll include it at the end of this post.

Getting the flags

First I create some directories for my flags in various formats:

CreateDirectory[FileNameJoin[{$UserDocumentsDirectory, #}]] & /@ 
                {"FlagsSVG","FlagsEPS", "FlagsPNG"}

Now, using binary Import and Export download all the flags:

  FileNameJoin[{$UserDocumentsDirectory,"FlagsSVG", #[[1]] <> ".svg"}], 
  Import[#[[2]], "Byte"], 
] & /@ allCountryWikiFlags;


So, now we have 240 SVG flags that we can't import into Mathematica. Next step is to have them converted to EPS which Mathematica can import. I wrote a batch script in Adobe Illustrator to do this, but I wasn't really pleased with the results; besides I wanted a solution anybody could use. I therefore downloaded Inkscape, a free and very good SVG drawing program, available for all major OS-es. It can be called from a command line and I planned to have Mathematica do this.

I used the portable version, which doesn't require installation. The following is the path to the executable. If you want to try this, change this line to the path in your situation.

inkscapePortablePath = "C:\\Users\\Sjoerd\\Desktop\\InkscapePortable\\InkscapePortable";

Now run Inkscape to convert the SVGs to EPS (and PNG) controlled by Mathematica. I don't use Run here. Run works without problems, but using Read prevents the command line box popping up (tip from Mr.Wizard here):


Read["!" <> inkscapePortablePath <> " -f \""
     <> FileNameJoin[{$UserDocumentsDirectory, 
           "FlagsSVG", # <> ".svg"}]
         <> "\" -e \"" <> 
         FileNameJoin[{$UserDocumentsDirectory, "FlagsPNG", # <> ".png"}]
     <> "\" -w 600"
    ] & /@ allCountryWikiFlags[[All, 1]];


Read["!" <> inkscapePortablePath <> " -f \""
     <> FileNameJoin[{$UserDocumentsDirectory, 
           "FlagsSVG", # <> ".svg"}]
         <> "\" -E \"" <> 
         FileNameJoin[{$UserDocumentsDirectory, "FlagsEPS", # <> ".eps"}]
    ] & /@ allCountryWikiFlags[[All, 1]];

This takes a few minutes and then you'll have 240 EPS and PNG files. The reason I generate PNG as well is to check for EPS import errors in Mathematica.

Some results

CountryData["Argentina","Flag"] :

Mathematica graphics

Wiki flag:

Mathematica graphics

CountryData["Spain", "Flag"] :

Mathematica graphics

Wiki flag:

Mathematica graphics


Unfortunately, many EPS flags do not import well in Mathematica.

This is the EPS flag of Bosnia and Herzegovina:

Mathematica graphics

Note the stars. They should have looked as follows:

Mathematica graphics

This is due to the fact that Mathematica's FilledCurve that is often used in imported EPS files handles self intersecting polygons different from EPS.

I found the following country flags to have problems like that:

{"Algeria", "Bermuda", "Bosnia and Herzegovina", "Brazil", "Chile", 
"China", "East Timor", "Egypt", "French Guiana", "Georgia", "Ghana", 
"Gibraltar", "Iceland", "Isle of Man", "Jamaica", "Kosovo", 
"Kyrgyzstan", "Libya", "Madagascar", "Mauritania", "Montserrat", 
"Nepal", "Pakistan", "Papua New Guinea", "Pitcairn Islands", "Puerto 
Rico", "Samoa", "Slovakia", "South Africa", "South Sudan", "Sudan", 
"Suriname", "Tajikistan", "Tanzania", "Turkey", "United Kingdom", 

Some even failed to load:

{"Belize", "British Virgin Islands", "Cayman Islands", "Ecuador", 
"Falkland Islands", "Mexico", "Réunion", "Saint Pierre and Miquelon", 
"São Tomé and Príncipe", "Turks and Caicos Islands", "Vatican City"}

Various other problems I found with {"Cook Islands", "Greenland", "New Zealand", "Niue", "Tuvalu", "Wallis and Futuna Islands"}.

Solving problems

A solution could be to skip EPS in favor of PNG versions in the case of the exceptions listed above. The PNGs are very good and I actually like them better in many cases as I feel the line thickness Mathematica uses in importing is a tad too much. They don't take up more space either. The PNG and EPS folders are of about the same size.

If you decide to use PNGs you could actually also use the ones that you can find on Wikipedia and skip the Inkscape conversion process altogether. With some massaging you can turn the link list found below in a list suitable for downloading the various resolution versions of the flag available on Wikipedia (200, 500, 1000, 2000 pixels).

The following automates the download process:

CreateDirectory[FileNameJoin[{$UserDocumentsDirectory, "Flags1000pxPNG"}]]

Export[FileNameJoin[{$UserDocumentsDirectory,"Flags1000pxPNG", #[[1]] <> ".png"}], 
      "commons" ~~ "/" ~~ x__ ~~ "/" ~~ y__ ~~ "/" ~~ name__ ~~ 
        "svg" :> 
       "commons/thumb/" <> x <> "/" <> y <> "/" <> name <> 
        "svg/1000px-" <> name <> "svg.png"], "Byte"], "Byte"] & /@ 

The list with flag hyperlinks

The first field in each row is Mathematica's CountryData["Countries","Name"] name. The second field is the Wikipedia link.

(Here, the list was supposed to be, but it happened to push the post over Stackexchange's character limit of 30,000 characters. To the rescue, a trick that Belisarius and I have been preparing for some time, which involves simply executing the following line. Like magic, you will get a notebook with the hyperlinks in it.)

NotebookPut@ImportString[Uncompress@FromCharacterCode@Flatten@ImageData[Import@ "","Byte"],"NB"] 
share|improve this answer
very nice! ${}{}$ – R. M. Sep 30 '12 at 23:31
wow... the encoded image loads like a charm! :) – Dr. belisarius Oct 1 '12 at 2:27
If this was done in free time allowed by the recent absence of your ♦ I think the site will benefit from the change. This is great! – Mr.Wizard Oct 1 '12 at 8:36
@mr.wizard Thanks! It certainly helps. During the weekend this answer took so much time that I couldn't check the new questions list at all. Going into dead-ends like the vector art site was especially time-consuming and developing and testing the web-scraping code took some time too. I'm not sure if I have actually saved time automating it all (compared to clicking and copying myself) but it prevented a lot of boredom, made me learn a few things, and is useful for later. Unfortunatly the end result (the EPS flags) isn't perfect. Perhaps a question on post-processing EPS imports would be good. – Sjoerd C. de Vries Oct 1 '12 at 9:53

The thought that occurs to me is that the images of the flags of the Philippines and United Kingdom are among the smallest in the CountryData set.

flags = {#, CountryData[#, "Flag"]} & /@ CountryData[];

You could extract the ImageSize specification from the end of the flag's data - most are {128,x} but a few are smaller:

(* "Switzerland" *)
(* {128,128} *)
(* "UnitedKingdom" *)
(* {110,55} *)

Or try running through looking for the shortest raster array used:

Sort[flags, Length[First[First[Last[#1]]] ] < 
   Length[First[First[Last[#2]]]] &]

smallest flags

Is it possible that the process of applying Image to Graphics[Raster[...]] data introduces bad scaling effects in certain situations or for flags with small raster arrays and/or small ImageSize specifications?

share|improve this answer

As I answered in your previous question, yours is a localized problem, so we need all available data about your configuration to repro it. For example, in my machine there is no such effect:

cf = CountryData[#, "Flag"] & /@ {"Argentina", "United Kingdom", "Philippines", "Togo", "Vatican"};
BarChart[Range[5], ChartLabels -> (Image[#, ImageSize -> {50}] & /@ cf)]

Mathematica graphics

share|improve this answer
I can repro his problem. I think it's just because the diagonal lines don't scale up/down well. You can see in {#, Image[CountryData[#, "Flag"], ImageSize -> 200]} & /@ CountryData[] // Grid that those flags with curves/diagonal lines do worse at scaling (as expected) – R. M. Sep 25 '12 at 4:04
@R.M. that can't the complete reason. Look at the flags of Australia and New Zealand which contain the Union Jack as "inserts" or South Africa (shown above immediately to the left of UK) yet render with no aliasing. Same for Jamaica, or even flags with curved elements like Turkey . – alancalvitti Sep 25 '12 at 4:11
@ℛ.ℳ Those problems aren't comparable to the ones reported by the OP. And sorry if I don't answer some of your comments in the future, but your new curved self is difficult to type. – Dr. belisarius Sep 25 '12 at 4:26
@alan, the term of art you want is canton, as in "the Union Jack is in the canton of the Australian and New Zealand flags". – J. M. Sep 25 '12 at 11:15

GoSquared has made available a 2400 flag icon set

enter image description here

Here is how you can quickely grab the 64px versions:

flags = Import["", 
countries = 
 StringDrop[#, 9] & /@ (Rest@
     StringCases[#, "flags/64/" ~~ __] & /@ 
 FileNameJoin[{$UserDocumentsDirectory, "Flags64pxPNG"}]]
    MapThread[Export, {FileNameJoin[{$UserDocumentsDirectory, 
      "Flags64pxPNG", #[[1]]}] & /@ countries, flags}]

for fun here is the average flag of 242 countries

Image@Mean[ImageData /@ flags]

Mathematica graphic

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.