How to open images, apply transforms.
from fastai2.vision.all import *
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))
im.resize((128,128))
im.save(fn)
tim = ToTensor()(im); tim.shape
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)
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]
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)