Thursday, December 31, 2015

Building a Home Security System with Raspberry Pi

Building a Home Security System with Raspberry Pi

Build your own sophisticated modular home security system using the popular Raspberry Pi board

About This Book
  • This book guides you through building a complete home security system with Raspberry Pi and helps you remotely access it from a mobile device over the Internet
  • It covers the fundamentals of interfacing sensors and cameras with the Raspberry Pi so that you can connect it to the outside world
  • It follows a modular approach so that you can choose the modules and features you want for your customized home security system
Who This Book Is For
This book is for anyone who is interested in building a modular home security system from scratch using a Raspberry Pi board, basic electronics, sensors, and simple scripts. This book is ideal for enthusiastic novice programmers, electronics hobbyists, and engineering professionals. It would be great if you have some basic soldering skills in order to build some of the interface modules.

What You Will Learn
  • Understand the concepts behind alarm systems and intrusion detection devices
  • Connect sensors and devices to the on-board digital GPIO ports safely
  • Monitor and control connected devices easily using Bash shell scripting
  • Build an I/O port expander using the I2C bus and connect sensors and anti-tamper circuits
  • Capture and store images using motion detectors and cameras
  • Access and manage your system remotely from your mobile phone
  • Receive intrusion alerts and images through your e-mail
  • Build a sophisticated multi-zone alarm system
In Detail
The Raspberry Pi is a powerful low-cost credit-card-sized computer, which lends itself perfectly as the controller for a sophisticated home security system. Using the on-board interfaces available, the Raspberry Pi can be expanded to allow the connection of a virtually infinite number of security sensors and devices. The Raspberry Pi has the processing power and interfaces available to build a sophisticated home security system but at a fraction of the cost of commercially available systems.

Building a Home Security System with Raspberry Pi starts off by showing you the Raspberry Pi and how to set up the Linux-based operating system. It then guides you through connecting switch sensors and LEDs to the native GPIO connector safely, and how to access them using simple Bash scripts. As you dive further in, you'll learn how to build an input/output expansion board using the I2C interface and power supply, allowing the connection of the large number of sensors needed for a typical home security setup.

In the later chapters of the book, we'll look at more sophisticated topics such as adding cameras, remotely accessing the system using your mobile phone, receiving intrusion alerts and images by e-mail, and more.

By the end of the book, you will be well-versed with the use of Raspberry Pi to power a home-based security system that sends message alerts whenever it is triggered and will be able to build a truly sophisticated and modular home security system. You will also gain a good understanding of Raspberry Pi's ecosystem and be able to write the functions required for a security system.

Style and approach
This easy-to-follow guide comprises a series of projects, where every chapter introduces a new concept and at the end of the book, all these concepts are brought together to create an entire home security system. This book features clear diagrams and code every step of the way.

Saturday, December 19, 2015

BeagleBone Black Cookbook

Over 70 recipes and solutions for inventors, makers, and budding engineers to create projects using the BeagleBone Black

BeagleBone Black Cookbook

About This Book
  • Learn how to develop applications with the BeagleBone Black and open source Linux software
  • Sharpen your expertise in making sophisticated electronic devices
  • Explore the BeagleBone Black with this easy-to-succeed recipe format
Who This Book Is For
If you are a hardware, Linux, and/or microcomputing novice, or someone who wants more power and possibilities with product prototypes, electronic art projects, or embedded computing experiments, then this book is for you. It is for Internet of Things enthusiasts who want to use more sophisticated hardware than the Raspberry Pi or the Arduino can provide.

Whether you are an engineering student, a DIYer, an inventor, or an advanced electronics enthusiast, this book delivers accessible, compelling instructions for using an advanced microcomputing platform.

What You Will Learn
  • Set up and run the BeagleBone Black for the first time
  • Learn the basics of microcomputing and Linux using the command line and easy kernel mods
  • Make introductory projects with Python, JavaScript, BoneScript, and Node.js
  • Explore physical computing and simple circuits using buttons, LEDs, sensors, and motors
  • Discover the unique features of the BeagleBone Black and its real-time computing functions
  • Build intermediate level audio and video applications
  • Assemble draft prototypes for wearable and Internet of Things devices
In Detail
With dozens of how-tos, this book kicks off with the basic steps for setting up and running the BeagleBone Black for the first time, from connecting the necessary hardware and using the command line with Linux commands to installing new software and controlling your system remotely. Following these recipes, more advanced examples take you through scripting, debugging, and working with software source files, eventually working with the Linux kernel. Subsequently, you will learn how to exploit the board's real-time functions. We will then discover exciting methods for using sound and video with the system before marching forward into an exploration of recipes for building Internet of Things projects. Finally, the book finishes with a dramatic arc upward into outer space, when you explore ways to set up test recipes for building a project on board a small satellite's payload.

Style and approach
This comprehensive recipe book deconstructs a complex, often confusing piece of technology, and transforms it to become accessible and fun with snappy, unintimidating prose, and extensive easy-to-succeed instructions.

Python to capture image from Pi Camera Module, with image effects


picamera.camera Module provide function to apply various image effect. This example modified from previous post "Python to capture image from Pi Camera Module, with White Balance setting", to add feature to apply image effect.

reference: API - picamera.camera Module - image_effect


myPiCam.py
import picamera
import Tkinter as tk
import ttk
import time
from PIL import ImageTk, Image
from threading import Thread
import io
import sys
from pkg_resources import require

RQS_0=0
RQS_QUIT=1
RQS_CAPTURE=2
rqs=RQS_0
rqsUpdateSetting=True

