-
Notifications
You must be signed in to change notification settings - Fork 1
/
image saving code NEWEST.py
101 lines (77 loc) · 3.47 KB
/
image saving code NEWEST.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Aug 8 12:10:09 2022
@author: williamnguyen & urvinath
"""
#-------------------------------------------------------------------#
#Please put in what you want here
angleInc = float(input('Please enter your angle increment e.g 5: ')) #your angle increment e.g 5 degrees
dir_path = str(input('Please enter the directory of your record loop images. E.g E:\\Xingjian Hou\\SavingImgTest\\test: '))
number_frames = int(input('Please enter the number of frames per rotation e.g 19, 18, 17: '))
#-------------------------------------------------------------------#
import time
import os
from os import scandir, rename
from os.path import splitext, exists, join
from shutil import move
from itomUi import ItomUi
import logging
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
import os.path
# ? supported image types
image_extensions = [".jpg", ".jpeg", ".jpe", ".jif", ".jfif", ".jfi", ".png", ".gif", ".webp", ".tiff", ".tif", ".psd", ".raw", ".arw", ".cr2", ".nrw",
".k25", ".bmp", ".dib", ".heif", ".heic", ".ind", ".indd", ".indt", ".jp2", ".j2k", ".jpf", ".jpf", ".jpx", ".jpm", ".mj2", ".svg", ".svgz", ".ai", ".eps", ".ico"]
# ? supported Video types
video_extensions = [".webm", ".mpg", ".mp2", ".mpeg", ".mpe", ".mpv", ".ogg",
".mp4", ".mp4v", ".m4v", ".avi", ".wmv", ".mov", ".qt", ".flv", ".swf", ".avchd"]
# ? supported Audio types
audio_extensions = [".m4a", ".flac", "mp3", ".wav", ".wma", ".aac"]
# ? supported Document types
document_extensions = [".doc", ".docx", ".odt",
".pdf", ".xls", ".xlsx", ".ppt", ".pptx"]
#---------------------------
def make_unique(dest, name):
filename, extension = splitext(name)
counter = 1
# * IF FILE EXISTS, ADDS NUMBER TO THE END OF THE FILENAME
while exists(f"{dest}/{name}"):
name = f"{filename}({str(counter)}){extension}"
counter += 1
return name
def move_file(dest, entry, name):
if exists(f"{dest}/{name}"):
unique_name = make_unique(dest, name)
oldName = join(dest, name)
newName = join(dest, unique_name)
rename(oldName, newName)
move(entry, dest)
#function to count the number of images in the folder
count = 0
total =0
# Iterate directory
for path in os.listdir(dir_path):
# check if current path is a file
if os.path.isfile(os.path.join(dir_path, path)):
total += 1
print('Total number of frames:', total)
j=1
while number_frames*j <= total:
for path in os.listdir(dir_path):
if os.path.isfile(os.path.join(dir_path, path)):
count +=1
if count==number_frames*j:
newpath = ''+str(j*angleInc)
newpath = dir_path+str(j*angleInc)
os.makedirs(newpath)
j=j+1
for i in range(number_frames):
new_count = count-i
if os.path.exists(dir_path+'//'+str(new_count)+'.tif')==True:
oldpath = dir_path+'//'+str(new_count)+'.tif'
move_file(newpath,oldpath,oldpath)
else:
oldpath = dir_path+'//'+"0"+str(new_count)+'.tif'
move_file(newpath,oldpath,oldpath)
print('Done!. The images SHOULD be saved in different folders now.')