Monday, July 28, 2008

Windows XP Home won't allow login after installation, domain error

I've recently installed Windows XP Home to a computer which was previously equipped with Win XP Professional. There was a minor glitch during the installation when the installer refused to delete the C: partition because there were some installation files present. This was possibly caused by inserting the install CD into running computer, the installer then copied some files into C: and expected to put Windows on a D: or E: partition. After reset a new installation started and run OK. Unfortunately, I couldn't log in into the machine although I haven't set any password for the only user account set up during installation. The message was "Domain does not Exist" with "smart" advice to contact system administrator. I've found several questions posted on the web regarding the same problem but no real answer to the problem. The trouble is, I have not set up any domain and Win XP Home officially does not even support domains!
My conclusion was that the installer somehow picked up some remnants of the previous Win XP Pro system (which does support domains but on this PC was not using them) and malfunctioned.

So I've done a reinstall with following steps:
1) format not only C: but also D: partitions;
2) full format on C: to NTFS before system installation begins (i.e. not the fast formatting option); it takes about 12 minutes on a 50 GB partition;
3) different user name from the previous (XP Pro) system.

I'm not sure whether these steps are all necessary or which one did the trick but after second installation everything works as it should.

Thursday, July 10, 2008

Showing a webcam image in the desktop window using Python. Part 3: self-refreshing app

This is the last part of a three-part series. In the first part, I've described how to read image from the web. The second part was dedicated to showing images in a window on the desktop. This last part will deal with auto-refreshing of the displayed image. This is particularly useful when you want to display a webcam image which is regularly updated.
To handle autorefresh we will use a Tkinter method after() which will call a defined function after some given delay.
To the previous code, we will add the following function:


def timingloop(start=0):
now = time.clock()
if start == 1:
getimagefromweb(myurl)
if now > container.start + container.refresh:
getimagefromweb(myurl)
container.start = now
else:
remains = str(int(container.refresh - now + container.start))
message = 'Next update in '+remains+' seconds.'
container.statusbarVar.set(message)
container.statusbar.update()
container.main.after(200, timingloop)

This function controls downloading new images by calling getimagefromweb() and checks when to do it by comparing saved times with current time. Finally, the function calls itself after 200 miliseconds.
For the program to work you also have to add some other stuff into the code. To the beginning add "import time" to import time library. The last part of the script has to be slightly modified as follows:


# the script
myurl = 'http://siemens.mesto-zatec.cz/obrazek.jpg'
container = Container()
container.width = 800
container.height = 602

container.start = time.clock()
container.refresh = 20 # window refresh rate in seconds

drawwindow()
timingloop(1)

mainloop()
print 'end OK'