def camHandler():
    global rqs
    rqs = RQS_0
    
    camera = picamera.PiCamera()
    #stream = io.BytesIO()

    #set default
    camera.sharpness = 0
    camera.contrast = 0
    camera.brightness = 50
    camera.saturation = 0
    camera.ISO = 0
    camera.video_stabilization = False
    camera.exposure_compensation = 0
    camera.exposure_mode = 'auto'
    camera.meter_mode = 'average'
    camera.awb_mode = 'auto'
    camera.image_effect = 'none'
    camera.color_effects = None
    #camera.rotation = 0
    camera.rotation = 270
    camera.hflip = False
    camera.vflip = False
    camera.crop = (0.0, 0.0, 1.0, 1.0)
    #camera.resolution = (1024, 768)
    camera.resolution = (400, 300)
    #end of set default
    #camera.start_preview()

    while rqs != RQS_QUIT:
        #check if need update setting
        global rqsUpdateSetting
        if rqsUpdateSetting == True:
            rqsUpdateSetting = False
            camera.sharpness = scaleSharpness.get()
            camera.contrast = scaleContrast.get()
            camera.brightness = scaleBrightness.get()
            camera.saturation = scaleSaturation.get()
            camera.exposure_compensation = scaleExpCompensation.get()

            awb_mode_setting = varAwbMode.get()
            labelAwbVar.set(awb_mode_setting)
            camera.awb_mode = awb_mode_setting

            if awb_mode_setting == "off":
                gr = scaleGainRed.get()
                gb = scaleGainBlue.get()
                gAwb = (gr, gb)
                camera.awb_gains = gAwb
                labelAwbVar.set(awb_mode_setting + " : "
                    + str(gAwb))

            image_effect_setting = varImageEffect.get()
            labelImageEffectVar.set(image_effect_setting)
            camera.image_effect = image_effect_setting

            if image_effect_setting == 'solarize':
                if cbSolarize_yuv_Var.get():
                    yuv = 1
                else:
                    yuv = 0
                solarize_para = (
                    yuv,
                    scSolarize_x0_Var.get(),
                    scSolarize_y0_Var.get(),
                    scSolarize_y1_Var.get(),
                    scSolarize_y2_Var.get())
                labelImageEffectVar.set(image_effect_setting + " " + str(solarize_para))
                camera.image_effect_params = solarize_para
            elif image_effect_setting == 'colorpoint':
                camera.image_effect_params = quadrantVar.get()
                labelImageEffectVar.set(image_effect_setting + " " + str(quadrantVar.get()))
            elif image_effect_setting == 'colorbalance':
                colorbalance_para = (
                    scColorbalance_lens_Var.get(),
                    scColorbalance_r_Var.get(),
                    scColorbalance_g_Var.get(),
                    scColorbalance_b_Var.get(),
                    scColorbalance_u_Var.get(),
                    scColorbalance_v_Var.get())
                labelImageEffectVar.set(image_effect_setting + " " + str(colorbalance_para))
                camera.image_effect_params = colorbalance_para
            elif image_effect_setting == 'colorswap':
                labelImageEffectVar.set(image_effect_setting + " " + str(cbColorswap_dir_Var.get()))
                camera.image_effect_params = cbColorswap_dir_Var.get()
            elif image_effect_setting == 'posterise':
                labelImageEffectVar.set(image_effect_setting + " " + str(scPosterise_steps_Var.get()))
                camera.image_effect_params = scPosterise_steps_Var.get()
            elif image_effect_setting == 'blur':
                labelImageEffectVar.set(image_effect_setting + " " + str(scBlur_size_Var.get()))
                camera.image_effect_params = scBlur_size_Var.get()
            elif image_effect_setting == 'film':
                film_para = (
                    scFilm_strength_Var.get(),
                    scFilm_u_Var.get(),
                    scFilm_v_Var.get())
                labelImageEffectVar.set(image_effect_setting + " " + str(film_para))
                camera.image_effect_params = film_para
            elif image_effect_setting == 'watercolor':
                if cbWatercolor_uv_Var.get():
                    watercolor_para = (
                        scWatercolor_u_Var.get(),
                        scWatercolor_v_Var.get())
                    labelImageEffectVar.set(image_effect_setting + " " + str(watercolor_para))
                    camera.image_effect_params = watercolor_para
                else:
                    watercolor_para = ()
                    labelImageEffectVar.set(image_effect_setting + " " + str(watercolor_para))
                    camera.image_effect_params = watercolor_para

        if rqs == RQS_CAPTURE:
            print("Capture")
            rqs=RQS_0
            timeStamp = time.strftime("%Y%m%d-%H%M%S")
            jpgFile='img_'+timeStamp+'.jpg'
            camera.resolution = (2592, 1944)    #set photo size
            camera.capture(jpgFile)
            camera.resolution = (400, 300)      #resume preview size
            labelCapVal.set(jpgFile)
        else:
            stream = io.BytesIO()
            camera.capture(stream, format='jpeg')
            stream.seek(0)
            tmpImage = Image.open(stream)
            tmpImg = ImageTk.PhotoImage(tmpImage)
            previewPanel.configure(image = tmpImg)
            #sleep(0.5)
                
    print("Quit")        
    #camera.stop_preview()
    
def startCamHandler():
    camThread = Thread(target=camHandler)
    camThread.start()

def quit():
    global rqs
    rqs=RQS_QUIT

    global tkTop
    tkTop.destroy()

def capture():
    global rqs
    rqs = RQS_CAPTURE
    labelCapVal.set("capturing")

def cbScaleSetting(new_value):
    global rqsUpdateSetting
    rqsUpdateSetting = True

def cbButtons():
    global rqsUpdateSetting
    rqsUpdateSetting = True

tkTop = tk.Tk()
tkTop.wm_title("helloraspberrypi.blogspot.com")
tkTop.geometry('1000x650')

previewWin = tk.Toplevel(tkTop)
previewWin.title('Preview')
previewWin.geometry('400x300')
previewPanel = tk.Label(previewWin)
previewPanel.pack(side = "bottom", fill = "both", expand = "yes")

#tkButtonQuit = tk.Button(tkTop, text="Quit", command=quit).pack()

tkButtonCapture = tk.Button(
    tkTop, text="Capture", command=capture)
tkButtonCapture.pack()

SCALE_WIDTH = 980;

labelCapVal = tk.StringVar()
tk.Label(tkTop, textvariable=labelCapVal).pack()

notebook = ttk.Notebook(tkTop)
frame1 = ttk.Frame(notebook)
frame2 = ttk.Frame(notebook)
frame3 = ttk.Frame(notebook)
notebook.add(frame1, text='Setting')
notebook.add(frame2, text='White Balance')
notebook.add(frame3, text='Image Effect')
notebook.pack()

# Tab Setting
scaleSharpness = tk.Scale(
    frame1,
    from_=-100, to=100,
    length=SCALE_WIDTH,
    orient=tk.HORIZONTAL,
    label="sharpness",
    command=cbScaleSetting)
scaleSharpness.set(0)
scaleSharpness.pack(anchor=tk.CENTER)

scaleContrast = tk.Scale(
    frame1,
    from_=-100, to=100,
    length=SCALE_WIDTH,
    orient=tk.HORIZONTAL,
    label="contrast",
    command=cbScaleSetting)
scaleContrast.set(0)
scaleContrast.pack(anchor=tk.CENTER)

scaleBrightness = tk.Scale(
    frame1,
    from_=0, to=100,
    length=SCALE_WIDTH,
    orient=tk.HORIZONTAL,
    label="brightness",
    command=cbScaleSetting)
scaleBrightness.set(50)
scaleBrightness.pack(anchor=tk.CENTER)

scaleSaturation = tk.Scale(
    frame1,
    from_=-100, to=100,
    length=SCALE_WIDTH,
    orient=tk.HORIZONTAL,
    label="saturation",
    command=cbScaleSetting)
scaleSaturation.set(0)
scaleSaturation.pack(anchor=tk.CENTER)

scaleExpCompensation = tk.Scale(
    frame1,
    from_=-25, to=25,
    length=SCALE_WIDTH,
    orient=tk.HORIZONTAL,
    label="exposure_compensation",
    command=cbScaleSetting)
scaleExpCompensation.set(0)
scaleExpCompensation.pack(anchor=tk.CENTER)

# Tab White Balance

