Thursday, March 27, 2008

ImageMagick to increase contrast

convert -contrast-stretch 0%x30% SS01565.tif sscs5.tif

Wednesday, March 26, 2008

#Python Script for put water mark annontation on image

#Python Script for put water mark annontation on image
#John Zhou MWH Global 12/19/2006
#Not for other use
#need Python and PIL Installed

import win32com.client
import fnmatch
import string
import os
import Image,ImageDraw,ImageFont,sys
engine = win32com.client.Dispatch("DAO.DBEngine.36")

strPath="E:\\PWP_WaterMark\\"
#Determine the path to your database and use the following syntax:
#make sure the database name
db = engine.OpenDatabase(strPath+"ConsolidatedGPS_18Dec2006.mdb")
totalNum=0
ferr=open("c:\\err.txt","w")
fsuccess=open("c:\\success.txt","w")
for fileName in os.listdir ( strPath+"Field Photos"):
if fnmatch.fnmatch ( fileName, '*.jpg' ) :
path, name = os.path.split (fileName)
queryString="select PoleFacilityID, Address, PhotoLocation from Pole where PhotoLocation like '*" +name+"*'"
# print queryString
x=1
rs = db.OpenRecordset( queryString )
Anno=""
while not rs.EOF and x<2:
Anno= rs.Fields("PoleFacilityID").Value+"|"+rs.Fields("Address").Value+ "|"+name
rs.MoveNext
x=x+1
strFileName = strPath+"Field Photos\\"+fileName.upper()
totalNum=totalNum+1
if (len(Anno)>0):
fsuccess.write(strFileName+"\n")
else:
ferr.write(strFileName+"\n")
# print strFileName
im1=Image.open(strFileName)
im2=Image.new(im1.mode,im1.size)
font = ImageFont.truetype("arial.ttf", 16)
textsize=font.getsize(Anno)
for mySize in range(18,100,2):
if textsize[0]>=im1.size[0]-im1.size[0]*0.2:
break
font = ImageFont.truetype("arial.ttf", mySize)
textsize=font.getsize(Anno)

draw2=ImageDraw.Draw(im2)
draw2.rectangle([0,im2.size[1],im2.size[0],im2.size[1]-textsize[1]],fill=128)
im3=Image.blend(im1,im2,0.2)
draw3=ImageDraw.Draw(im3)
draw3.text(((im3.size[0]-textsize[0])/2, im3.size[1]-textsize[1]), Anno, font=font)
im3.save(strFileName,"JPEG")
print totalNum
ferr.close()
fsuccess.close()


#rs = db.OpenRecordset("select PoleFacilityID, Address, PhotoLocation from Pole")
# db.Execute("delete * from customers where balancetype = 'overdue' and name = 'bill'")
#while not rs.EOF:
# print rs.Fields("PoleFacilityID").Value
# rs.MoveNext

















#Python Script for put water mark annontation on image
# where PhotoLocation like '*PWP*'
#Instantiate the Jet engine.
import win32com.client
import fnmatch
import string
import os
import Image,ImageDraw,ImageFont,sys
engine = win32com.client.Dispatch("DAO.DBEngine.36")
#Determine the path to your database and use the following syntax:
db = engine.OpenDatabase(r"C:\Documents and Settings\John Zhou\Desktop\ElectricArea3.mdb")
#rs = db.OpenRecordset("pole")
#You can also do the following
x=1
for fileName in os.listdir ( r"C:\Documents and Settings\John Zhou\Desktop\Field Photos"):
if fnmatch.fnmatch ( fileName, '*.jpg' ):
path, name = os.path.split (fileName)
queryString="select PoleFacilityID, Address, PhotoLocation from Pole where PhotoLocation like '*" +name+"*'"
# print queryString
x=1
rs = db.OpenRecordset( queryString )
Anno=""
while not rs.EOF and x<2:
Anno= rs.Fields("PoleFacilityID").Value+"|"+rs.Fields("Address").Value+ "|"+name
rs.MoveNext
x=x+1
# if len(Anno) = 0:
# break
strFileName = "C:\\Documents and Settings\\John Zhou\\Desktop\\Field Photos\\"+fileName.upper()
print strFileName
im1=Image.open(strFileName)
im2=Image.new(im1.mode,im1.size)
font = ImageFont.truetype("arial.ttf", 16)
textsize=font.getsize(Anno)
for mySize in range(18,100,2):
if textsize[0]>=im1.size[0]-im1.size[0]*0.2:
break
font = ImageFont.truetype("arial.ttf", mySize)
textsize=font.getsize(Anno)

draw2=ImageDraw.Draw(im2)
draw2.rectangle([0,im2.size[1],im2.size[0],im2.size[1]-textsize[1]],fill=128)
im3=Image.blend(im1,im2,0.2)
draw3=ImageDraw.Draw(im3)
draw3.text(((im3.size[0]-textsize[0])/2, im3.size[1]-textsize[1]), Anno, font=font)
im3.save("c:\\"+name,"JPEG")



#rs = db.OpenRecordset("select PoleFacilityID, Address, PhotoLocation from Pole")
# db.Execute("delete * from customers where balancetype = 'overdue' and name = 'bill'")
#while not rs.EOF:
# print rs.Fields("PoleFacilityID").Value
# rs.MoveNext

Tuesday, March 25, 2008

Sort Domain

http://edndoc.esri.com/arcobjects/9.2/CPP_VB6_VBA_VCPP_Doc/COM_Samples_Docs/Geodatabase/Schema_Creation_and_Management/Sort_a_domain/e826c5a8-9740-4f0b-86b6-d3b834735574.htm

Thursday, March 20, 2008

XML reformatting

