Skip to content
GitLab
Menu
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
ISRIC
wsm.rest
Commits
c0c5b34a
Commit
c0c5b34a
authored
Oct 06, 2020
by
turdu001
Browse files
Moved code to PIL (Pillow) library
parent
38e6ed5b
Changes
3
Hide whitespace changes
Inline
Side-by-side
imageProcessing/Original2ResizeImages.py
View file @
c0c5b34a
"""
"""
Update: Ulan Turdukulov, 6th October 2020
1) Moved code to PIL library (Pillow): pip install Pillow
2) PIL doesn't have percent resize option - used width and height of the original image
Note, some formats (jpg) require RGB conversion
From a specific folder containing tiff original files will open the file and 10per,
20_200 1per 40_40050per, 80_800, original
Notice: Please check the limits on: /etc/ImageMagick-6/policy.xml Using env variables is not working
"""
path_str
=
"""
...
...
@@ -26,20 +30,22 @@ path_str = """
"""
import
os
import
glob
import
subprocess
import
shlex
import
click
@
click
.
command
()
@
click
.
option
(
'--make_path'
,
is_flag
=
True
,
default
=
True
,
help
=
"Will generate outputs path on destination folde"
)
@
click
.
option
(
'--in_path'
,
nargs
=
1
,
type
=
click
.
Path
(
exists
=
True
),
required
=
True
,
help
=
'Path with original tiff images'
)
@
click
.
option
(
'--out_path'
,
nargs
=
1
,
type
=
click
.
Path
(
exists
=
True
),
required
=
True
,
help
=
f
'Output path for transfomed images path structure:
{
path_str
}
'
)
def
main
(
make_path
,
in_path
,
out_path
):
#def main(make_path = True, in_path = "/home/jesus/git/wsm.rest/imageProcessing/tmp3/original/tif",
# out_path="/home/jesus/git/wsm.rest/imageProcessing/final"):
formats
=
[
"jpg"
,
"png"
,
"tif"
]
folders
=
[
"10per"
,
"1per"
,
"20_200"
,
"40_400"
,
"50per"
,
"80_800"
,
"original"
]
from
PIL
import
Image
# import shlex
# import click
# @click.command()
# @click.option('--make_path', is_flag = True, default = True ,help = "Will generate outputs path on destination folde")
# @click.option('--in_path',nargs=1, type=click.Path(exists=True), required=True, help = 'Path with original tiff images')
# @click.option('--out_path', nargs=1, type=click.Path(exists=True), required=True,
# help = f'Output path for transfomed images path structure: {path_str}')
# def main(make_path, in_path, out_path):
def
main
(
make_path
=
True
,
in_path
=
"c:/Users/turdu001/wsm.rest/imageProcessing/testImages/original"
,
out_path
=
"c:/Users/turdu001/wsm.rest/imageProcessing/testImages/final"
):
formats
=
[
"jpg"
,
"png"
,
"tif"
]
folders
=
[
"10per"
,
"1per"
,
"20_200"
,
"40_400"
,
"50per"
,
"80_800"
,
"original"
]
if
make_path
:
for
folder
in
folders
:
...
...
@@ -47,37 +53,59 @@ def main(make_path, in_path, out_path):
dir_path
=
os
.
path
.
join
(
out_path
,
folder
,
format
)
if
not
os
.
path
.
exists
(
dir_path
):
os
.
makedirs
(
dir_path
)
images
=
glob
.
glob
(
os
.
path
.
join
(
in_path
,
"*.tif"
))
images
=
glob
.
glob
(
os
.
path
.
join
(
in_path
,
"*.tif"
))
for
image
in
images
:
file_name
=
os
.
path
.
splitext
(
os
.
path
.
basename
(
image
))
#
('GB-001-def', '.tif')
file_name
=
os
.
path
.
splitext
(
os
.
path
.
basename
(
image
))
#
('GB-001-def', '.tif')
file_name
=
file_name
[
0
]
image
=
Image
.
open
(
image
)
# get original width and height of the image
origin_width
=
image
.
size
[
0
]
origin_height
=
image
.
size
[
1
]
for
folder
in
folders
:
for
format
in
formats
:
dir_path
=
os
.
path
.
join
(
out_path
,
folder
,
format
)
if
folder
==
"original"
:
format_name
=
""
else
:
else
:
format_name
=
f
".
{
folder
}
"
out_file_name
=
os
.
path
.
join
(
dir_path
,
file_name
+
format_name
+
f
".
{
format
}
"
)
#NG-006-def.50per.png
#Got problems with
if
"per"
in
folder
:
per_value
=
folder
.
split
(
"per"
)[
0
]
command
=
f
"convert -delete 1--1
{
image
}
-resize
{
per_value
}
%!
{
out_file_name
}
"
if
"_"
in
folder
:
res_size
=
folder
.
replace
(
"_"
,
"x"
)
command
=
f
"convert -delete 1--1
{
image
}
-resize
{
res_size
}
!
{
out_file_name
}
"
if
"original"
in
folder
:
command
=
f
"convert -delete 1--1
{
image
}
{
out_file_name
}
"
print
(
f
"running:
{
command
}
"
)
subprocess
.
call
(
command
,
shell
=
True
)
out_file_name
=
os
.
path
.
join
(
dir_path
,
file_name
+
format_name
+
f
".
{
format
}
"
)
# NG-006-def.50per.png
if
"per"
in
folder
:
# percent resize
per_value
=
int
(
folder
.
split
(
"per"
)[
0
])
# extract percent value
width
=
int
((
per_value
/
100
)
*
origin_width
)
# change to new width
width
=
1
if
width
==
0
else
width
# if width =0 then width =1 (for small images)
height
=
int
((
per_value
/
100
)
*
origin_height
)
height
=
1
if
height
==
0
else
height
# if height =0 then height =1 (for small images)
new_image
=
image
.
resize
((
width
,
height
),
Image
.
ANTIALIAS
)
if
new_image
.
mode
!=
'RGB'
:
# check if RGB conversion is required
new_image
=
new_image
.
convert
(
'RGB'
)
new_image
.
save
(
out_file_name
)
if
"_"
in
folder
:
# pixel size resize
res_size
=
folder
.
split
(
"_"
)
# get new width & height
width
=
int
(
res_size
[
0
])
height
=
int
(
res_size
[
1
])
new_image
=
image
.
resize
((
width
,
height
),
Image
.
ANTIALIAS
)
if
new_image
.
mode
!=
'RGB'
:
# check if RGB conversion is required
new_image
=
new_image
.
convert
(
'RGB'
)
new_image
.
save
(
out_file_name
)
if
"original"
in
folder
:
# just convert and move original images
new_image
=
image
.
resize
((
origin_width
,
origin_height
),
Image
.
ANTIALIAS
)
if
new_image
.
mode
!=
'RGB'
:
# check if RGB conversion is required
new_image
=
new_image
.
convert
(
'RGB'
)
new_image
.
save
(
out_file_name
)
print
(
"all done"
)
if
__name__
==
'__main__'
:
main
()
\ No newline at end of file
main
()
imageProcessing/testImages/AU-001-def.1per.png
deleted
100644 → 0
View file @
38e6ed5b
7.46 KB
imageProcessing/testImages/AU-001-def.1per.tif
deleted
100644 → 0
View file @
38e6ed5b
File deleted
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment