Skip to content

Commit caee390

Browse files
committed
format
1 parent 95ac593 commit caee390

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

gallery/transforms/plot_transform_tensor_image.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
Try on `collab <https://colab.research.google.com/github/pytorch/vision/blob/gh-pages/main/_generated_ipynb_notebooks/plot_custom_tv_tensors.ipynb>`_
88
or :ref:`go to the end <sphx_glr_download_auto_examples_transforms_plot_custom_tv_tensors.py>` to download the full example code.
99
10-
In thie example, we explain the basic usgae of :func:`~torchvision.transforms.functional.to_tensor`, :func:`~torchvision.transforms.functional.pil_to_tensor` and :func:`~torchvision.transforms.functional.to_pil_image`.
10+
In thie example, we explain the basic usgae of :func:`~torchvision.transforms.functional.to_tensor`, :func:`~torchvision.transforms.functional.pil_to_tensor` and :func:`~torchvision.transforms.functional.to_pil_image`.
1111
And the difference between :func:`~torchvision.transforms.functional.to_tensor` and :func:`~torchvision.transforms.functional.pil_to_tensor`.
1212
"""
1313

@@ -17,13 +17,13 @@
1717
# In this cell, we illustrate the different representation of a PIL image and tensor
1818
import PIL.Image as Image
1919
from torchvision.transforms.functional import to_tensor
20-
from helpers import plot # use your favorite visualization library
20+
from helpers import plot # use your favorite visualization library
2121

22-
img_pil = Image.open('../assets/person1.jpg')
22+
img_pil = Image.open("../assets/person1.jpg")
2323
width, height = img_pil.size
2424
# There is no straight forward way to get channel information
2525
# Please read https://pillow.readthedocs.io/en/stable/handbook/concepts.html for more detail
26-
num_channels = 3 # hardcoded since it's a color image.
26+
num_channels = 3 # hardcoded since it's a color image.
2727
print("PIL image: width x height x num_channels:", width, height, num_channels)
2828

2929
img_tensor = to_tensor(img_pil)
@@ -37,7 +37,7 @@
3737
# In this cell, we explain the difference between :func:`~torchvision.transforms.functional.pil_to_tensor` vs. :func:`~torchvision.transforms.functional.to_tensor`
3838
from torchvision.transforms.functional import pil_to_tensor
3939

40-
img_pil = Image.open('../assets/person1.jpg')
40+
img_pil = Image.open("../assets/person1.jpg")
4141
img_to_tensor = to_tensor(img_pil)
4242
num_channels, height, width = img_to_tensor.shape
4343
print("Tensor image(to_tensor): num_channels x height x width:", num_channels, height, width)
@@ -52,15 +52,16 @@
5252
# The shape is the same but **data type** is different! The **tensor value** is also different!
5353

5454
# %%
55-
print(img_to_tensor) # tensor that is returned by calling to_tensor()
56-
print(img_pil_to_tensor) # tensor that is returned by calling pil_to_tensor()
55+
print(img_to_tensor) # tensor that is returned by calling to_tensor()
56+
print(img_pil_to_tensor) # tensor that is returned by calling pil_to_tensor()
5757

5858
# %%
5959
# Notice :func:`~torchvision.transforms.functional.to_tensor` automatically scale the image, but :func:`~torchvision.transforms.functional.pil_to_tensor` does not. To rescale the image back,
6060

6161
import torch
62+
6263
img_pil_to_tensor_2 = (img_to_tensor * 255).to(torch.uint8)
63-
print((img_pil_to_tensor_2 == img_pil_to_tensor).all().item()) # check if two tensors are same
64+
print((img_pil_to_tensor_2 == img_pil_to_tensor).all().item()) # check if two tensors are same
6465

6566
# %%
6667
# **TLDR** it's recommended to use :func:`~torchvision.transforms.functional.pil_to_tensor` for visualization tasks since most visualization library
@@ -74,7 +75,7 @@
7475
# In this cell, we explain an example usage of :func:`~torchvision.transforms.functional.to_pil_image`
7576
from torchvision.transforms.functional import to_pil_image
7677

77-
img_pil = Image.open('../assets/person1.jpg')
78+
img_pil = Image.open("../assets/person1.jpg")
7879

7980
# convert to tensor using to_tensor() and pil_to_tensor()
8081
img_to_tensor = to_tensor(img_pil)
@@ -88,4 +89,3 @@
8889

8990
# %%
9091
# Both tensor can be converted back to PIL image.
91-

0 commit comments

Comments
 (0)