XML reformatting

http://www.stahlforce.com/dev/index.php?tool=xmlstar

Thursday, March 6, 2008

remove blankline awk

Many times of output of some packages contain lot of blank lines. You can remove those blank line using awk as follows:

Assume that /tmp/test is the file containing blank lines

cat /tmp/test | awk '$0!~/^$/ {print $0}' > /tmp/test1

This script is to exclude blank line ^ represent beginning of a line, $ represents end of a line, therefore ^$ stands for a line without any contents. ! stands for not.

It could be done by grep in the similar way. grep -v "^$" filename > newfilename
-v stands for "not"

Tuesday, March 4, 2008

quick imagemagick command to identify massive files resolution and size

G:\MWH_PWP Water Sources\WorkOrders>identify -format "%f,%x,%y,%p,%P" -quiet *.tif>D:\workorder.txt

scripts to read and write BLOB in MS Access

Const Blocksize = 32768

'**********************************************************************
'FUNCTION: ReadBLOB()
'
'PURPOSE:
'Reads a BLOB from a disk file and stores the contents in the specified
'table and field.
'
'PREREQUISITES:
'The specified table with the OLE object field to contain the binary
'data must be opened in Visual Basic code and prepared for a new record.
'
'ARGUMENTS:
'Source - the path and filename of the file to be stored.
'T - the table object to store the data in.
'Field - the OLE object to store the
'RETURN:
'0 on fail 1 on success
'**********************************************************************

Public Function ReadBLOB(Source As String, T As Recordset, sField As String)

Dim NumBlocks As Integer, SourceFile As Integer, i As Integer
Dim FileLength As Long, LeftOver As Long
Dim FileData() As Byte, RetVal As Variant

On Error GoTo Err_ReadBLOB

SourceFile = FreeFile
Open Source For Binary Access Read As SourceFile
FileLength = LOF(SourceFile)
If FileLength <> 0 Then
NumBlocks = FileLength \ Blocksize
LeftOver = FileLength Mod Blocksize 'remainder appended first
'initialize status bar meter.
RetVal = SysCmd(acSysCmdInitMeter, "Reading BLOB", FileLength \ 1000)
ReDim FileData(LeftOver)
Get SourceFile, , FileData()
T(sField).AppendChunk FileData() 'store the first image chunk
RetVal = SysCmd(acSysCmdUpdateMeter, LeftOver / 1000)
ReDim FileData(Blocksize)
For i = 1 To NumBlocks
Get SourceFile, , FileData()
T(sField).AppendChunk FileData() 'remaining chunks
'update status bar meter
RetVal = SysCmd(acSysCmdUpdateMeter, Blocksize * i / 1000)
Next i
'remove status bar meter
RetVal = SysCmd(acSysCmdRemoveMeter)
End If
Close SourceFile
ReadBLOB = 1
Exit Function

Err_ReadBLOB:
MsgBox Err.Description
ReadBLOB = 0
Exit Function

End Function

'**********************************************************************
'FUNCTION: WriteBLOB()
'
'PURPOSE:
'WritesBLOB information stored in the specified table and field to the
'specified disk file.
'
'PREREQUISITES:
'
'ARGUMENTS:
'Destination - the path and filename of the file to be extracted.
'T - the table object the data is stored in.
'Field - the OLE object to store the data in.
'
'RETURN:
'0 on fail 1 on success
'**********************************************************************

Public Function WriteBLOB(T As Recordset, sField As String, Destination As String)

Dim NumBlocks As Integer, DestFile As Integer, i As Integer
Dim FileLength As Long, LeftOver As Long
Dim FileData() As Byte, RetVal As Variant

On Error GoTo Err_WriteBLOB

' Get the length of the file.
FileLength = T(sField).ActualSize()
If FileLength <> 0 Then
DestFile = FreeFile
NumBlocks = FileLength \ Blocksize
LeftOver = FileLength Mod Blocksize 'reminder appended first
'initialize status bar meter
RetVal = SysCmd(acSysCmdInitMeter, "Writing BLOB", NumBlocks)
Open Destination For Binary Access Write Lock Write As DestFile
ReDim FileData(LeftOver)
FileData() = T(sField).GetChunk(LeftOver)
Put DestFile, , FileData() 'write first chunk
ReDim FileData(Blocksize)
For i = 1 To NumBlocks
FileData() = T(sField).GetChunk(Blocksize)
Put DestFile, , FileData() 'write remaining chunks
'update status bar meter
RetVal = SysCmd(acSysCmdUpdateMeter, i)
Next i
Close DestFile
End If
'remove status bar meter
RetVal = SysCmd(acSysCmdRemoveMeter)
WriteBLOB = 1
Exit Function

Err_WriteBLOB:
MsgBox Err.Description
WriteBLOB = 0
Exit Function

End Function



Sub test()

Dim Cnn As ADODB.Connection
Dim Rst As ADODB.Recordset
Set Cnn = New ADODB.Connection
Cnn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0; " & "Data Source=E:\PWP Program\Task C Data Conversion\C.3 Power DC\Perform Full Data Conversion\Area2\PWP_Electric_2_test.mdb"
Cnn.Open
'Cnn.Execute ("SELECT CONFIGURATION FROM ConduitSection WHERE OBJECTID=78723;")

'Open a recordest based on the open connection
' Set Rst = New ADODB.Recordset

' Set Rst.ActiveConnection = Cnn
Set Rst = Cnn.Execute("SELECT CONFIGURATION FROM ConduitSection WHERE OBJECTID=78723;")
Rst.MoveFirst
MsgBox Rst.RecordCount

WriteBLOB Rst, "CONFIGURATION", "C:\testblog"

End Sub