lfAwbMode = ttk.LabelFrame(frame2, text="awb_mode")
lfAwbMode.pack(fill="x")
lfAwbGains = ttk.LabelFrame(frame2, text="awb_gains")
lfAwbGains.pack(fill="x")

labelAwbVar = tk.StringVar()
tk.Label(lfAwbMode, textvariable=labelAwbVar).pack()

#--
AWB_MODES = [
    ("off", "off"),
    ("auto", "auto"),
    ("sunlight", "sunlight"),
    ("cloudy", "cloudy"),
    ("shade", "shade"),
    ("tungsten", "tungsten"),
    ("fluorescent", "fluorescent"),
    ("incandescent", "incandescent"),
    ("flash", "flash"),
    ("horizon", "horizon"),
    ]

varAwbMode = tk.StringVar()
varAwbMode.set("auto")
for text, awbmode in AWB_MODES:
    awbModeBtns = tk.Radiobutton(
        lfAwbMode,
        text=text,
        variable=varAwbMode,
        value=awbmode,
        command=cbButtons)
    awbModeBtns.pack(anchor=tk.W)
#--
scaleGainRed = tk.Scale(
    lfAwbGains,
    from_=0.0, to=8.0,
    resolution=0.1,
    length=SCALE_WIDTH,
    orient=tk.HORIZONTAL,
    label="Red",
    command=cbScaleSetting)
scaleGainRed.set(0.0)
scaleGainRed.pack(anchor=tk.CENTER)

scaleGainBlue = tk.Scale(
    lfAwbGains,
    from_=0.0, to=8.0,
    resolution=0.1,
    length=SCALE_WIDTH,
    orient=tk.HORIZONTAL,
    label="Blue",
    command=cbScaleSetting)
scaleGainBlue.set(0.0)
scaleGainBlue.pack(anchor=tk.CENTER)

# Tab Image Effect
#For Image effects, ref:
#http://picamera.readthedocs.org/en/latest/api_camera.html?highlight=effect#picamera.camera.PiCamera.image_effect
labelImageEffectVar = tk.StringVar()
tk.Label(frame3, textvariable=labelImageEffectVar).pack()
#-- image_effect

varImageEffect = tk.StringVar()
varImageEffect.set('none')

lfNoParaOpts1 = ttk.Frame(frame3)
lfNoParaOpts1.pack(fill="x", expand="yes")

tk.Radiobutton(lfNoParaOpts1, variable=varImageEffect,
        text='none',value='none',command=cbButtons).pack(anchor=tk.W, side=tk.LEFT)
tk.Radiobutton(lfNoParaOpts1, variable=varImageEffect,
        text='negative',value='negative',command=cbButtons).pack(anchor=tk.W, side=tk.LEFT)
tk.Radiobutton(lfNoParaOpts1, variable=varImageEffect,
        text='sketch',value='sketch',command=cbButtons).pack(anchor=tk.W, side=tk.LEFT)
tk.Radiobutton(lfNoParaOpts1, variable=varImageEffect,
        text='denoise',value='denoise',command=cbButtons).pack(anchor=tk.W, side=tk.LEFT)
tk.Radiobutton(lfNoParaOpts1, variable=varImageEffect,
        text='emboss',value='emboss',command=cbButtons).pack(anchor=tk.W, side=tk.LEFT)
tk.Radiobutton(lfNoParaOpts1, variable=varImageEffect,
        text='oilpaint',value='oilpaint',command=cbButtons).pack(anchor=tk.W, side=tk.LEFT)
tk.Radiobutton(lfNoParaOpts1, variable=varImageEffect,
        text='hatch',value='hatch',command=cbButtons).pack(anchor=tk.W, side=tk.LEFT)
tk.Radiobutton(lfNoParaOpts1, variable=varImageEffect,
        text='gpen',value='gpen',command=cbButtons).pack(anchor=tk.W, side=tk.LEFT)

lfNoParaOpts2 = ttk.Frame(frame3)
lfNoParaOpts2.pack(fill="x", expand="yes")
tk.Radiobutton(lfNoParaOpts2, variable=varImageEffect,
        text='pastel',value='pastel',command=cbButtons).pack(anchor=tk.W, side=tk.LEFT)
tk.Radiobutton(lfNoParaOpts2, variable=varImageEffect,
        text='saturation',value='saturation',command=cbButtons).pack(anchor=tk.W, side=tk.LEFT)
tk.Radiobutton(lfNoParaOpts2, variable=varImageEffect,
        text='washedout',value='washedout',command=cbButtons).pack(anchor=tk.W, side=tk.LEFT)
tk.Radiobutton(lfNoParaOpts2, variable=varImageEffect,
        text='cartoon',value='cartoon',command=cbButtons).pack(anchor=tk.W, side=tk.LEFT)
tk.Radiobutton(lfNoParaOpts2, variable=varImageEffect,
        text='deinterlace1',value='deinterlace1',command=cbButtons).pack(anchor=tk.W, side=tk.LEFT)
tk.Radiobutton(lfNoParaOpts2, variable=varImageEffect,
        text='deinterlace2',value='deinterlace2',command=cbButtons).pack(anchor=tk.W, side=tk.LEFT)

lfSolarize = ttk.LabelFrame(frame3, text="solarize")
lfSolarize.pack(fill="x", expand="yes")

tk.Radiobutton(lfSolarize, variable=varImageEffect,
        text='solarize',value='solarize',command=cbButtons).pack(anchor=tk.W, side=tk.LEFT)

cbSolarize_yuv_Var = tk.BooleanVar()
tk.Checkbutton(lfSolarize, text="yuv",
    variable=cbSolarize_yuv_Var, command=cbButtons).pack(anchor=tk.W, side=tk.LEFT)

scSolarize_x0_Var = tk.IntVar()
scSolarize_x0_Var.set(128)
tk.Scale(lfSolarize, from_=0, to=255,
    orient=tk.HORIZONTAL, length=200, label="x0",
    variable=scSolarize_x0_Var, command=cbScaleSetting).pack(anchor=tk.W, side=tk.LEFT)

scSolarize_y0_Var = tk.IntVar()
scSolarize_y0_Var.set(128)
tk.Scale(lfSolarize, from_=0, to=255,
    orient=tk.HORIZONTAL, length=200, label="y0",
    variable=scSolarize_y0_Var, command=cbScaleSetting).pack(anchor=tk.W, side=tk.LEFT)

scSolarize_y1_Var = tk.IntVar()
scSolarize_y1_Var.set(128)
tk.Scale(lfSolarize, from_=0, to=255,
    orient=tk.HORIZONTAL, length=200, label="y1",
    variable=scSolarize_y1_Var, command=cbScaleSetting).pack(anchor=tk.W, side=tk.LEFT)

scSolarize_y2_Var = tk.IntVar()
scSolarize_y2_Var.set(0)
tk.Scale(lfSolarize, from_=0, to=255,
    orient=tk.HORIZONTAL, length=200, label="y2",
    variable=scSolarize_y2_Var, command=cbScaleSetting).pack(anchor=tk.W, side=tk.LEFT)

