Skip to content

Commit 4cf5208

Browse files
authored
Merge pull request #8 from achimoraites/fix/webp-support
fix(image-converter): support .webp images
2 parents f4fd0cc + dd202b4 commit 4cf5208

File tree

2 files changed

+163
-30
lines changed

2 files changed

+163
-30
lines changed

.gitignore

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
/venv
2+
/images
3+
/converted
4+
5+
# Byte-compiled / optimized / DLL files
6+
__pycache__/
7+
*.py[cod]
8+
*$py.class
9+
10+
# C extensions
11+
*.so
12+
13+
# Distribution / packaging
14+
.Python
15+
build/
16+
develop-eggs/
17+
dist/
18+
downloads/
19+
eggs/
20+
.eggs/
21+
lib/
22+
lib64/
23+
parts/
24+
sdist/
25+
var/
26+
wheels/
27+
pip-wheel-metadata/
28+
share/python-wheels/
29+
*.egg-info/
30+
.installed.cfg
31+
*.egg
32+
MANIFEST
33+
34+
# PyInstaller
35+
# Usually these files are written by a python script from a template
36+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
37+
*.manifest
38+
*.spec
39+
40+
# Installer logs
41+
pip-log.txt
42+
pip-delete-this-directory.txt
43+
44+
# Unit test / coverage reports
45+
htmlcov/
46+
.tox/
47+
.nox/
48+
.coverage
49+
.coverage.*
50+
.cache
51+
nosetests.xml
52+
coverage.xml
53+
*.cover
54+
*.py,cover
55+
.hypothesis/
56+
.pytest_cache/
57+
58+
# Translations
59+
*.mo
60+
*.pot
61+
62+
# Django stuff:
63+
*.log
64+
local_settings.py
65+
db.sqlite3
66+
db.sqlite3-journal
67+
68+
# Flask stuff:
69+
instance/
70+
.webassets-cache
71+
72+
# Scrapy stuff:
73+
.scrapy
74+
75+
# Sphinx documentation
76+
docs/_build/
77+
78+
# PyBuilder
79+
target/
80+
81+
# Jupyter Notebook
82+
.ipynb_checkpoints
83+
84+
# IPython
85+
profile_default/
86+
ipython_config.py
87+
88+
# pyenv
89+
.python-version
90+
91+
# pipenv
92+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
93+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
94+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
95+
# install all needed dependencies.
96+
#Pipfile.lock
97+
98+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow
99+
__pypackages__/
100+
101+
# Celery stuff
102+
celerybeat-schedule
103+
celerybeat.pid
104+
105+
# SageMath parsed files
106+
*.sage.py
107+
108+
# Environments
109+
.env
110+
.venv
111+
env/
112+
venv/
113+
ENV/
114+
env.bak/
115+
venv.bak/
116+
117+
# Spyder project settings
118+
.spyderproject
119+
.spyproject
120+
121+
# Rope project settings
122+
.ropeproject
123+
124+
# mkdocs documentation
125+
/site
126+
127+
# mypy
128+
.mypy_cache/
129+
.dmypy.json
130+
dmypy.json
131+
132+
# Pyre type checker
133+
.pyre/

raw_image_converter/__main__.py

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@
1717

1818

1919
# create a message function
20-
def message(file, bool):
20+
def message(file, converted):
2121
screenLock.acquire()
2222
# if is converted
23-
if bool:
23+
if converted:
2424
print(datetime.now().time().strftime('%H:%M:%S') + " Converted: " + file)
2525
else:
2626
print(datetime.now().time().strftime('%H:%M:%S') + " Converting: " + file)
@@ -32,7 +32,6 @@ def message(file, bool):
3232
os.makedirs(directory)
3333

3434

35-
3635
# convert RAW images function
3736
def convert_raw(file, directory, tgtDir, extension=".jpg"):
3837
# path = 'image.nef'
@@ -49,16 +48,17 @@ def convert_raw(file, directory, tgtDir, extension=".jpg"):
4948

5049

5150
# convert function
52-
def convert_file(file, directory, tgtDir):
51+
def convert_file(file, directory, tgtDir, extension=".jpg"):
52+
mappings = {
53+
'.jpg': 'JPEG',
54+
'.png': 'PNG',
55+
}
56+
save_format = mappings.get(extension, 'JPEG')
5357
try:
5458
message(file, False)
5559
path = os.path.join(tgtDir, file)
5660
im = Image.open(path)
57-
# basewidth = 2048
58-
# wpercent = (basewidth/float(im.size[0]))
59-
# hsize = int((float(im.size[1])*float(wpercent)))
60-
# im = im.resize((basewidth,hsize), Image.ANTIALIAS)
61-
im.save(os.path.join(directory, file + ".jpg"), "JPEG", dpi=(600, 600))
61+
im.save(os.path.join(directory, file + extension), save_format, dpi=(600, 600))
6262
message(file, True)
6363