We have added two new variables with timestamp and a refresh rate in seconds after which the image will be re-loaded. Finally, the getimagefromweb() function has been replaced by the timingloop() with parameter "1" which tells it that it is the start of the program and that it should load the image immediately. After the first timingloop() there is enough time to call mainloop() without which the program hangs (and I'm not sure why). As you can see using after() is a bit unintuitive because you can call other functions after "after()" was called and is still running in the background.

Tuesday, July 8, 2008

Showing a webcam image in the desktop window using Python. Part 2: displaying images in windows

In the previous post I have shown how to download an image from web with the use of Python. Today I will show code which takes this image and draws it in a standalone window.
Before we start it is necessary to be prepared somewhat. You need a specialised library to make windows. I 'm using Tkinter which comes with Python as default. There are more options like wxwidgets but I've started with Tkinter and so far it has covered all my needs. It seemed quite tricky to learn so I have found a good tutorial on Tkinter and learned it that way. Unfortunately, it is only in czech and currently seems offline so I cannot link to it anyway :-(.
To deal with images I'm using the PIL or Python Imaging Library. This you will need to download and install in order to use following code. Good news is that it's free :-)
Programming windowed applications is a lot more complicated than simple beginner stuff and in today's code there are commands which I don't know why they are there...
The code:

from Tkinter import *
import urllib
from PIL import Image, ImageTk

class Container:
pass

def drawwindow():
main = Tk()
main.title('Zatec webcam') # window title
main.resizable(width=False, height=False)
container.main = main
container.canvas = Canvas(main, width=container.width, height=container.height)
container.canvas.pack(expand=1, fill=BOTH)
container.statusbarVar = StringVar()
container.statusbar = Label(main, textvariable=container.statusbarVar)
container.statusbar.pack()
container.statusbarVar.set('Ready.')
container.statusbar.update()


def showimage(image):
'''Loads given image and puts it into the window'''
img = Image.open(image)
photo = ImageTk.PhotoImage(img)
container.canvas.create_image(container.width/2+2, container.height/2, image=photo)
container.obr = photo # why is this line necessary?

def getimagefromweb(url):
'''Downloads content from given url and saves it as image.'''
container.statusbarVar.set('Reading new image from web...')
container.statusbar.update()
try:
u = urllib.urlopen(url) # open url
container.statusbarVar.set('Url opened...')
container.statusbar.update()

content = u.read() # read the opened url
container.statusbarVar.set('Url read...')
container.statusbar.update()

u.close() # url was closed
container.statusbarVar.set('Url closed...')
container.statusbar.update()

except IOError:
print('IOError')
pass
except:
print('Unknown url error')

# saving what was downloaded
f = open('img.jpg','wb')
f.write(content)
f.close() # file was closed

# showing the image
showimage('img.jpg')
container.statusbarVar.set('Ready.')
container.statusbar.update()

# the script
myurl = 'http://siemens.mesto-zatec.cz/obrazek.jpg'
container = Container()
container.width = 800
container.height = 602

drawwindow()
getimagefromweb(myurl)

mainloop()
print 'end OK'

The class Container is used to store information which is passed between functions. The code could be rewritten to form a single class but it works this way too :-).
There are three functions. Drawwindow is used to create window with desired elements in it (Canvas and Label). To provide some info on download status there is a text in the Label. This is done with the help of StringVar() which is from Tkinter library. If we used only a string, it would be immutable later on. After the Label is changed, an .update() function must be called to change it on the display too.
The showimage() function gets an image file from disk and puts it into the Canvas. There is some magic in it, sorry about that.
The third function, getimagefromweb(), is adopted from previous post by adding info into the Label and after the image saved it calls the showimage() function.
In the script, we first draw the window elements with drawwindow() and then download an image and paste it into the Canvas with getimagefromweb().
The last command, mainloop(), tells Tkinter that we are ready and draws everything on the display. Without this command nothing will be shown (though it will be ready). Also, after mainloop is called, the focus is changed from your script to the GUI which is driven by user action. After you close the window the focus is returned to your python script.

Sunday, July 6, 2008

Showing a webcam image in the desktop window using Python. Part 1: reading image from web

I will describe everything that is necessary to show a window with a webcam (or any other) image in it. I'm using similar app to display a stock price chart (downloaded from a website) which is regularly updated. I will divide the text into three parts because I think this will be easier to grasp than one large article. This part will deal with downloading web content, in the second one the content will be displayed and in the third, final, one I will cover the self-refreshing which was the most difficult to figure out.
Again, I believe there is more than one way to do it. What follows is how I solved it.
There is a nice built-in library in Python called urllib which we will use to download the image in question.
The code is simple:

import urllib

def getimagefromweb(url):
'''Downloads content from given url and saves it as image.'''
try:
u = urllib.urlopen(url) # open url
content = u.read() # read the openned url
u.close() # url was closed

except IOError:
print('IOError')
except:
print('Unknown url error')

# saving what was downloaded
f = open('img.jpg','wb')
f.write(content)
f.close() # file was closed


myurl = 'http://siemens.mesto-zatec.cz/obrazek.jpg'
getimagefromweb(myurl)
print 'all OK'