lfwatercolor = ttk.LabelFrame(frame3, text="watercolor")
lfwatercolor.pack(fill="x", expand="yes")
tk.Radiobutton(lfwatercolor, variable=varImageEffect,
        text='watercolor',value='watercolor',command=cbButtons).pack(anchor=tk.W, side=tk.LEFT)

cbWatercolor_uv_Var = tk.BooleanVar()
cbWatercolor_uv_Var.set(False)
tk.Checkbutton(lfwatercolor, text="uv",
    variable=cbWatercolor_uv_Var, command=cbButtons).pack(anchor=tk.W, side=tk.LEFT)

scWatercolor_u_Var = tk.IntVar()
scWatercolor_u_Var.set(0)
tk.Scale(lfwatercolor, from_=0, to=255,
    orient=tk.HORIZONTAL, length=200, label="u",
    variable=scWatercolor_u_Var, command=cbScaleSetting).pack(anchor=tk.W, side=tk.LEFT)
scWatercolor_v_Var = tk.IntVar()
scWatercolor_v_Var.set(0)
tk.Scale(lfwatercolor, from_=0, to=255,
    orient=tk.HORIZONTAL, length=200, label="v",
    variable=scWatercolor_v_Var, command=cbScaleSetting).pack(anchor=tk.W, side=tk.LEFT)

lffilm = ttk.LabelFrame(frame3, text="film")
lffilm.pack(fill="x", expand="yes")
tk.Radiobutton(lffilm, variable=varImageEffect,
        text='film',value='film',command=cbButtons).pack(anchor=tk.W, side=tk.LEFT)

scFilm_strength_Var = tk.IntVar()
scFilm_strength_Var.set(0)
tk.Scale(lffilm, from_=0, to=255,
    orient=tk.HORIZONTAL, length=200, label="strength",
    variable=scFilm_strength_Var, command=cbScaleSetting).pack(anchor=tk.W, side=tk.LEFT)
scFilm_u_Var = tk.IntVar()
scFilm_u_Var.set(0)
tk.Scale(lffilm, from_=0, to=255,
    orient=tk.HORIZONTAL, length=200, label="u",
    variable=scFilm_u_Var, command=cbScaleSetting).pack(anchor=tk.W, side=tk.LEFT)
scFilm_v_Var = tk.IntVar()
scFilm_v_Var.set(0)
tk.Scale(lffilm, from_=0, to=255,
    orient=tk.HORIZONTAL, length=200, label="v",
    variable=scFilm_v_Var, command=cbScaleSetting).pack(anchor=tk.W, side=tk.LEFT)

lfblur = ttk.LabelFrame(frame3, text="blur")
lfblur.pack(fill="x", expand="yes")
tk.Radiobutton(lfblur, variable=varImageEffect,
        text='blur',value='blur',command=cbButtons).pack(anchor=tk.W, side=tk.LEFT)
scBlur_size_Var = tk.IntVar()
scBlur_size_Var.set(1)
tk.Scale(lfblur, from_=1, to=2,
    orient=tk.HORIZONTAL, length=100, label="size",
    variable=scBlur_size_Var, command=cbScaleSetting).pack(anchor=tk.W, side=tk.LEFT)

lfcolorswap = ttk.LabelFrame(frame3, text="colorswap")
lfcolorswap.pack(fill="x", expand="yes")
tk.Radiobutton(lfcolorswap, variable=varImageEffect,
        text='colorswap',value='colorswap',command=cbButtons).pack(anchor=tk.W, side=tk.LEFT)
cbColorswap_dir_Var = tk.BooleanVar()
cbColorswap_dir_Var.set(False)
tk.Checkbutton(lfcolorswap, text="dir - 0:RGB to BGR/1:RGB to BRG",
    variable=cbColorswap_dir_Var, command=cbButtons).pack(anchor=tk.W, side=tk.LEFT)

lfposterise = ttk.LabelFrame(frame3, text="posterise")
lfposterise.pack(fill="x", expand="yes")
tk.Radiobutton(lfposterise, variable=varImageEffect,
        text='posterise',value='posterise',command=cbButtons).pack(anchor=tk.W, side=tk.LEFT)
scPosterise_steps_Var = tk.IntVar()
scPosterise_steps_Var.set(4)
tk.Scale(lfposterise, from_=2, to=32,
    orient=tk.HORIZONTAL, length=200, label="steps",
    variable=scPosterise_steps_Var, command=cbScaleSetting).pack(anchor=tk.W, side=tk.LEFT)

lfcolorpoint = ttk.LabelFrame(frame3, text="colorpoint")
lfcolorpoint.pack(fill="x", expand="yes")
tk.Radiobutton(lfcolorpoint, variable=varImageEffect,
        text='colorpoint',value='colorpoint',command=cbButtons).pack(anchor=tk.W, side=tk.LEFT)
quadrantVar = tk.IntVar()
quadrantVar.set(0)
tk.Radiobutton(lfcolorpoint, text="green",
    variable=quadrantVar, value=0, command=cbButtons).pack(anchor=tk.W, side=tk.LEFT)
tk.Radiobutton(lfcolorpoint, text="red/yellow",
    variable=quadrantVar, value=1, command=cbButtons).pack(anchor=tk.W, side=tk.LEFT)
tk.Radiobutton(lfcolorpoint, text="blue",
    variable=quadrantVar, value=2, command=cbButtons).pack(anchor=tk.W, side=tk.LEFT)
tk.Radiobutton(lfcolorpoint, text="purple",
    variable=quadrantVar, value=3, command=cbButtons).pack(anchor=tk.W, side=tk.LEFT)

lfcolorbalance = ttk.LabelFrame(frame3, text="colorbalance: I can't see the effect!")
lfcolorbalance.pack(fill="x", expand="yes")
tk.Radiobutton(lfcolorbalance, variable=varImageEffect,
        text='colorbalance',value='colorbalance',command=cbButtons).pack(anchor=tk.W, side=tk.LEFT)

scColorbalance_lens_Var = tk.DoubleVar()
scColorbalance_lens_Var.set(0)
tk.Scale(lfcolorbalance, from_=0, to=256,
    orient=tk.HORIZONTAL, length=140, label="lens",
    variable=scColorbalance_lens_Var, command=cbScaleSetting).pack(anchor=tk.W, side=tk.LEFT)

scColorbalance_r_Var = tk.DoubleVar()
scColorbalance_r_Var.set(1)
tk.Scale(lfcolorbalance, from_=0, to=256,
    orient=tk.HORIZONTAL, length=140, label="r",
    variable=scColorbalance_r_Var, command=cbScaleSetting).pack(anchor=tk.W, side=tk.LEFT)

scColorbalance_g_Var = tk.DoubleVar()
scColorbalance_g_Var.set(1)
tk.Scale(lfcolorbalance, from_=0, to=256,
    orient=tk.HORIZONTAL, length=140, label="g",
    variable=scColorbalance_g_Var, command=cbScaleSetting).pack(anchor=tk.W, side=tk.LEFT)

