How to open images, apply transforms.
from fastai2.vision.all import *

Open

From file name:

fn = Path('assets/cthulhu.jpeg')
im = PILImage.create(fn); im

Open as black and white:

im_bw = PILImageBW.create(fn); im_bw

From tensor/numpy-array:

tim = ToTensor()(im)
_ = PILImage.create(ToTensor()(im))

Resize

im.resize((128,128))

Save

im.save(fn)

Convert to Tensor

tim = ToTensor()(im); tim.shape
torch.Size([3, 144, 256])

Pipeline

A pipeline can be used to combine a sequence of functions.

def get_size(x): return x.size
pipe = Pipeline([PILImage.create, Resize(128), get_size])
pipe(fn)
(256, 144)

IMPORTANT: The expected output of the cell above would be (128,128) since we are calling Resize(128), but we are instead getting (256,144).
This happens because Pipeline reorders our functions based on an attribute called order, which defaults to 0 for all functions we pass.

[(f.name, f.order) for f in pipe]
[('PILBase.create', 0), ('get_size', 0), ('Resize', 1)]

For our function to execute after Resize, it's order has to be greater than 1 (the execution order is ascedent).

get_size_tfm = mk_transform(get_size)
get_size_tfm.order = 2
pipe = Pipeline([PILImage.create, Resize(128), get_size_tfm])
pipe(fn)
(128, 128)