The (only) function "getimagefromweb" receives a url with content to be downloaded and then reads from this adress in the same way that is used when reading/writing files. When dealing with web it is very advisable to use error-catching code like this one. It is common that the website will not respond in time and without error catching your application would crash.
As a final task the function saves image to harddisk so that it can be later re-read and shown on the desktop. This is a workaroud because I could not figure out a way how to display it directly.
The url leads to the webcam of the main square of the Zatec city where I was born and have grown up. It has quite interesting medieval centre so its nice to have it shown on desktop :-)

Friday, July 4, 2008

How to make a simple .wav file with Python

EDIT: I've edited this code a little and incorporated reader's comments, here's the new version

I've recently had an idea to test some vibration-resonance problems by playing a set of sounds and detecting the vibrations in question. The glitch is, how to create a sound of desired frequency? I believe there is a lot of ways to solve this but having only a hammer (read Python) as my tool it looked like a nail ;-)

The idea is then to create a simple sine wave and play it back with a computer. My first try was with pygame module but the sound quality was poor and unsatisfactory.
If not playing sound directly one can write the wave to the file and then play the file with whatever means are suitable. This turned out to be the better way after all.
A search of the internet turned up this discussion of creating .wav files with Python. I've adopted the "minimal example" from there (written by Andrea Valle) to be usable with modern modules (it used obsolete Numeric and RandomArray modules). The code is not neat but it works well.

import numpy as N
import wave

class SoundFile:
def __init__(self, signal):
self.file = wave.open('test.wav', 'wb')
self.signal = signal
self.sr = 44100

def write(self):
self.file.setparams((1, 2, self.sr, 44100*4, 'NONE', 'noncompressed'))
self.file.writeframes(self.signal)
self.file.close()

# let's prepare signal
duration = 4 # seconds
samplerate = 44100 # Hz
samples = duration*samplerate
frequency = 440 # Hz
period = samplerate / float(frequency) # in sample points
omega = N.pi * 2 / period

xaxis = N.arange(int(period),dtype = N.float) * omega
ydata = 16384 * N.sin(xaxis)

signal = N.resize(ydata, (samples,))

ssignal = ''
for i in range(len(signal)):
ssignal += wave.struct.pack('h',signal[i]) # transform to binary

f = SoundFile(ssignal)
f.write()
print 'file written'
This code should work as intended and produce .wav files of given length and frequency. A few comments:
a) if you need different sampling frequencies, just replace all 44100 numbers with desired sampling frequency;
b) the setparams command is relatively well described in wave doc in Python help, its fourth parameter (nframes) may probably be whatever number you want, the procedure is designed to write all data you sent to it;
c) the 16384 number seems to be a volume setting for .wav file, this value is near the maximum so only lowering the volume is possible from here;
d) wave.struct.pack('h', #) - this was tough to find but without this command the resulting file will not be correct (at least on Windows machines), I do not yet understand what it does...

Thursday, July 3, 2008

Introductory and testing first post

Hello there, whoever reads this!
I've started this blog to collect my dilluted and unorganized programming and general computer knowledge. I'm not a full time programmer and all of my coding skills were self-taught. There are long periods of time when I'm not coding and having bad memory this causes me to forget anything I've ever known. After a year without programming I usually even forget the basic language commands and syntax.
This site is dedicated to preserving some of my present knowledge for future reference by anybody who may be interested in it.
I've a long history of programming languages including Basic, Pascal, C/C++, Matlab and, more recently, Python.
I'm currently a scientist-in-training and I've found Matlab to be a very fine and powerful tool. Unfortunately the price is quite prohibitive for occasionally used software and for random playing. I believe Python is a very good replacement of Matlab (at least for most of my purposes) and recommend it to anyone who might appreciate simple syntax, fast development and high portability. In connection with some add-on modules like SciPy I believe it can compete with Matlab on even grounds.
As a sideline I also intend to use this blog to learn some web-creating skills.
That'll be all for now.
R.L.