scColorbalance_b_Var = tk.DoubleVar()
scColorbalance_b_Var.set(1)
tk.Scale(lfcolorbalance, from_=0, to=256,
    orient=tk.HORIZONTAL, length=140, label="b",
    variable=scColorbalance_b_Var, command=cbScaleSetting).pack(anchor=tk.W, side=tk.LEFT)

scColorbalance_u_Var = tk.IntVar()
scColorbalance_u_Var.set(0)
tk.Scale(lfcolorbalance, from_=0, to=255,
    orient=tk.HORIZONTAL, length=140, label="u",
    variable=scColorbalance_u_Var, command=cbScaleSetting).pack(anchor=tk.W, side=tk.LEFT)

scColorbalance_v_Var = tk.IntVar()
scColorbalance_v_Var.set(0)
tk.Scale(lfcolorbalance, from_=0, to=255,
    orient=tk.HORIZONTAL, length=140, label="v",
    variable=scColorbalance_v_Var, command=cbScaleSetting).pack(anchor=tk.W, side=tk.LEFT)

#=======================================
print("start")
startCamHandler()

tk.mainloop()


Download the Python script HERE.


updated@2016-04-15:
Current Raspbian Jessie come with PIL installed by default, but no ImageTk. If you run Python script reported with error:
ImportError: cannot import name ImageTk

Install python-imaging-tk
$ sudo apt-get install python-imaging-tk


Friday, December 18, 2015

Python for Secret Agents - Second Edition

Gather, analyze, and decode data to reveal hidden facts using Python, the perfect tool for all aspiring secret agents

Python for Secret Agents - Second Edition

About This Book
  • Discover the essential features of Python programming: statements, variables, expressions, and many of the built-in data types
  • Use Python's standard library to do more sophisticated data gathering and analysis
  • Written by a Python programming expert, with over 35 years' experience as a consultant, teacher, author and software developer
Who This Book Is For
This book is for Secret Agents who have some exposure to Python. Our focus is on the Field Agents who are ready to do more sophisticated and complex programming in Python. We'll stick to simple statistics for the most part. A steady hand with a soldering iron is not required, but a skilled field agent should be able to assemble a working Arduino circuit to gather their own sensor data.

What You Will Learn
  • Upgrade Python to the latest version and discover its latest and greatest tools
  • Use Python libraries to extract data from log files that are designed more for people to read than for automated analysis
  • Summarize log files and extract meaningful information
  • Gather data from social networking sites and leverage your experience of analyzing log files to summarize the data you find
  • Extract text and images from social networking sites
  • Parse the complex and confusing data structures in a PDF file to extract meaningful text that we can analyze
  • Connect small, intelligent devices to our computer to use them as remote sensors
  • Use Python to analyze measurements from sensors to calibrate them and use sensors efficiently
In Detail
Python is easy to learn and extensible programming language that allows any manner of secret agent to work with a variety of data. Agents from beginners to seasoned veterans will benefit from Python's simplicity and sophistication. The standard library provides numerous packages that move beyond simple beginner missions. The Python ecosystem of related packages and libraries supports deep information processing.

This book will guide you through the process of upgrading your Python-based toolset for intelligence gathering, analysis, and communication. You'll explore the ways Python is used to analyze web logs to discover the trails of activities that can be found in web and database servers. We'll also look at how we can use Python to discover details of the social network by looking at the data available from social networking websites.

Finally, you'll see how to extract history from PDF files, which opens up new sources of data, and you'll learn about the ways you can gather data using an Arduino-based sensor device.

Style and approach
Each chapter will include a background briefing that covers an essential Python technology. After some in-depth exploration of the features, the chapter will conclude with a mission that is a concrete application of the Python tools and techniques covered.

Python to capture image from Pi Camera Module, with White Balance setting


Further works on previous post of "Python to capture image from Pi Camera Module, with preview", add feature of White Balance setting.


myPiCam.py
import picamera
import Tkinter as tk
import ttk
import time
from PIL import ImageTk, Image
from threading import Thread
import io
import sys
from pkg_resources import require

RQS_0=0
RQS_QUIT=1
RQS_CAPTURE=2
rqs=RQS_0
rqsUpdateSetting=True

def camHandler():
    global rqs
    rqs = RQS_0
    
    camera = picamera.PiCamera()
    #stream = io.BytesIO()

    #set default
    camera.sharpness = 0
    camera.contrast = 0
    camera.brightness = 50
    camera.saturation = 0
    camera.ISO = 0
    camera.video_stabilization = False
    camera.exposure_compensation = 0
    camera.exposure_mode = 'auto'
    camera.meter_mode = 'average'
    camera.awb_mode = 'auto'
    camera.image_effect = 'none'
    camera.color_effects = None
    #camera.rotation = 0
    camera.rotation = 270
    camera.hflip = False
    camera.vflip = False
    camera.crop = (0.0, 0.0, 1.0, 1.0)
    #camera.resolution = (1024, 768)
    camera.resolution = (400, 300)
    #end of set default
    #camera.start_preview()

    while rqs != RQS_QUIT:
        #check if need update setting
        global rqsUpdateSetting
        if rqsUpdateSetting == True:
            rqsUpdateSetting = False
            camera.sharpness = scaleSharpness.get()
            camera.contrast = scaleContrast.get()
            camera.brightness = scaleBrightness.get()
            camera.saturation = scaleSaturation.get()
            camera.exposure_compensation = scaleExpCompensation.get()

            awb_mode_setting = varAwbMode.get()
            labelAwbVar.set(awb_mode_setting)
            camera.awb_mode = awb_mode_setting

            if awb_mode_setting == "off":
                gr = scaleGainRed.get()
                gb = scaleGainBlue.get()
                gAwb = (gr, gb)
                camera.awb_gains = gAwb
                labelAwbVar.set(awb_mode_setting + " : "
                    + str(gAwb))
        
        if rqs == RQS_CAPTURE:
            print("Capture")
            rqs=RQS_0
            timeStamp = time.strftime("%Y%m%d-%H%M%S")
            jpgFile='img_'+timeStamp+'.jpg'
            camera.resolution = (2592, 1944)    #set photo size
            camera.capture(jpgFile)
            camera.resolution = (400, 300)      #resume preview size
            labelCapVal.set(jpgFile)
        else:
            stream = io.BytesIO()
            camera.capture(stream, format='jpeg')
            stream.seek(0)
            tmpImage = Image.open(stream)
            tmpImg = ImageTk.PhotoImage(tmpImage)
            previewPanel.configure(image = tmpImg)
            #sleep(0.5)
                
    print("Quit")        
    #camera.stop_preview()
    
def startCamHandler():
    camThread = Thread(target=camHandler)
    camThread.start()

def quit():
    global rqs
    rqs=RQS_QUIT

    global tkTop
    tkTop.destroy()

