#25 | py01 – Importing an image with Tkinter and introduction to Geoplotlib

Hi guys,

Today, I learned two different things:
– First, how to import and show an image with Tkinter from my documents in Python.
– And then to show a map with custom coordinated points on an OSMap also running with Python.


Import an Image with Tkinter

I wanted to know how to import any image from a menu, that way I don’t need to write the absolute path in the code.
For this kind of code, I tried to understand a new function: def.
“def” is used to define a function with a specific command.
For example, if I need a pizza, I have to call a number and ask for a specific pizza and then give my address. “def” gives me the opportunity to make a button called “pizza()” that can make all the stuff previously quoted in a single keyword.

In my case, I make three “def” functions for three different commands:
– Open() will open the folder with my images in the root of my program.
– Close() will delete all images showed on the canvas.
– About() will be another button for any different pieces of information I wanted to show.

For the fun part, I wanted to make a golden ratio rectangle for my canvas, so I decided to calculate it with this formula:

And then, knowing a good ratio for my canvas, I can now write the code.
I just needed to import some more library like Pillow for some image module.
For that, I just had to use “pip” to download the library with this sentence in the cmd window:

pip install pillow --force

Now, I want to create a menu with Tkinter.
You can see below a preview of what it looks like with an “1600×1056” image:

Introduction to Geoplotlib

Geoplotlib is a library like Tkinter except this one is necessary for mapping and other stuff involving geographic coordinates.
This module is important if I want to create a map of Dreamworld.
I still don’t know how to make it but it will be a good step to custom my own map using geoplotlib.
For this part, I need to understand how it works and with Bach explanations of the process, I can grasp how programming is like. Instead of knowing everything, I learn to read the official docs and find the information I need to make what I want. Luckily, geoplotlib is easy to learn the basic code. I used some examples to help me to understand the mechanics.

↑ This map shows all the bus stops in Danemark.

↑ Importing custom markers on dots.

↑ Voronoid map of the bus stops, dark area shown compacted data and light area, less crowded informations.

Finally, the best part I guess is the one where I tried to custom the markers AND the style of the map.
I got some difficulties to understand because of my lack of knowledge with Python, but with some help, I understand the great part of the program: for example, geoplotlib got some issues with its script so the module didn’t run properly. I needed to change the source code to make it works.
The second big issue was Decimal Degrees Coordinates (DD) used by the module. In google maps, the coordinates are written with the Degree-Minute-Second system (DMS). I needed to convert it and implant the coordinates in a .csv file. These coordinates will be shown as plots or markers on the map.
And then, the final issue was to understand the difference between dictionnaries and lists for the matrix notations.
In fact, geoplotlib doesn’t use different dictionaries for each list of data, but one dictionary for all its lists.

Explanations:

Dictionary = {'name1' = 'definition1', 'name2' = 'definition2', ...}
List = ['name1', 'name2', 'name3']

How we think geoplotlib works:

ListofDictionaries = [{"name1": 0,"name2": 0},{"name1": 1,"name2": 1}]

How geoplotlib really works:

DictionaryofLists = {'name1":[0,1], 'name2': [0,1]}

So yeah… It’s quite a mess and I have not completly understood everything yet.
However, here is the short code for this mess:

Like you have seen in the code and to be a little less anxious about the basical graphic design of OSM, we decided to change it with a watercolored style (because it is cool). What it looks like:


Red and White(Teal) positions 😉

See you soon guys and take care ! ♥


What I learned:

  • def() function
  • Tkinter image importation
  • Golden ratio use
  • install with pip
  • marking a map with geoplotlib
  • what is a voronoid math design
  • the difference between dictonary and list in matrix code
  • learning to use a documentation (important)
  • having fun with programmation (important too)

Leave a Reply

Your email address will not be published.