Wednesday, December 24, 2014

irfanview: rename file names in batch processing

Renaming camera photos in
$E36868(%Y%m%d)_$E36868(%H%M%S)

terminator: windows size and position

Example of a confing file: ~/.config/terminator/config


[global_config]
[keybindings]
[profiles]
  [[default]]
    use_system_font = False
    font = Monospace 12
    background_image = None
[layouts]
  [[default]]
    [[[child1]]]
      profile = default
      type = Terminal
      parent = window0
    [[[window0]]]
      type = Window
      parent = ""
      position = 1110:1043
      size = 1300, 450 

[plugins]

Wednesday, November 05, 2014

Ubuntu: pdf to images and images to pdf

pdftoppm -png a_pdf_file.pdf img
convert *.png a_pdf_file_as_img.pdf

Tuesday, November 04, 2014

Git pretty tree alias

git config --global alias.tree "log --graph --full-history --all --color --pretty=format:\"%x1b[31m%h%x09%x1b[32m%d%x1b[0m%x20%s\""

Tuesday, September 23, 2014

rename with grouping

rename "s/(\d\d)/0\$1/" 28/ The code will rename folder named "28" into "028".

Thursday, September 04, 2014

Firefox OS MultiROM build from 4th September 2014 for Nexus 5

Following this guide, I built the current (4th September 2014) Firefox OS from git repositories for Nexus 5 for MultiROM.  The guide explains how to build it, make MultiROM and how to install it on Nexus 5. The problem is that it contains build rom from June 2014. So I decided to build the current one from September 2014

 USE AT OWN RISK! 
I don't assume any responsibility for damage caused to your device or lost data.

The Firefox_OS_2014_09_04.zip rom for MultiRom can be downloaded from here.







Monday, August 04, 2014

ImageMagick: resize images to specific width or hight

For example to resize tif images to width of 512 pixels:mogrify -resize 512 *.tif
Whereas to resize tif images to height of 512 pixels:mogrify -resize x512 *.tif

Tuesday, July 29, 2014

Git: Show updated tree of branches in console

To do this, just add the following to ~/.gitconfig[alias]
  tree = log --graph --all --decorate --pretty=oneline --abbrev-commit
Then git tree will give text based tree of branches:

To run git tree in console showing e.g. 20 latest commits we can run git tree in a while loop as follows: while true; do clear; git tree -20; sleep 2; done

Wednesday, July 23, 2014

Gnome 3.12: Add VirtualBox machine to gnome's activities and dash

Specific virtual machines can be launched directly from gnome activities or gnome dash without the need to start standard Virtual Box GUI. For this we need to create desktop files and place them in our local applications folder, i.e., /.local/share/applications.
Example of a desktop file starting virtual machine called w7 is as follows (file name w7.desktop)
[Desktop Entry]
Encoding=UTF-8
Version=1.0
Name=w7
Comment=Starts the VirtualBox machine w7
Type=Application
Exec=/usr/lib/virtualbox/VirtualBox --comment "w7" --startvm "25e911a6-7bae-422a-84c2-30ced68d701d"
Icon=w7.png
view raw w7.desktop hosted with ❤ by GitHub
These files can be created manually, but also using Virtual Box GUI. Once we have these files, it is enough just to copy them into /.local/share/applications and they will be e immediately available in the gnomes activities. From the we can just drop and drag them to dash.
If we want to create them manually, we can use the example from the above. The ids of the virtual machines can be obtained using the following vboxmanage list vms
"w7" {25e911a6-7bae-422a-84c2-30ced68d701d}

Tuesday, July 22, 2014

Gnome 3.12: Auto start applications with a delay

In gnome, applications can be start by copying *.desktop files into ~/.config/autostart.

The installed desktop files of your applications are in /usr/share/applications and in ~/.local/share/applications. Thus you can copy files from there into ~/.config/autostart and modify to your liking.

Off course, you can also create your own desktop files for running, or starting applications with a delay. For example, I made the desktop file called myautostart.desktop in ~/.config/autostart[Desktop Entry]
Type=Application
Name=MyStartup
Comment=Starts my applications
Exec=/bin/bash /home/put-your-user-name/.mystartups.sh
X-GNOME-Autostart-enabled=true
Hidden=false
NoDisplay=false

As can be seen the file starts ~/.mystartups.sh which has the following contents:#!/bin/bash
sleep 2
exo-open /usr/share/applications/terminator.desktop
sleep 1
exo-open /usr/share/applications/chromium.desktop

The file launches terminator and chromium using desktop files with 2 and 1 second delays.

Sunday, July 20, 2014

Imagemagick: Flattening tiff images

mogrify -alpha off -colorspace gray -depth 8 -flatten *.tiff

Ubuntu: Fix ntfs parition on usb drive

If there are problems with mounting usb thumb drive (in my case the usb is /dev/sdc1), one can use this:sudo ntfsfix /dev/sdc1

Saturday, July 12, 2014

Capture desktop using ffmpeg and re-size the resulting video.

