from PIL import Image
import win32com.client
import sys
import os
def (imgpath):
img=Image.open(imgpath)
width,height=img.size
data=img.getdata()
datalist=list(data)
whlist=[]
j=0
for i in range(height):
whlist.append(datalist[j:(i+1)*width])
j=(i+1)*width
return whlist
def rgb_to_hex(rgb):
bgr=tuple(reversed(rgb))
strValue = '%02x%02x%02x' % bgr
iValue = int(strValue, 16)
return iValue
def creatExcel(imgdata,savename):
xl = win32com.client.Dispatch("Excel.Application")
ss = xl.Workbooks.Add()
sh = ss.ActiveSheet
xl.Visible = True
for i,line in enumerate(imgdata):
print "draw %d line" % i
for j,col in enumerate(imgdata[i]):
sh.Cells(i+1,j+1).Interior.Color=rgb_to_hex(col)
sh.Cells(i+1,j+1).RowHeight=3
sh.Cells(i+1,j+1).ColumnWidth=0.3
curdir=os.getcwd()
ss.SaveAs(curdir+"\"+savename)
ss.Close(False)
xl.Application.Quit()
def main(imgpath,savename):
imgdata=getImageRGBdata(imgpath)
creatExcel(imgdata,savename)
if __name__=="__main__":
argv=sys.argv[1:]
if not argv:
imgpath=r'E:workspacepytestimagesxxxxxxxxx.jpg'
savename=r'20151218.xlsx'
else:
imgpath=argv[0]
savename=argv[1]
main(imgpath,savename)
近期评论