Monday, September 14, 2020

Fixing: "matplotlib error - no module named tkinter" in Windows 10

If you are using python 3.x in Windows 10 and encoutered this error: "matplotlib error - no module named tkinter", it's very possible that you didn't install the Tcl/Tk module when installing Python to your machine. The tkinter module comes from Tcl/Tk. Fear not, you don't need to uninstall and reinstall Python to fix it. You only need to modify your Python installation to install the missing Tcl/Tk support as follows:

  1. Go to Add/Remove Programs from Windows 10 search box. From there, select your Python installation, like this: 

  2. Then click the option to modify the Python installation. You'll be presented with this dialog: 

  3. Then click on the Modify option in the Python installer dialog. In the Optional Features dialog that shows up, make sure you checked the tcl/tk and IDLE option, like this (inside the red box): 

  4. Next, just follow the instruction after clicking on the Next button to apply the changes.  
After this, the matplotlib error should be fixed as the tkinter python module is installed. 

Note that even if you install the tcl/tk library via pip install successfully when encountering the matplotlib error, you will probably still left with the same matplotlib error. I'm not sure why it happened in my system. It's probably the module search path is somehow not working properly unless you do a "user/account-wide" or system-wide installation for the module to make it work. In any case, the solution above fixed the error in all cases that I have tested. I think it should work for you too. 

Monday, September 7, 2020

Removing "Still" Frames from Video Files with Ffmpeg

 Ffmpeg is a very versatile tool for video pre-processing and post-processing. One of its most useful feature is removing frames which doesn't have a lot of changes in it, i.e. relatively static background. The parameter to use for this purposes is the mpdecimate flag, the presentation timeframe (setpts)flag might also be used for synchronization purposes. In my case, the command I used to throwaway the relatively static vidio frames as follows:

ffmpeg -i input_vid.mp4 -vf mpdecimate,setpts=N/FRAME_RATE/TB output_vid.mp4

Details about the mpdecimate flag can be read over at: https://ffmpeg.org/ffmpeg-filters.html#decimate-1, and details about the setpts parameter is at https://ffmpeg.org/ffmpeg-filters.html#toc-setpts_002c-asetpts. Hopefully, this is helpful for you.

Sunday, August 23, 2020

Implementing C struct-like Data Structure in Python (Requires Python 3.6+)

 The solution to implement C struct-like data structure in Python is explained in: https://stackoverflow.com/questions/35988/c-like-structures-in-python. You need the presence of dataclass python class to create a syntactically and semantically similar data structure to C struct. This class is implemented in Python 3.7. Therefore, python 3.7 is the lowest version of python supported. However, the support has been backported to Python version 3.6 (see: https://pypi.org/project/dataclasses/#description ). 

Perhaps, you might ask, why not just use Python collections namedtuple data type. Well, namedtuple is syntactically similar to C struct. However, it's immutable, meaning: once you have set its value, you cannot change the value. The only way to change the object value is by creating a new object with the same type and setting the value into different value.

Anyway, this is a sample usage of dataclass:  

as you can see in the screenshot above, the value of c_struc class instance can be changed (mutable), as in a C struct.

Configuring Custom ipython Terminal Color Scheme

In some cases ipython default terminal color scheme is not the most readable one, especially when using Powershell in Windows. One of the way to fix the problem is to use custom/non-default color scheme. The steps to accomplish that as follows (tested in ipython v7.17 and python 3.7.9):

  1.  Run `pygmentize -L styles` in your terminal to check available ipython color styles/schemes. Experiment with the color scheme by starting ipython while settting the color scheme as one of its starting parameter, for example: ipython TerminalInteractiveShell.highlighting_style=native. Replace native with the color scheme that you want to test. ipython should start with the color scheme that you specify.
  2. Create default ipython configuration file for the current user by running: `ipython profile create` , just use the default configuration file. See: https://ipython.readthedocs.io/en/stable/config/intro.html for more in depth explanation of this command. 
  3. Edit the default configuration file located at: ~/.ipython/profile_default/ipython_config.py. In Windows, this file is located at: C:\Users\[username]\.ipython\profile_default\ipython_config.py.  Open the ipython_config.py file and edit the line that contain c.TerminalInteractiveShell.highlighting_style parameter. Set this config value to the color scheme that you want to make permanent. For example, in my config, I set the value to 'native' as follows: c.TerminalInteractiveShell.highlighting_style = 'native'. This will apply the 'native' color scheme as the default color scheme when you start ipython. 
The result of this configuration changes in my Powershell is shown below.