Skip to content

Commit fa78cdc

Browse files
authored
Merge pull request #3 from achimoraites/refactor/conversion
refactor: target image format
2 parents d72093a + 46c63e8 commit fa78cdc

File tree

3 files changed

+21
-23
lines changed

3 files changed

+21
-23
lines changed

README.md

+14-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
# Python Image Converter
2-
## This application is designed to run with Python 2.7
32

43
## This is a simple Image converter written in Python
54
The app is using PIL ,rawpy and imageio .
@@ -17,27 +16,33 @@ The .ai files are renamed as .pdf and moved to the converted directory !!!
1716
Personally i convert a lot of .psd , .TIF and .dng files !!!
1817
## Speed
1918
This script is multithreaded and checks if you have already converted an image!
19+
2020
### Before you can use it Install :
21-
```
22-
pip install Image
23-
pip install rawpy
24-
pip install imageio
25-
21+
```python
22+
pip install Pillow rawpy imageio
2623
```
2724
### Example usage
2825

2926
```
3027
git clone https://github.com/Cyb3rN4u7/Python-Image-Converter.git [my-app-name]
3128
cd [my-app-name]
3229
cd app
33-
python convert.py -s <Enter-Path-Of-Directory>
30+
31+
# simple usage
32+
python convert.py --s <Enter-Path-Of-Directory>
33+
34+
# set a custom target image format
35+
python convert.py --s <Enter-Path-Of-Directory> --ext '.png'
3436
```
35-
* The -s argument is where you set the path to the directory you want to convert!
37+
- The --s argument is where you set the path to the directory you want to convert!
38+
- The --ext argument is where you specify the image format that will be used for the converted images; by default the `.jpg` is used. valid options are:
39+
- `.jpg`
40+
- `.png`
3641

3742
The application will create a folder 'converted' where all your converted images are located!
3843

3944
And you are done!
4045

4146
## ScreenShot
42-
<img src='app/img/sample.png' alt='Python Image Converter'>
47+
<img src='sample.png' alt='Python Image Converter'>
4348

app/convert.py

+7-14
Original file line numberDiff line numberDiff line change
@@ -34,25 +34,15 @@ def message(file, bool):
3434

3535

3636
# convert RAW images function
37-
def convert_raw(file, directory, tgtDir):
37+
def convert_raw(file, directory, tgtDir, extension=".jpg"):
3838
# path = 'image.nef'
3939

4040
try:
4141
message(file, False)
4242
path = os.path.join(tgtDir, file)
4343
with rawpy.imread(path) as raw:
4444
rgb = raw.postprocess()
45-
imageio.imsave(os.path.join(directory, file + '.jpg'), rgb)
46-
message(file, True)
47-
except:
48-
pass
49-
50-
try:
51-
message(file, False)
52-
path = os.path.join(tgtDir, file)
53-
with rawpy.imread(path) as raw:
54-
rgb = raw.postprocess()
55-
imageio.imsave(os.path.join(directory, file + '.png'), rgb)
45+
imageio.imsave(os.path.join(directory, file + extension), rgb)
5646
message(file, True)
5747
except:
5848
pass
@@ -119,15 +109,18 @@ def main():
119109

120110
parser = optparse.OptionParser("usage: " + sys.argv[0] + \
121111
"\n-s <source directory> \n ex: usage%prog -s C:\\Users\\USerName\\Desktop\\Photos_Dir \n After -s Specify the directory you will convert")
122-
parser.add_option('-s', dest='nname', type='string', \
112+
parser.add_option('--s', dest='nname', type='string', \
123113
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.')
124116
(options, args) = parser.parse_args()
125117
if (options.nname == None):
126118
print(parser.usage)
127119
exit(0)
128120
else:
129121
tgtDir = os.path.abspath(options.nname)
130122

123+
131124
print("Started conversion at : " + datetime.now().time().strftime('%H:%M:%S') + '\n')
132125
print("Converting \n -> " + tgtDir + " Directory !\n")
133126
# find files to convert
@@ -138,7 +131,7 @@ def main():
138131
if image_not_exists(file):
139132
if 'RAW' == checkExtension(file):
140133
# Added multithreds to complete conversion faster
141-
t2 = Thread(target=convert_raw, args=(file, directory, tgtDir))
134+
t2 = Thread(target=convert_raw, args=(file, directory, tgtDir, options.target_image_extension))
142135
t2.start();
143136

144137
if 'NOT_RAW' == checkExtension(file):
File renamed without changes.

0 commit comments

Comments
 (0)