#! /usr/bin/env python
# -*- coding:utf-8 -*-
import os
import sys
import shutil
import json
import urllib3
import csv
import subprocess
import fnmatch
import datetime
import struct
import Image
import scipy
import scipy.misc
import scipy.cluster
NUM_CLUSTERS = 5
def getDominantColor(image):
# print 'reading image'
# im = Image.open('image.jpg')
# im = Image.open('/tmp/image01.jpg')
im = Image.open(image)
im = im.resize((150,
150)) # optional, to reduce
time
ar = scipy.misc.fromimage(im)
shape = ar.shape
ar =
ar.reshape(scipy.product(shape[:2]), shape[2])
# print 'finding clusters'
codes, dist =
scipy.cluster.vq.kmeans(ar, NUM_CLUSTERS)
# print 'cluster centres:\n', codes
vecs, dist = scipy.cluster.vq.vq(ar,
codes) #
assign codes
counts, bins = scipy.histogram(vecs,
len(codes)) # count occurrences
index_max =
scipy.argmax(counts)
# find most frequent
peak = codes[index_max]
colour = ''.join(chr(c) for c in
peak).encode('hex')
# print 'most frequent is %s (#%s)' %
(peak, colour)
return peak.tolist()
def makeRGB(code):
return
'rgb({0:d},{1:d},{2:d})'.format(code[0],code[1],code[2])
def delImages( path ):
# print fnmatch.filter(os.listdir(path),
'image*.jpg')
flist=fnmatch.filter(os.listdir(path),
'image*.jpg')
# print len(flist)
for file in flist :
delfile = path
+ "/" + file
# print
'delImages>'+delfile
os.remove(delfile)
if __name__ == '__main__':
CurDir=sys.argv[0]
# print 'CurDir>'+CurDir
DirName=os.path.dirname(CurDir)
# print 'DirName>'+DirName
if not DirName:
DirName="."
shell_file=DirName+"/convert.sh"
# print 'shell_file>'+shell_file
csv_file='/tmp/param.csv'
#天気情報取得
city='230010' #
名古屋
# city='130010' # 東京
# city='270000' # 大阪
url =
'http://weather.livedoor.com/forecast/webservice/json/v1?city={0:s}'.format(city)
http = urllib3.PoolManager()
resp = http.request('GET', url)
data =
json.loads(resp.data.decode('utf-8'))
# print data
# print(json.dumps(data, sort_keys=True,
indent=4))
#日付と地域
# print(data['publicTime'][0:10])
# print(data['title'])
csv0=data['publicTime'][0:10]
csv0=csv0+' '+data['title']
#今日の天気
#
print(data['forecasts'][0]['dateLabel'])
# print(data['forecasts'][0]['telop'])
csv1=data['forecasts'][0]['dateLabel']
csv1=csv1+'
'+data['forecasts'][0]['telop']
#明日の天気
#
print(data['forecasts'][1]['dateLabel'])
# print(data['forecasts'][1]['telop'])
csv2=data['forecasts'][1]['dateLabel']
csv2=csv2+'
'+data['forecasts'][1]['telop']
#明後日の天気(無い時がある)
wk1 = datetime.datetime.now() +
datetime.timedelta(days=+2)
wk2 = '%s' % ( wk1.strftime( '%Y-%m-%d'
) )
# print 'wk2>'+wk2
if wk2 in json.dumps(data,
sort_keys=True, indent=4):
#
print(data['forecasts'][2]['dateLabel'])
#
print(data['forecasts'][2]['telop'])
csv3=data['forecasts'][2]['dateLabel']
csv3=csv3+'
'+data['forecasts'][2]['telop']
else:
csv3=" "
#CSVファイル作成
fd=open(csv_file,'w')
writer = csv.writer(fd)
csvdata=[csv0.encode('utf-8'),csv1.encode('utf-8'),csv2.encode('utf-8'),csv3.encode('utf-8')]
writer.writerow(csvdata)
fd.close()
in_path="/tmp"
out_path="/tmp/out"
if not os.path.exists(out_path):
os.mkdir(out_path)
else:
delImages(out_path)
#イメージ処理開始
# print
fnmatch.filter(os.listdir(in_path), 'image*.jpg')
flist=fnmatch.filter(os.listdir(in_path), 'image*.jpg')
for file in flist :
in_file =
in_path + "/" + file
# print
'in_file>'+in_file
out_file =
out_path + "/" + file
# print
'out_file>'+out_file
#
#イメージの近似色を求める
args=['convert','-crop','1500x400+50+50',in_file,out_file]
subprocess.call(args)
dominant=getDominantColor(out_file)
# print
'out_file dominant>'+str(dominant)
#フォントの色を決定
maxpos=max(enumerate(dominant), key=lambda dominant:
dominant[1])[0]
# print maxpos
fill_color='rgb(255,255,255)' #white
if
dominant[0]>100 and dominant[1]>100 and
dominant[2]>100:
fill_color='rgb(0,0,0)' #black
elif maxpos==0
and dominant[maxpos]>60:
fill_color='rgb(0,255,255)' #cyan
elif maxpos==1
and dominant[maxpos]>60:
fill_color='rgb(255,0,0)' #red
elif maxpos==2
and dominant[maxpos]>60:
fill_color='rgb(255,0,255)' #pink
#フォントの色を固定色にする場合はこちらを有効にする
#
fill_color='rgb(0,0,0)' #black
#
fill_color='rgb(255,255,255)' #white
#文字編集
# print
'in_file>'+in_file
# print
'out_file>'+out_file
# print
'fill_color>'+fill_color
args=[shell_file,csv_file,in_file,out_file,fill_color]
subprocess.call(args)
|