The following command can be used to record your display and the compress the resulting image ffmpeg -f x11grab -video_size 2560x1440 -i $DISPLAY -f alsa -i default -c:v ffvhuff -c:a flac test.mkv
ffmpeg -i test.mkv -vcodec libx264 -preset ultrafast -qp 0 -acodec copy -vf scale=-1:720 output.mkv

Thursday, July 03, 2014

How to install virtual environment of Python 3.4.x from source in Ubuntu 14.04

Below is a recipe that I use to used to install and set-up virtual environment of Python 3.4.x in Ubuntu 14.04 x64.
# Below are commands used to install and set-up virtual environment of Python 3.4 in Ubuntu 14.04.
# First we need to install dependencies. Please not that this is much larger list than is required for setting up only Python.
# The reason is that usually I and my users install other python packages through pip (list of the popular ones is at the
# end) and these packages also require some packages to be installed first. Thus, I prefer to install everything at
# once.
sudo apt-get install build-essential libc6-dev libreadline-dev libncursesw5-dev libssl-dev libgdbm-dev libsqlite3-dev libbz2-dev liblzma-dev tk-dev bzip2 libbz2-dev sqlite3 tk8.5-dev zlib1g-dev liblzma-dev libblas3 liblapack3 gfortran libopenblas-dev liblapack-dev libmagickwand-dev libxml2-dev libxslt1-dev qtmobility-dev git cmake libqt4-dev libwebp-dev
# Download latest Python, unpack it, and enter into Python folder.
# When writing this post, Python 3.4.1 was the latest release.
wget https://www.python.org/ftp/python/3.4.1/Python-3.4.1.tar.xz
tar xvfJ ./Python-3.4.1.tar.xz
cd Python-3.4.1/
# Configure, compile and install in /opt/python341
./configure --prefix=/opt/python341 --enable-shared
make
sudo make install
make clean
# Create Python virtual invornoment in /home/<yourusername>/mypy34env/
/opt/python341/bin/pyvenv ~/mypy34env/
# active the python virtual environment
source ~/mypy34env/bin/activate
# Check if we are really using the just installed python
which python pip
# expected output
#/home/<yourusername>/mypy34env/bin/python
#/home/<yourusername>/mypy34env/bin/pip
# In some cases, python shared libraries will be need. Thus we can add the location of python's libraries to the linker.
export LD_LIBRARY_PATH=/opt/python341/lib:$LD_LIBRARY_PATH
# I and many my users usually require other packaged (such as numpy, pillow, pyside) to be installed in python.
# With all dependencies installed before, installing these packages is very simple.
# Please note that installing all of them will take a little while. From my experience, pyside takes the longest
# time to compile.
pip install ipython numpy scipy sympy matplotlib pandas pillow wand flask simplejson requests arrow lxml pyside

Thursday, June 26, 2014

Reading DICOM files in Python 3

In Python 3, dicom files can be read, analysied and processed using Wand which is Python binding for ImageMagic. The example below shows how to get medical and image date embedded in DICOM and also how to disply the image itself.
from wand.image import Image
from wand.display import display
in_file = 'some_dicom_file.dcm'
with Image(filename=in_file) as img:
# show metadata for DICOM, such as dates, patient info, resolution, etc.
for k, v in img.metadata.items():
print(k,v)
# display the image
display(img)

Python 3.4 and Pillow 2.4 with JPEG2000 (openjpeg 2) support in Ubuntu 14.04

Pillow support for JPEG2000 comes from openjpeg 2 library. Unfortunatly, Ubuntu's libopenjpeg2 package is not version 2 of the openjpeg library, but actually version 1.3 which wont work with Pillow.

Installing Pillow 2.4 (pip install -I pillow) simply results in "OPENJPEG (JPEG2000) support not available".

Thus, it is needed to compile the openjpeg library 2.0 from source. For this, first lets download the openjpeg 2.0.1:wget http://downloads.sourceforge.net/project/openjpeg.mirror/2.0.1/openjpeg-2.0.1.tar.gz

tar xzvf openjpeg-2.0.1.tar.gz
cd openjpeg-2.0.1/
cmake .
make
sudo make install


Please note that we install version of openjpeg 2.0, rather than newer 2.1. Pillow wont recognize openjpeg 2.1 either.

Assuming everything went fine, we can reinstall Pillow:pip install -I pillow
If openjpeg 2.0.1 was detected successfully, we should get the following info "OPENJPEG (JPEG2000) support available" among others.

Wednesday, June 18, 2014

DICOM (dcm) files wont upload to Wordpress

Uploading DICOM files (*.dcm) files into Wordpress, may not work. For example using Visual Form Builder plugin for file upload, wont upload DICOM's. This is because wordpress and/or file upload plugins use wp_get_mime_types() function from wp-includes/functions.php to check mime type of the file being upload. Unfortunatly, dicom is not there. Thus it need to be added. To do this, just add 'dcm' => 'application/dicom', into wp_get_mime_types(). For example, as shown below:

Thursday, June 12, 2014

Xubuntu: Take screenshot of current window only

