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

No comments: