This repository was archived by the owner on Apr 22, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinvert.py
More file actions
executable file
·90 lines (78 loc) · 2.82 KB
/
invert.py
File metadata and controls
executable file
·90 lines (78 loc) · 2.82 KB
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
#!/usr/bin/python3
from os import listdir, system, mkdir, getcwd
from os.path import isfile, join, realpath
import argparse
# { ^ ; ^ }
# ___
# way to getting absolute path to the script and its Name
abspath = realpath(__file__)
abspath, sname = abspath.rsplit('/',1)
abspath += '/'
# abspath - absolute path to the folder conatinging script(absoulute means from root folder / )
# sname - name of script (if it would be changed by someone)
#print('sname: ',sname)
#print('abspath: ', abspath)
standardExtension = ['.png','.jpg','.jpeg','.gif','.tiff']
def isDotExt(name,extensions):
for i in extensions:
if i in str(name):
return True
return False
def findmany(string,find):
many = 0
for i in string:
if i == find:
many += 1
return many
# activating args parser
parser = argparse.ArgumentParser()
# help text for flag parser
helpDir = "Name for new or existing directory for inverted pictures to be put in"
helpExt = "Name of one extension by which script will filter puctures and convert only those with given extension"
helpApExt = 'Add extension over to the standard extension, repetive extension will be anihilated.'
helpPic = "You can choose to invert one picture, make sure you write picture name with extension."
# parsing flags
parser.add_argument("-dn", "--directoryname", type = str, default = "inverted/", help = helpDir)
parser.add_argument("-e", "--extension", type = str, nargs='+', default = None, help = helpExt)
parser.add_argument('-ae', '--appendextension', nargs='+', type=str, default = None, help = helpApExt)
parser.add_argument("-p", "--picture", type = str, nargs='+', default = None, help = helpPic)
args = parser.parse_args()
#
#extension checking module, trust the user (no don't trust him)
if args.appendextension != None:
if args.extension == None:
dotExtension = standardExtension + args.appendextension
else:
dotExtension = args.extension
else:
if args.extension == None:
dotExtension = standardExtension
else:
dotExtension = args.extension
#checking if user given picture as argument and parsing it if not downloading it from listdir
if args.picture:
pic = list()
for picture in args.picture:
if '/' in picture:
tpic = picture.rsplit('/',1)[1]
if tpic != '' and findmany(tpic,'.') == 1 and isDotExt(tpic,dotExtension):
pic.append(tpic)
else:
pic = [file for file in listdir(abspath) if isfile(join(file)) and isDotExt(file,dotExtension)]
#print(type(pic))
#print(pic)
#checking landing directory name
if args.directoryname == 'inverted/':
dn = args.directoryname
else:
dn = args.directoryname.replace('/','')
dn += '/'
#print(dn)
#checking directory existance
if dn.split('/')[0] in listdir(abspath):
print('Directory '+dn+' of that name exist in this folder.')
else:
mkdir(abspath+dn)
#conversion itself, and saving, as we call magick command
for i in pic:
system('magick '+i+' -negate '+dn+i)