Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
OpenDAS
vision
Commits
ef67fd92
Commit
ef67fd92
authored
Sep 23, 2019
by
Surgan Jandial
Committed by
Francisco Massa
Sep 23, 2019
Browse files
Adding File object option to utils.save_image (#1301)
* format param added * lint fixes * lint fixes * lint fixes
parent
367e8514
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
29 additions
and
3 deletions
+29
-3
test/test_utils.py
test/test_utils.py
+23
-0
torchvision/utils.py
torchvision/utils.py
+6
-3
No files found.
test/test_utils.py
View file @
ef67fd92
...
...
@@ -4,6 +4,9 @@ import tempfile
import
torch
import
torchvision.utils
as
utils
import
unittest
from
io
import
BytesIO
import
torchvision.transforms.functional
as
F
from
PIL
import
Image
class
Tester
(
unittest
.
TestCase
):
...
...
@@ -52,6 +55,26 @@ class Tester(unittest.TestCase):
utils
.
save_image
(
t
,
f
.
name
)
assert
os
.
path
.
exists
(
f
.
name
),
'The pixel image is not present after save'
def
test_save_image_file_object
(
self
):
with
tempfile
.
NamedTemporaryFile
(
suffix
=
'.png'
)
as
f
:
t
=
torch
.
rand
(
2
,
3
,
64
,
64
)
utils
.
save_image
(
t
,
f
.
name
)
img_orig
=
Image
.
open
(
f
.
name
)
fp
=
BytesIO
()
utils
.
save_image
(
t
,
fp
,
format
=
'png'
)
img_bytes
=
Image
.
open
(
fp
)
assert
torch
.
equal
(
F
.
to_tensor
(
img_orig
),
F
.
to_tensor
(
img_bytes
)),
'Image not stored in file object'
def
test_save_image_single_pixel_file_object
(
self
):
with
tempfile
.
NamedTemporaryFile
(
suffix
=
'.png'
)
as
f
:
t
=
torch
.
rand
(
1
,
3
,
1
,
1
)
utils
.
save_image
(
t
,
f
.
name
)
img_orig
=
Image
.
open
(
f
.
name
)
fp
=
BytesIO
()
utils
.
save_image
(
t
,
fp
,
format
=
'png'
)
img_bytes
=
Image
.
open
(
fp
)
assert
torch
.
equal
(
F
.
to_tensor
(
img_orig
),
F
.
to_tensor
(
img_bytes
)),
'Pixel Image not stored in file object'
if
__name__
==
'__main__'
:
unittest
.
main
()
torchvision/utils.py
View file @
ef67fd92
...
...
@@ -88,13 +88,16 @@ def make_grid(tensor, nrow=8, padding=2,
return
grid
def
save_image
(
tensor
,
f
ilename
,
nrow
=
8
,
padding
=
2
,
normalize
=
False
,
range
=
None
,
scale_each
=
False
,
pad_value
=
0
):
def
save_image
(
tensor
,
f
p
,
nrow
=
8
,
padding
=
2
,
normalize
=
False
,
range
=
None
,
scale_each
=
False
,
pad_value
=
0
,
format
=
None
):
"""Save a given Tensor into an image file.
Args:
tensor (Tensor or list): Image to be saved. If given a mini-batch tensor,
saves the tensor as a grid of images by calling ``make_grid``.
fp - A filename(string) or file object
format(Optional): If omitted, the format to use is determined from the filename extension.
If a file object was used instead of a filename, this parameter should always be used.
**kwargs: Other arguments are documented in ``make_grid``.
"""
from
PIL
import
Image
...
...
@@ -103,4 +106,4 @@ def save_image(tensor, filename, nrow=8, padding=2,
# Add 0.5 after unnormalizing to [0, 255] to round to nearest integer
ndarr
=
grid
.
mul
(
255
).
add_
(
0.5
).
clamp_
(
0
,
255
).
permute
(
1
,
2
,
0
).
to
(
'cpu'
,
torch
.
uint8
).
numpy
()
im
=
Image
.
fromarray
(
ndarr
)
im
.
save
(
f
ilename
)
im
.
save
(
f
p
,
format
=
format
)
Write
Preview
Markdown
is supported
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