-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy path图片末尾添加一行信息,如果已添加则显示信息(拖拽,多个文件或文件夹).py
54 lines (48 loc) · 1.92 KB
/
图片末尾添加一行信息,如果已添加则显示信息(拖拽,多个文件或文件夹).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
# encoding:utf-8
# https://github.com/wangandi520/andyspythonscript
from pathlib import Path
import sys
import time
def doAddMessageToImage(filePath):
# type(filePath): Path
# 设置文件类型
fileType = ['.png','.jpg']
# 需要添加的信息,当前时间time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
myMessage = '需要添加的信息' + time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
encodeMessage:bytes = myMessage.encode('utf-8')
if filePath.suffix.lower() in fileType:
with open(filePath, 'rb') as fileRead:
fileReadLine = fileRead.readlines()
if filePath.suffix.lower() == '.jpg':
if fileReadLine[-1][-2:] == b'\xff\xd9':
fileReadLine.append(b'\n')
fileReadLine.append(encodeMessage)
with open(filePath.parent.joinpath(filePath.stem + '_new' + filePath.suffix), 'wb') as fileSave:
fileSave.writelines(fileReadLine)
else:
decodeMessage = fileReadLine[-1].decode('utf-8')
print(decodeMessage)
if filePath.suffix.lower() == '.png':
if fileReadLine[-1][-1:] == b'\x82':
fileReadLine.append(b'\n')
fileReadLine.append(encodeMessage)
with open(filePath.parent.joinpath(filePath.stem + '_new' + filePath.suffix), 'wb') as fileSave:
fileSave.writelines(fileReadLine)
else:
decodeMessage = fileReadLine[-1].decode('utf-8')
print(decodeMessage)
def main(inputPath):
del inputPath[0]
for aPath in inputPath:
if Path.is_dir(Path(aPath)):
for file in Path(aPath).glob('**/*'):
doAddMessageToImage(file)
if Path.is_file(Path(aPath)):
doAddMessageToImage(Path(aPath))
input('按回车键退出')
if __name__ == '__main__':
try:
if len(sys.argv) >= 2:
main(sys.argv)
except IndexError:
pass