1

I want to use ipywidgets.interact to show Pillow images similar to what someone already did here. However, in my case Jupyter would not render the image. Instead it looks like this:

Screenshot of Jupyter

If I output the image separately into a cell then it displays. Does anyone know how I can fix this?

C. E.
  • 10,297
  • 10
  • 53
  • 77
  • Do you know when your resource was written? I see it mentions 'OS X El Capitan' which dates to 2015. It is important to keep in mind ipywidgets has changed a lot just in the last couple of years. However, this does seem to work, I'll post an answer. It may just be your environment isn't properly set up. You need ipywidgets. – Wayne Mar 10 '22 at 20:53

1 Answers1

0

I suspect you didn't have your environment set up quite right with the matching versions. Or maybe not imported everything needed.

If you go here and run that code it will work because the environment served via MyBinder.org specified here has ipywidgets and current versions of everything necessary. (It actually isn't listed there but voila has it as a dependency and so it's installed in the build; you'll see it listed if you run %pip list in a cell in the notebook.)

When that notebook shows up after a few seconds, execute the entire thing by selecting Run > Run All Cells from the toolbar menu. Importantly, You won't see the adjustable image when you first open the notebook, without running it actively there.

After running it, now the slider should work for adjusting.

The code that I used with ipywidgets.interact to show a Pillow image so that it's adjustable:

%matplotlib inline
from ipywidgets import interact, interactive, fixed, interact_manual
import ipywidgets as widgets

from PIL import Image
img = Image.open('picture.png').convert('L')

@interact
def binarize(th: (0, 255, 1)):
    return img.point(lambda p: 255 if p > th else 0)

I just tried it and that %matplotlib inline isn't needed at this time, consider it optional.

Wayne
  • 6,607
  • 8
  • 36
  • 93
  • I'm sorry but I think this answer misses the point. `ipywidgets` works fine as can be seen in my screenshot. If the output was a number for example I would have no complaints. My only complaint is that instead of displaying an image, it displays a textual representation of that image. I understand that what you post here should work, it doesn't for me though and the question is why that might be. Thank you anyway. – C. E. Mar 11 '22 at 15:33
  • What you see in your post is the notebook giving the underlying Python object information for an object. You often used to see this kind of code shown when using matplotlib to generate a plot you didn't put a semi-colon at the end to suppress 'output' return showing. Recently handling this type of thing is less 'noisy' in Jupyter and so you often don't need the `;` at the end now. ....to be continued – Wayne Mar 11 '22 at 17:18
  • Sometimes it still shows and usually you can put a semi-colon at the end of your code and suppress it. Sometimes it also shows up when your notebook cannot render the object. I think that is the case here, but hard to say since you don't show or describe what you mean by 'If I output the image separately into a cell then it displays.' Does the image in the other code adjust when you use the slider? Does interact work in your notebook for other examples that don't involve pillow? – Wayne Mar 11 '22 at 17:34
  • Is it simply because you have a return statement as the last line and last lines are special? Did you try adding another line that is just a semi-colon? In support of the 'noisy' output of the return code sometimes showing up, see `
    ` in https://stackoverflow.com/q/59936392/8508004 , `` in https://groups.google.com/forum/#!msg/jupyter/SMBUkOWPetA/nIVztypABQAJ . the huge array and list object info highlighted in https://stackoverflow.com/q/14506583/8508004 .
    – Wayne Mar 11 '22 at 17:34
  • Yes, the interactivity is working. As I said, if I output a number from the function it will work as expected. The text representation of the image is also updated when I move the slider. When I say that images are corectly displayed when I output them in normal cells (not ones created by interact) it means that I am seeing the image, not the text representation. – C. E. Mar 11 '22 at 19:04
  • If the text representation updates as you slide, then it simply sounds like a mismatch or failure of some sort somewhere in your environment so rendering isn't happening. It would be curious if a simpler `interact` example works? I have seen icases recently where `interactive` works better for matplotlib where pillow wasn't involved. – Wayne Mar 11 '22 at 19:14
  • Yes, exactly, there is something wrong in the environment which is causing it to fail. I’m looking for ideas for how to debug that. I have tried lots of different things but I have not had any success with any type of images. – C. E. Mar 11 '22 at 21:00