6464
except:
@@ -85,13 +85,13 @@ def image_not_exists(e):
8585

8686

8787
# here we check each file to decide what to do
88-
def checkExtension(ext):
88+
def check_extension(ext):
8989
# set supported raw conversion extensions!
9090
extensionsForRawConversion = ['.dng', '.raw', '.cr2', '.crw', '.erf', '.raf', '.tif', '.kdc', '.dcr', '.mos',
9191
'.mef', '.nef', '.orf', '.rw2', '.pef', '.x3f', '.srw', '.srf', '.sr2', '.arw',
9292
'.mdc', '.mrw']
9393
# set supported imageio conversion extensions
94-
extensionsForConversion = ['.ppm', '.psd', '.tif']
94+
extensionsForConversion = ['.ppm', '.psd', '.tif', '.webp']
9595

9696
for i in extensionsForRawConversion:
9797
if ext.lower().endswith(i):
@@ -107,20 +107,21 @@ def checkExtension(ext):
107107
def main():
108108
print('### PYTHON IMAGE CONVERTER ### \n \n')
109109

110-
parser = optparse.OptionParser("usage: " + sys.argv[0] + \
111-
"\n-s <source directory> \n ex: usage%prog -s C:\\Users\\USerName\\Desktop\\Photos_Dir \n After -s Specify the directory you will convert")
112-
parser.add_option('--s', dest='nname', type='string', \
113-
help='specify your source directory!')
114-
parser.add_option('--ext', dest='target_image_extension', type='choice', \
115-
default=".jpg", choices = ['.jpg', '.png'], help='the image format to be used for the converted images.')
110+
parser = optparse.OptionParser("usage: " + sys.argv[0] +
111+
"\n-s <source directory> \n ex: usage%prog --s "
112+
"C:\\Users\\USerName\\Desktop\\Photos_Dir \n After --s Specify the directory you "
113+
"will convert")
114+
parser.add_option('--s', dest='nname', type='string', help='specify your source directory!')
115+
parser.add_option('--ext', dest='target_image_extension', type='choice',
116+
default=".jpg", choices=['.jpg', '.png'],
117+
help='the image format to be used for the converted images.')
116118
(options, args) = parser.parse_args()
117-
if (options.nname == None):
119+
if options.nname is None:
118120
print(parser.usage)
119121
exit(0)
120122
else:
121123
tgtDir = os.path.abspath(options.nname)
122124

123-
124125
print("Started conversion at : " + datetime.now().time().strftime('%H:%M:%S') + '\n')
125126
print("Converting \n -> " + tgtDir + " Directory !\n")
126127
# find files to convert
@@ -129,21 +130,20 @@ def main():
129130
for file in os.listdir(tgtDir):
130131
# CHECK IF WE HAVE CONVERTED THIS IMAGE! IF YES SKIP THE CONVERSIONS!
131132
if image_not_exists(file):
132-
if 'RAW' == checkExtension(file):
133-
# Added multithreds to complete conversion faster
133+
if 'RAW' == check_extension(file):
134+
# Added multithreading to complete conversion faster
134135
t2 = Thread(target=convert_raw, args=(file, directory, tgtDir, options.target_image_extension))
135-
t2.start();
136-
137-
if 'NOT_RAW' == checkExtension(file):
138-
t = Thread(target=convert_file, args=(file, directory, tgtDir))
139-
t.start();
140-
if file.endswith('.tif'):
141-
t = Thread(target=convert_file, args=(file, directory, tgtDir))
142-
t.start();
136+
t2.start()
137+
138+
if 'NOT_RAW' == check_extension(file):
139+
t = Thread(target=convert_file, args=(file, directory, tgtDir, options.target_image_extension))
140+
t.start()
141+
143142
print(" \n Converted Images are stored at - > \n " + os.path.abspath(directory))
144143
except:
145144
print(
146-
"\n The directory at : \n " + tgtDir + " \n Are you sure is there? \n I am NOT! \n It NOT EXISTS !! Grrrr....\n\n")
145+
"\n The directory at : \n " + tgtDir + "\n Are you sure is there? \n I am NOT! \n It NOT EXISTS !! "
146+
"Grrrr....\n\n")
147147

148148

149149
if __name__ == '__main__':

0 commit comments

Comments
 (0)