def capture():
    global rqs
    rqs = RQS_CAPTURE
    labelCapVal.set("capturing")

def cbScaleSetting(new_value):
    global rqsUpdateSetting
    rqsUpdateSetting = True

def cbButtons():
    global rqsUpdateSetting
    rqsUpdateSetting = True

tkTop = tk.Tk()
tkTop.wm_title("Raspberry Pi Camera")
tkTop.geometry('800x500')

previewWin = tk.Toplevel(tkTop)
previewWin.title('Preview')
previewWin.geometry('400x300')
previewPanel = tk.Label(previewWin)
previewPanel.pack(side = "bottom", fill = "both", expand = "yes")

tk.Label(tkTop, text="http://helloraspberrypi.blogspot.com/").pack()
tk.Label(tkTop, text=require('picamera')).pack()

tkButtonQuit = tk.Button(
    tkTop, text="Quit", command=quit)
tkButtonQuit.pack()

tkButtonCapture = tk.Button(
    tkTop, text="Capture", command=capture)
tkButtonCapture.pack()

SCALE_WIDTH = 780;

labelCapVal = tk.StringVar()
tk.Label(tkTop, textvariable=labelCapVal).pack()

notebook = ttk.Notebook(tkTop)
frame1 = ttk.Frame(notebook)
frame2 = ttk.Frame(notebook)
notebook.add(frame1, text='Setting')
notebook.add(frame2, text='White Balance')
notebook.pack()

# Tab Setting
scaleSharpness = tk.Scale(
    frame1,
    from_=-100, to=100,
    length=SCALE_WIDTH,
    orient=tk.HORIZONTAL,
    label="sharpness",
    command=cbScaleSetting)
scaleSharpness.set(0)
scaleSharpness.pack(anchor=tk.CENTER)

scaleContrast = tk.Scale(
    frame1,
    from_=-100, to=100,
    length=SCALE_WIDTH,
    orient=tk.HORIZONTAL,
    label="contrast",
    command=cbScaleSetting)
scaleContrast.set(0)
scaleContrast.pack(anchor=tk.CENTER)

scaleBrightness = tk.Scale(
    frame1,
    from_=0, to=100,
    length=SCALE_WIDTH,
    orient=tk.HORIZONTAL,
    label="brightness",
    command=cbScaleSetting)
scaleBrightness.set(50)
scaleBrightness.pack(anchor=tk.CENTER)

scaleSaturation = tk.Scale(
    frame1,
    from_=-100, to=100,
    length=SCALE_WIDTH,
    orient=tk.HORIZONTAL,
    label="saturation",
    command=cbScaleSetting)
scaleSaturation.set(0)
scaleSaturation.pack(anchor=tk.CENTER)

scaleExpCompensation = tk.Scale(
    frame1,
    from_=-25, to=25,
    length=SCALE_WIDTH,
    orient=tk.HORIZONTAL,
    label="exposure_compensation",
    command=cbScaleSetting)
scaleExpCompensation.set(0)
scaleExpCompensation.pack(anchor=tk.CENTER)

# Tab White Balance

lfAwbMode = ttk.LabelFrame(frame2, text="awb_mode")
lfAwbMode.pack(fill="x", expand="yes")
lfAwbGains = ttk.LabelFrame(frame2, text="awb_gains")
lfAwbGains.pack(fill="x", expand="yes")

labelAwbVar = tk.StringVar()
tk.Label(lfAwbMode, textvariable=labelAwbVar).pack()

#--
AWB_MODES = [
    ("off", "off"),
    ("auto", "auto"),
    ("sunlight", "sunlight"),
    ("cloudy", "cloudy"),
    ("shade", "shade"),
    ("tungsten", "tungsten"),
    ("fluorescent", "fluorescent"),
    ("incandescent", "incandescent"),
    ("flash", "flash"),
    ("horizon", "horizon"),
    ]

varAwbMode = tk.StringVar()
varAwbMode.set("auto")
for text, awbmode in AWB_MODES:
    awbModeBtns = tk.Radiobutton(
        lfAwbMode,
        text=text,
        variable=varAwbMode,
        value=awbmode,
        command=cbButtons)
    awbModeBtns.pack(anchor=tk.W)
#--
scaleGainRed = tk.Scale(
    lfAwbGains,
    from_=0.0, to=8.0,
    resolution=0.1,
    length=SCALE_WIDTH,
    orient=tk.HORIZONTAL,
    label="Red",
    command=cbScaleSetting)
scaleGainRed.set(0.0)
scaleGainRed.pack(anchor=tk.CENTER)

scaleGainBlue = tk.Scale(
    lfAwbGains,
    from_=0.0, to=8.0,
    resolution=0.1,
    length=SCALE_WIDTH,
    orient=tk.HORIZONTAL,
    label="Blue",
    command=cbScaleSetting)
scaleGainBlue.set(0.0)
scaleGainBlue.pack(anchor=tk.CENTER)
#
print("start")
startCamHandler()

tk.mainloop()


Next:
- Python to capture image from Pi Camera Module, with image effects

Thursday, December 17, 2015

Python to capture image from Pi Camera Module, with preview


The previous post show a exercise of "Python to capture image from Pi Camera Module", blind shot without preview. It's modify to show preview continually, to framing before clicking capture button.

