analitics

Pages

Thursday, February 16, 2017

Compare two images: the histogram method.

This is a very simple example about how to compare the histograms of both images and print the inconsistencies are bound to arise.
The example come with alternative solution: Histogram method.
The script was run under Fedora 25.
If the images are the same the result will be 0.0.
For testing I change the image2.png by make a line into this with a coverage of 10%.
The result of the script was:
1116.63243729
The images come with this dimensions: 738 x 502 px.
import math
import operator
from math import *
import PIL

from PIL import Image
h1 = Image.open("image1.png").histogram()
h2 = Image.open("image2.png").histogram()

rms = math.sqrt(reduce(operator.add,
        map(lambda a,b: (a-b)**2, h1, h2))/len(h1))
print rms
About the operator module exports a set of efficient functions corresponding to the intrinsic operators of Python.
Example:
operator.lt(a, b)
operator.le(a, b)
operator.eq(a, b)
operator.ne(a, b)
operator.ge(a, b)
operator.gt(a, b)
operator.__lt__(a, b)
operator.__le__(a, b)
operator.__eq__(a, b)
operator.__ne__(a, b)
operator.__ge__(a, b)
operator.__gt__(a, b)

This is like math operators:
lt(a, b) is equivalent to a < b
le(a, b) is equivalent to a <= b
Another example:
>>> # Elementwise multiplication
>>> map(mul, [0, 1, 2, 3], [10, 20, 30, 40])
[0, 20, 60, 120]

>>> # Dot product
>>> sum(map(mul, [0, 1, 2, 3], [10, 20, 30, 40]))
200

Thursday, January 26, 2017

The kivy pyhon module for android.

First you need to install kivy python module.
The kivy python module
C:\>cd Python27

C:\Python27>cd Scripts

C:\Python27\Scripts>pip install kivy
Collecting kivy
Downloading Kivy-1.9.1-cp27-none-win32.whl (7.4MB)
100% |################################| 7.4MB 50kB/s
Collecting Kivy-Garden>=0.1.4 (from kivy)
Downloading kivy-garden-0.1.4.tar.gz
Requirement already satisfied: requests in c:\python27\lib\site-packages (from Kivy-Garden>=0.1.4->kivy)
Installing collected packages: Kivy-Garden, kivy
Running setup.py install for Kivy-Garden ... done
Successfully installed Kivy-Garden-0.1.4 kivy-1.9.1

Use this to add new pthon modules:
python -m pip install --upgrade docutils pygments pypiwin32 kivy.deps.sdl2 kivy.deps.glew kivy.deps.gstreamer --extra-index-url https://kivy.org/downloads/packages/simple/
If the python kivy.deps.gstreamer don't working then is not problem will try without this python module.
To see outdated python modules , use this commnds:
python -m pip install --upgrade pip wheel setuptools
pip list outdated
Let's upgrade all of python modules with this:
pip freeze > requirements.txt && pip install --upgrade -r requirements.txt && del requirements.txt
Another way to install this python module can be found here.
The demo can be star with this comman line:
C:\Python27>python share\kivy-examples\demo\showcase\main.py
You can also test one simple python script:
from kivy.app import App
from kivy.uix.button import Button

class TestApp(App):
def build(self):
return Button(text='Hello World')

TestApp().run()

To use with android operating system then test Buildozer.
This tool supports packaging for Android via the python-for-android project, and for iOS via the kivy-ios project. Support for other operating systems is intended in the future.