Pressing Prt Sc button on the keyboard takes screenshot of the entire desktop. To take the screenshot of the selected/current window only, we can use Alt + Prt Sc

Tuesday, June 10, 2014

ImageMagick: get size and resolution (DPI) of images

To get size and resolution in DPI of many images at once using ImageMagick, we can use this command:identify -format "%w x %h %x x %y\n" *.tiff This result for example in:576 x 432 72 PixelsPerInch x 72 PixelsPerInch 576 x 432 72 PixelsPerInch x 72 PixelsPerInch 576 x 432 72 PixelsPerInch x 72 PixelsPerInch 576 x 432 72 PixelsPerInch x 72 PixelsPerInch 576 x 432 72 PixelsPerInch x 72 PixelsPerInch 576 x 432 72 PixelsPerInch x 72 PixelsPerInch

Friday, June 06, 2014

Python 3.4: Check if module installed

To check whether a given module is install in Python 3.4, we can use PathFinder.find_spec class method. For example, to check if pyperclip moduel is installed / available we can use the following code:
from importlib.machinery import PathFinder
if PathFinder.find_spec('pyperclip'):
print('pyperclip module found')
else:
print('pyperclip module not found')
view raw example.py hosted with ❤ by GitHub

Wednesday, May 28, 2014

Google: search for websites containing exactly the given word or pharase

To search for websites containing exactly the given word or phrase, one can use allintext: operator. For example:
allintext:"some phrase"

Friday, May 23, 2014

Python 3 and tkinter: scroll canvas with mouse wheel and drag it around with middle mouse button

This is an example in Python 3.4 running in 64-bit Ubuntu 14.04 of a canvas that can be scrolled with mouse wheel and dragged around with middle mouse button. Hope this example will be useful to you.

'''
Example of canvas with that can be scrolled with mouse and
dragged around with middle mouse of the button.
Written and tested using Python 3.4 on Ubuntu x64 14.04
'''
from tkinter import *
class MyCanvas(Canvas):
def __init__(self, parent=None, img=None, *parms, **kparms):
Canvas.__init__(self, parent, *parms, **kparms)
self._width = 1000;
self._height = 1000;
self._starting_drag_position = ()
self.config(width = self._width, height=self._height, bg='green')
self._draw_some_example_objects()
self._add_scrollbars()
self._addMouseBindings()
self.pack(fill=BOTH, expand=YES)
def _add_scrollbars(self):
self.sbarV = Scrollbar(self.master, orient=VERTICAL)
self.sbarH = Scrollbar(self.master, orient=HORIZONTAL)
self.sbarV.config(command=self.yview)
self.sbarH.config(command=self.xview)
self.config(yscrollcommand=self.sbarV.set)
self.config(xscrollcommand=self.sbarH.set)
self.sbarV.pack(side=RIGHT, fill=Y)
self.sbarH.pack(side=BOTTOM, fill=X)
def _addMouseBindings(self):
# mouse wheel scroll
self.bind('<4>', lambda event : self.yview('scroll', -1, 'units'))
self.bind('<5>', lambda event : self.yview('scroll', 1, 'units'))
# dragging canvas with mouse middle button
self.bind("<Button-2>", self.__start_scroll)
self.bind("<B2-Motion>", self.__update_scroll)
self.bind("<ButtonRelease-2>", self.__stop_scroll)
def __start_scroll(self, event):
# set the scrolling increment.
# value of 0 is unlimited and very fast
# set it to 1,2,3 or whatever to make it slower
self.config(yscrollincrement=3)
self.config(xscrollincrement=3)
self._starting_drag_position = (event.x, event.y)
self.config(cursor="fleur")
def __update_scroll(self, event):
deltaX = event.x - self._starting_drag_position[0]
deltaY = event.y - self._starting_drag_position[1]
self.xview('scroll', deltaX, 'units')
self.yview('scroll', deltaY, 'units')
self._starting_drag_position = (event.x, event.y)
def __stop_scroll(self, event):
# set scrolling speed back to 0, so that mouse scrolling
# works as expected.
self.config(xscrollincrement=0)
self.config(yscrollincrement=0)
self.config(cursor="")
def _draw_some_example_objects(self):
colors = dict(outline="black")
self.config(scrollregion=(0,0, self._width, self._height))
self.create_rectangle(30, 10, 120, 120, fill="red", **colors)
self.create_rectangle(330, 410, 420, 460, fill="blue", **colors)
self.create_rectangle(830, 810, 920, 960, fill="yellow", **colors)
self.create_rectangle(830, 210, 990, 500, fill="cyan", **colors)
self.create_rectangle(130, 810, 290, 999, fill="gray", **colors)
class MyGUI(Tk):
def __init__(self, *args, **kwargs):
Tk.__init__(self, *args, **kwargs)
self.title("Drag canvas with mouse")
self.geometry("500x500")
self._addWidgets()
def _addWidgets(self):
my_canvas = MyCanvas(self)
if __name__ == '__main__':
MyGUI().mainloop()