- To make the GUI and Camera preview run simultaneously, code for camera are moved to run in background thread, in camHandler().
- My target is to take the RPi/Camera Module outdoor, without display. So I remove camera.start_preview() and stop_preview(). By removing preview, the GUI response improved very much.
- The preview window update frame-by-frame, may be not so smooth! Anyway, better than nothing:(
- When click the Quit button, may be the Tkinter GUI closed before the background thread terminated, so may be have the error of "RuntimeError: Too early to create image" and 'Exception AttributeError:...". Just ignore for now.


myPiCam.py
import picamera
import Tkinter
import time
from PIL import ImageTk, Image
from threading import Thread
import io
import sys

RQS_0=0
RQS_QUIT=1
RQS_CAPTURE=2
rqs=RQS_0

def camHandler():
    global rqs
    rqs = RQS_0
    
    camera = picamera.PiCamera()
    #stream = io.BytesIO()

    #set default
    camera.sharpness = 0
    camera.contrast = 0
    camera.brightness = 50
    camera.saturation = 0
    camera.ISO = 0
    camera.video_stabilization = False
    camera.exposure_compensation = 0
    camera.exposure_mode = 'auto'
    camera.meter_mode = 'average'
    camera.awb_mode = 'auto'
    camera.image_effect = 'none'
    camera.color_effects = None
    #camera.rotation = 0
    camera.rotation = 270
    camera.hflip = False
    camera.vflip = False
    camera.crop = (0.0, 0.0, 1.0, 1.0)
    #camera.resolution = (1024, 768)
    camera.resolution = (400, 300)
    #end of set default
    #camera.start_preview()

    while rqs != RQS_QUIT:
        if rqs == RQS_CAPTURE:
            print("Capture")
            rqs=RQS_0
            timeStamp = time.strftime("%Y%m%d-%H%M%S")
            jpgFile='img_'+timeStamp+'.jpg'
            camera.resolution = (2592, 1944)    #set photo size
            camera.capture(jpgFile)
            camera.resolution = (400, 300)      #resume preview size
            labelCapVal.set(jpgFile)
        else:
            #set parameter
            camera.sharpness = scaleSharpness.get()
            camera.contrast = scaleContrast.get()
            camera.brightness = scaleBrightness.get()
            camera.saturation = scaleSaturation.get()
            camera.exposure_compensation = scaleExpCompensation.get()

            stream = io.BytesIO()
            camera.capture(stream, format='jpeg')
            stream.seek(0)
            tmpImage = Image.open(stream)
            tmpImg = ImageTk.PhotoImage(tmpImage)
            previewPanel.configure(image = tmpImg)
            #sleep(0.5)
                
    print("Quit")        
    #camera.stop_preview()
    
def startCamHandler():
    camThread = Thread(target=camHandler)
    camThread.start()

def quit():
    global rqs
    rqs=RQS_QUIT

    global tkTop
    tkTop.destroy()

def capture():
    global rqs
    rqs = RQS_CAPTURE
    labelCapVal.set("capturing")

tkTop = Tkinter.Tk()
tkTop.wm_title("Raspberry Pi Camera")
tkTop.geometry('800x500')

previewWin = Tkinter.Toplevel(tkTop)
previewWin.title('Preview')
previewWin.geometry('400x300')
previewPanel = Tkinter.Label(previewWin)
previewPanel.pack(side = "bottom", fill = "both", expand = "yes")

tkButtonQuit = Tkinter.Button(
    tkTop, text="Quit", command=quit)
tkButtonQuit.pack()

tkButtonCapture = Tkinter.Button(
    tkTop, text="Capture", command=capture)
tkButtonCapture.pack()

SCALE_WIDTH = 780;

labelCapVal = Tkinter.StringVar()
Tkinter.Label(tkTop, textvariable=labelCapVal).pack()

scaleSharpness = Tkinter.Scale(
    tkTop,
    from_=-100, to=100,
    length=SCALE_WIDTH,
    orient=Tkinter.HORIZONTAL,
    label="sharpness")
scaleSharpness.set(0)
scaleSharpness.pack(anchor=Tkinter.CENTER)

scaleContrast = Tkinter.Scale(
    tkTop,
    from_=-100, to=100,
    length=SCALE_WIDTH,
    orient=Tkinter.HORIZONTAL,
    label="contrast")
scaleContrast.set(0)
scaleContrast.pack(anchor=Tkinter.CENTER)

scaleBrightness = Tkinter.Scale(
    tkTop,
    from_=0, to=100,
    length=SCALE_WIDTH,
    orient=Tkinter.HORIZONTAL,
    label="brightness")
scaleBrightness.set(50)
scaleBrightness.pack(anchor=Tkinter.CENTER)

scaleSaturation = Tkinter.Scale(
    tkTop,
    from_=-100, to=100,
    length=SCALE_WIDTH,
    orient=Tkinter.HORIZONTAL,
    label="saturation")
scaleSaturation.set(0)
scaleSaturation.pack(anchor=Tkinter.CENTER)

scaleExpCompensation = Tkinter.Scale(
    tkTop,
    from_=-25, to=25,
    length=SCALE_WIDTH,
    orient=Tkinter.HORIZONTAL,
    label="exposure_compensation")
scaleExpCompensation.set(0)
scaleExpCompensation.pack(anchor=Tkinter.CENTER)

print("start")
startCamHandler()

Tkinter.mainloop()



Next:
- Python to capture image from Pi Camera Module, with White Balance setting

Wednesday, December 16, 2015

Python Programming for Raspberry Pi, Sams Teach Yourself in 24 Hours (2nd Edition)

Python Programming for Raspberry Pi, Sams Teach Yourself in 24 Hours (2nd Edition) (Sams Teach Yourself -- Hours)

In just 24 sessions of one hour or less, Sams Teach Yourself Python Programming for Raspberry Pi in 24 Hours teaches you Python programming on Raspberry Pi, so you can start creating awesome projects for home automation, home theater, gaming, and more. Using this book’s straightforward, step-by-step approach, you’ll move from the absolute basics all the way through network and web connections, multimedia, and even connecting with electronic circuits for sensing and robotics. Every lesson and case study application builds on what you’ve already learned, giving you a rock-solid foundation for real-world success!

Step-by-step instructions carefully walk you through the most common Raspberry Pi Python programming tasks.
Quizzes at the end of each chapter help you test your knowledge.
By the Way notes present interesting information related to the discussion.
Did You Know? tips offer advice or show you easier ways to perform tasks.
Watch Out! cautions alert you to possible problems and give you advice on how to avoid them.
  • Get your Raspberry Pi and choose the right low-cost peripherals
  • Set up Raspian Linux and the Python programming environment
  • Learn Python basics, including arithmetic and structured commands
  • Master Python 3 lists, tuples, dictionaries, sets, strings, files, and modules
  • Reuse the same Python code in multiple locations with functions
  • Manipulate string data efficiently with regular expressions
  • Practice simple object-oriented programming techniques
  • Use exception handling to make your code more reliable
  • Program modern graphical user interfaces with Raspberry Pi and OpenGL
  • Create Raspberry Pi games with the PyGame library
  • Learn network, web, and database techniques you can also use in business software
  • Write Python scripts that send email
  • Interact with other devices through Raspberry Pi’s GPIO interface
  • Walk through example Raspberry Pi projects that inspire you to do even more

Python to capture image from Pi Camera Module


In order to take my RPi outdoor to take photos using Camera Module, I prepare a simple Python program to capture image with GUI to easy capture image, with some simple control; such as sharpness, contrast, brightness, saturation and exposure_compensation.

Such that I can login my Pi (with xrdp installed) from mobile phone with GUI and run the Python to take photo.

Here how it work:


Program list, myPiCam.py
import picamera
from time import sleep
import Tkinter
import time
from PIL import ImageTk, Image

def quit():
    camera.stop_preview()
    global tkTop
    tkTop.destroy()

def loadJpg(file):

    JpgWin = Tkinter.Toplevel(tkTop)
    JpgWin.title('New Window')
    JpgWin.geometry('400x300')

    image = Image.open(file)
    image = image.resize((400, 300), Image.ANTIALIAS)
    img = ImageTk.PhotoImage(image)
    panel = Tkinter.Label(JpgWin, image=img)
    panel.pack(side = "bottom", fill = "both", expand = "yes")

    JpgWin.mainloop()

def capture():
    #set parameter
    camera.sharpness = scaleSharpness.get()
    camera.contrast = scaleContrast.get()
    camera.brightness = scaleBrightness.get()
    camera.saturation = scaleSaturation.get()
    camera.exposure_compensation = scaleExpCompensation.get()
    
    timeStamp = time.strftime("%Y%m%d-%H%M%S")
    jpgFile='img_'+timeStamp+'.jpg'
    camera.capture(jpgFile)
    loadJpg(jpgFile)

camera = picamera.PiCamera()

#set default
camera.sharpness = 0
camera.contrast = 0
camera.brightness = 50
camera.saturation = 0
camera.ISO = 0
camera.video_stabilization = False
camera.exposure_compensation = 0
camera.exposure_mode = 'auto'
camera.meter_mode = 'average'
camera.awb_mode = 'auto'
camera.image_effect = 'none'
camera.color_effects = None
#camera.rotation = 0
camera.rotation = 270
camera.hflip = False
camera.vflip = False
camera.crop = (0.0, 0.0, 1.0, 1.0)
#camera.resolution = (1024, 768)
camera.resolution = (2592, 1944)
#end of set default

camera.start_preview()
camera.brightness = 50

tkTop = Tkinter.Tk()
tkTop.wm_title("Raspberry Pi Camera - Brightness")
tkTop.geometry('800x500')

tkButtonQuit = Tkinter.Button(
    tkTop, text="Quit", command=quit)
tkButtonQuit.pack()

tkButtonCapture = Tkinter.Button(
    tkTop, text="Capture", command=capture)
tkButtonCapture.pack()

SCALE_WIDTH = 780;

scaleSharpness = Tkinter.Scale(
    tkTop,
    from_=-100, to=100,
    length=SCALE_WIDTH,
    orient=Tkinter.HORIZONTAL,
    label="sharpness")
scaleSharpness.set(0)
scaleSharpness.pack(anchor=Tkinter.CENTER)

scaleContrast = Tkinter.Scale(
    tkTop,
    from_=-100, to=100,
    length=SCALE_WIDTH,
    orient=Tkinter.HORIZONTAL,
    label="contrast")
scaleContrast.set(0)
scaleContrast.pack(anchor=Tkinter.CENTER)

scaleBrightness = Tkinter.Scale(
    tkTop,
    from_=0, to=100,
    length=SCALE_WIDTH,
    orient=Tkinter.HORIZONTAL,
    label="brightness")
scaleBrightness.set(50)
scaleBrightness.pack(anchor=Tkinter.CENTER)

scaleSaturation = Tkinter.Scale(
    tkTop,
    from_=-100, to=100,
    length=SCALE_WIDTH,
    orient=Tkinter.HORIZONTAL,
    label="saturation")
scaleSaturation.set(0)
scaleSaturation.pack(anchor=Tkinter.CENTER)

scaleExpCompensation = Tkinter.Scale(
    tkTop,
    from_=-25, to=25,
    length=SCALE_WIDTH,
    orient=Tkinter.HORIZONTAL,
    label="exposure_compensation")
scaleExpCompensation.set(0)
scaleExpCompensation.pack(anchor=Tkinter.CENTER)

Tkinter.mainloop()


If you run on Raspbian Jessie, you need to iinstall python-imaging-tk:
$ sudo apt-get install python-imaging-tk

If you run on Wheezy, you have to "Install PIL (with jpg supported) and ImageTk on Raspberry Pi/Raspbian".

Remark:
This code have to be run in terminal using python:
$ python myPiCam.py

If run in IDLE, it will report following error in next run:
PiCameraMMALError: Camera component couldn't be enabled: Out of resources (other than memory)

Python GUI Programming Cookbook

Over 80 object-oriented recipes to help you create mind-blowing GUIs in Python

Python GUI Programming Cookbook

About This Book
  • Use object-oriented programming to develop amazing GUIs in Python
  • Create a working GUI project as a central resource for developing your Python GUIs
  • Packed with easy-to-follow recipes to help you develop code using the latest released version of Python
Who This Book Is For
If you are a Python programmer with intermediate level knowledge of GUI programming and want to learn how to create beautiful, effective, and responsive GUIs using the freely available Python GUI frameworks, this book is for you.

What You Will Learn
  • Create amazing GUIs with Python's built-in Tkinter module
  • Customize the GUIs by using layout managers to arrange the GUI widgets
  • Advance to an object-oriented programming style using Python
  • Develop beautiful charts using the free Matplotlib Python module
  • Use threading in a networked environment to make the GUIs responsive
  • Discover ways to connect the GUIs to a database
  • Understand how unit tests can be created and internationalize the GUI
  • Extend the GUIs with free Python frameworks using best practices
In Detail
Python is a multi-domain, interpreted programming language. It is a widely used general-purpose, high-level programming language. It is often used as a scripting language because of its forgiving syntax and compatibility with a wide variety of different eco-systems. Its flexible syntax enables developers to write short scripts while at the same time, they can use object-oriented concepts to develop very large projects.

Python GUI Programming Cookbook follows a task-based approach to help you create beautiful and very effective GUIs with the least amount of code necessary. This book uses the simplest programming style, using the fewest lines of code to create a GUI in Python, and then advances to using object-oriented programming in later chapters. If you are new to object-oriented programming (OOP), this book will teach you how to take advantage of the OOP coding style in the context of creating GUIs written in Python.

Throughout the book, you will develop an entire GUI application, building recipe upon recipe, connecting the GUI to a database. In the later chapters, you will explore additional Python GUI frameworks, using best practices. You will also learn how to use threading to ensure your GUI doesn't go unresponsive.

By the end of the book, you will be an expert in Python GUI programming to develop a common set of GUI applications.

Style and approach
Every recipe in this programming cookbook solves a problem you might encounter in your programming career. At the same time, most of the recipes build on each other to create an entire, real-life GUI application.

Tuesday, December 15, 2015

Pi NoIR photo samples (without and with Hoya R72 filter)


Photo samples of Raspberry Pi Camera Module Pi NoIR, without and with Hoya R72 Infrared filter. All photos resized only, added with black frame and text, no any other touch up.

It seem that the photos using Hoya R72 filter are little blue, may be misfocused for infrared!



















The Raspberry Pi 2 installed xrdp, such that I can remote login from mobile phone.

On the Android phone, I use free aRDP: Secure RDP Client.

Once Raspberry Pi boot-up and connect to Android hotspot, Run aRDP on Android phone, and run the simple "Python program to capture image from Pi Camera Module".


~ My Raspberry Pi Camera Modules series (with programming)

Modify Raspberry Pi 2 case to add 52mm filter mount

Just bought a Pi NoIR (Infrared version of Camera Module). Then I want to test the effect of infrared photo with Hoya R72 filter (for Infrared Photography to blocks Visible Light Up To 720nm). So I have to add a 52mm filter mount on the Raspberry Pi 2 case.


52mm Hoya R72 Infrared filter:



Attract something like Blu-Tack on a 27-52mm  conversion ring:


Stick on RPi 2 case:



So I can install 52mm Hoya R72 filter on:
\


Finished:






~ Photo samples: Next - Pi NoIR photo samples (without and with Hoya R72 filter)