Last active
December 8, 2021 04:10
-
-
Save spiritedRunning/6847ab3543ab0fab0eb15a230ef65089 to your computer and use it in GitHub Desktop.
图片标注 按修改时间升序给图片加时间戳
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import cv2 | |
| import os, time | |
| samplePath = "sample" | |
| index = 0 | |
| def mark(filename): | |
| old_img_path = os.path.join(samplePath, filename) | |
| print("old_path: ", old_img_path) | |
| im = cv2.imread(old_img_path) | |
| width = int(im.shape[1] * 50 / 100) | |
| height = int(im.shape[0] * 50 / 100) | |
| resized = cv2.resize(im, (width, height), interpolation=cv2.INTER_AREA) | |
| cv2.imshow("resize", resized) | |
| modifyTime = time.ctime(os.stat(old_img_path).st_mtime) # 文件修改时间 | |
| print("modify time: ", modifyTime) | |
| # 添加时间 | |
| src_width = int(im.shape[1]) | |
| src_height = int(im.shape[0]) | |
| cv2.putText(im, modifyTime, (src_width - 600, src_height - 50), cv2.FONT_HERSHEY_COMPLEX, 1.2, (0, 0, 255), 2, | |
| cv2.LINE_AA) | |
| # 添加递增序号 | |
| global index | |
| index = index + 1 | |
| cv2.putText(im, str(index), (30, 50), cv2.FONT_HERSHEY_COMPLEX, 1.2, (0, 0, 255), 2, cv2.LINE_AA) | |
| new_filename = 'mark_' + filename | |
| mkdir(samplePath + "\\out") | |
| new_fullname = os.path.join(samplePath + "\\out", new_filename) | |
| cv2.imwrite(new_fullname, im) | |
| def sort_files(file_path): | |
| dir_list = os.listdir(file_path) | |
| if not dir_list: | |
| return | |
| else: | |
| dir_list = sorted(dir_list, key = lambda x: os.path.getmtime(os.path.join(file_path, x))) | |
| for name in dir_list: | |
| fullImagePath = os.path.join(samplePath, name) | |
| if os.path.isfile(fullImagePath): | |
| print("fullImagePath: ", fullImagePath) | |
| mark(name) | |
| def mkdir(path): | |
| folder = os.path.exists(path) | |
| if not folder: | |
| os.makedirs(path) | |
| sortedPath = sort_files(samplePath) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment