How to convert a PIL Image to a fastai Image object?

I am using a file_uploader from streamlit to get images into PIL JpegImageFile format :

import streamlit as st

inferencer = load_learner(path)

img_bytes = st.file_uploader("Squash It!!", type=['png', 'jpg', 'jpeg'])
if img_bytes is not None:
    st.write("Image Uploaded Successfully:")
    img =

    pred_class, pred_idx, outputs = inferencer.predict(img)
    for out in outputs:

    st.write("Decision: ", pred_class)

However, I can’t find a method in the docs that will convert a PIL Image to a fastai Image. It seems without having that fastai wrapper, I can’t do inference. I get the error:

'JpegImageFile' object has no attribute 'apply_tfms'
File "C:\Users\WNeill\PycharmProjects\fastai-homework\Lesson2-v3\", line 20, in <module>
    pred_class, pred_idx, outputs = inferencer.predict(img)


1 Like

Hi @arora_aman, I do not see that method in the docs for fastai… Can you point me to the documentation?

Edit: I think what you are referring to is part of the new library. I am going through the v3 course and have no desire to try implementing fastai2. I tried it once, and will not again.


img_fastai = Image(img_tensor)


from import *
import torchvision.transforms as T

img_pil =
img_tensor = T.ToTensor()(img_pil)
img_fastai = Image(img_tensor)


Thank you, that worked like a charm! I’m not familiar with the underlying PyTorch yet (I just started the course). This was my first little taste :slight_smile:

fastai2 has changed the mid level and low level APIs dramatically, if you’re not comfortable yet, go through the v3 course once and then refer these fastai2 course notebooks to understand what has been changed.

I urge you to learn more about fastai2 because it’s damm good :sweat_smile:

Glad to hear it helped. Cheers : )

how i can convert Fastai Image object back to Tensor ? gives you the underlying tensor from a fastai image.

i convert back and forth from PIL to FastAI like this:

import torchvision.transforms as tfms

def pil2fast(img):  
  return Image(tfms.ToTensor()(img))

def fast2pil(img):
  return tfms.ToPILImage()("RGB")

Hi, I am also working on related issues, I want to convert a tensor to a fastAI image, I want to call the Image() but it said the ‘module’ object is not callable. Is the Image has been removed from the fastai? How do I convert the image tensor to a fastai image object. Thank you.


Same here. Can convert image tensor to fastAI Image Object with Image(img).

‘module’ object is not callable as error

on Google Colab with following version:

for FastAI V2 try this:‘RGB’)))

Thank you @arora_aman, I had exactly this Streamlit issue and your solution worked perfectly.

    file_uploaded = st.file_uploader("Choose File", type=["png","jpg","jpeg"])
class_btn = st.button("Classify")
if file_uploaded is not None:
    **image = PILImage.create(file_uploaded)**
    st.image(image, caption='Uploaded Image', use_column_width=True)

Emphasis on the operative code.

hello sir, im working on a segmentation problem,
i need to convert the “[0], figsize=(10,10))” output to a pillow image, how do i do that?