Skip to main content

graphics - Why do some CountryData flags render at low resolution?


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



Answer




The discussion in this answer pertains to older versions of Mathematica which had low-resolution bitmaps of country flags. In my current version (11), the flags now are very detailed vector graphics, making the below superfluous.




Introduction


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 www.vectorportal.com), 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:


Export[
FileNameJoin[{$UserDocumentsDirectory,"FlagsSVG", #[[1]] <> ".svg"}],
Import[#[[2]], "Byte"],

"Byte"
] & /@ allCountryWikiFlags;

Conversion


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):


PNG:



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

EPS:


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


Alas


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",
"Zimbabwe"}

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"}],
Import[StringReplace[#[[2]],
"commons" ~~ "/" ~~ x__ ~~ "/" ~~ y__ ~~ "/" ~~ name__ ~~
"svg" :>
"commons/thumb/" <> x <> "/" <> y <> "/" <> name <>

"svg/1000px-" <> name <> "svg.png"], "Byte"], "Byte"] & /@
allCountryWikiFlags;

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@ "http://i.stack.imgur.com/sc7li.png","Byte"],"NB"] 

Comments

Popular posts from this blog

plotting - How to draw lines between specified dots on ListPlot?

I would like to create a plot where I have unconnected dots and some connected. So far, I have figured out how to draw the dots. My code is the following: ListPlot[{{1, 1}, {2, 2}, {3, 3}, {4, 4}, {1, 4}, {2, 5}, {3, 6}, {4, 7}, {1, 7}, {2, 8}, {3, 9}, {4, 10}, {1, 10}, {2, 11}, {3, 12}, {4,13}, {2.5, 7}}, Ticks -> {{1, 2, 3, 4}, None}, AxesStyle -> Thin, TicksStyle -> Directive[Black, Bold, 12], Mesh -> Full] I have thought using ListLinePlot command, but I don't know how to specify to the command to draw only selected lines between the dots. Do have any suggestions/hints on how to do that? Thank you. Answer One possibility would be to use Epilog with Line : ListPlot[ {{1, 1}, {2, 2}, {3, 3}, {4, 4}, {1, 4}, {2, 5}, {3, 6}, {4, 7}, {1, 7}, {2, 8}, {3, 9}, {4, 10}, {1, 10}, {2, 11}, {3, 12}, {4, 13}, {2.5, 7}}, Ticks -> {{1, 2, 3, 4}, None}, AxesStyle -> Thin, TicksStyle -> Directive[Black, Bold, 12], Mesh -> Full, Epilog -> { Line[ ...

equation solving - Invert and fit implicitly defined curve

I need to fit an implicitly defined curve. I thought I could get some data out of Solve , and then using FindFit . Therefore, I would like to find the relation the parametric curve defined by $F(x,y)=0$: Solve[-(1/2) + 1/2 (0.41202 BesselK[0, 0.1 Sqrt[x^2 + y^2]] + (0.101483 x BesselK[1, 0.1 Sqrt[x^2 + y^2]])/Sqrt[x^2 + y^2]) == 0, y] But I can't get an output: Solve was unable to solve the system with inexact coefficients or the system obtained by direct rationalization of inexact numbers present in the system. Since many of the methods used by Solve require exact input, providing Solve with an exact version of the system may help. >> Edit: In particular, I would like to fit the data coming from the curve with the expression of another curve, and not with a function $f(x)$. In particular, since this clearly looks like a cardioid , I would like it to fit to something like it. What other strategies could I try?

dynamic - How can I make a clickable ArrayPlot that returns input?

I would like to create a dynamic ArrayPlot so that the rectangles, when clicked, provide the input. Can I use ArrayPlot for this? Or is there something else I should have to use? Answer ArrayPlot is much more than just a simple array like Grid : it represents a ranged 2D dataset, and its visualization can be finetuned by options like DataReversed and DataRange . These features make it quite complicated to reproduce the same layout and order with Grid . Here I offer AnnotatedArrayPlot which comes in handy when your dataset is more than just a flat 2D array. The dynamic interface allows highlighting individual cells and possibly interacting with them. AnnotatedArrayPlot works the same way as ArrayPlot and accepts the same options plus Enabled , HighlightCoordinates , HighlightStyle and HighlightElementFunction . data = {{Missing["HasSomeMoreData"], GrayLevel[ 1], {RGBColor[0, 1, 1], RGBColor[0, 0, 1], GrayLevel[1]}, RGBColor[0, 1, 0]}, {GrayLevel[0], GrayLevel...