-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSZDIYPic.py
86 lines (69 loc) · 3.28 KB
/
SZDIYPic.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# Copyright (C) 2014 SZDIY Hackers' Community
#
# This file is part of szdiyCam.
#
# szdiyCam is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# szdiyCam is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with szdiyCam. If not, see <http://www.gnu.org/licenses/>.
from config import TMPDIRECTORY, imagePath
from PIL import Image, ImageDraw, ImageFont
from PictureCamera import PictureCamera
import time, os, sys
class SZDIYPic:
def __init__(self):
self.aCamera = PictureCamera() #setup a camera instance
def takeAShot(self,name,width,height):
self.aCamera.takeAShot(name,width,height);
#move the pic to tmp directory
os.system('mv '+name+' '+TMPDIRECTORY)
def __getPicTimeStampString(self,fileName, fileDirectory, stringFormat):
fileInfo = os.stat(fileDirectory+'/'+fileName)
return time.strftime(stringFormat, time.localtime(fileInfo.st_mtime))
def storeImg(self,fileName, fileDirectory):
date,time = self.__getPicTimeStampString(fileName, TMPDIRECTORY, '%Y-%m-%d %H.%M.%S').split(' ')
os.system('mkdir -p ' + imagePath+'/'+date)
os.system('cp '+fileDirectory+'/'+fileName+' '+imagePath+'/'+date+ '/' + time + '.jpg')
def __copyEXIF(self,originalImagePath,destinationImagePath):
try:
import pyexiv2
except ImportError:
print "pyexiv2 is not installed correctly, exif data won't be correct"
return
sourceImage = pyexiv2.metadata.ImageMetadata(originalImagePath)
destinationImage = pyexiv2.metadata.ImageMetadata(destinationImagePath)
sourceImage.read() #required to process the exif data
destinationImage.read() #required to process the exif data
sourceImage.copy(destinationImage)
image = Image.open(destinationImagePath)
destinationImage["Exif.Photo.PixelXDimension"] = image.size[0]
destinationImage["Exif.Photo.PixelYDimension"] = image.size[1]
destinationImage.write()
del image
def compressImageAndApplyWaterMark (self,inputFileName, outputFileName, quality=85, enWaterMark=True, **waterMarkArgs):
print "working on img"
im = Image.open(TMPDIRECTORY+'/'+inputFileName)
if enWaterMark:
timeInfo = self.__getPicTimeStampString(inputFileName, TMPDIRECTORY, '%Y-%m-%d %H:%M:%S')
#pass in or define default font parameters
fontSize = waterMarkArgs['fontSize'] if waterMarkArgs['fontSize'] else 20
hLocation = waterMarkArgs['hLocation'] if waterMarkArgs['hLocation'] else 10
vLocation = waterMarkArgs['vLocation'] if waterMarkArgs['vLocation'] else 10
# use a truetype font
fontPath = os.path.dirname(os.path.abspath(__file__))+'/'+'KellySlab-Regular.ttf'
font = ImageFont.truetype(fontPath, fontSize)
print "processing img..."
draw = ImageDraw.Draw(im)
draw.text((hLocation, vLocation), timeInfo, font=font)
del draw
im.save(TMPDIRECTORY+'/'+outputFileName, 'JPEG', quality=quality)
self.__copyEXIF(TMPDIRECTORY+'/'+inputFileName,TMPDIRECTORY+'/'+outputFileName) #copy the exif data over
del im