datacarpentry / image-processing

Image Processing with Python
https://datacarpentry.org/image-processing
Other
104 stars 125 forks source link

Ep3: Image not writeable #247

Closed gcapes closed 2 years ago

gcapes commented 2 years ago

In this section https://datacarpentry.org/image-processing/03-skimage-images/index.html#manipulating-pixels we're keeping only high-intensity pixels. However, I get the following error:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[34], line 2
      1 # keep only high-intensity pixels
----> 2 image[image < 128] = 0
      4 # display modified image
      5 fig, ax = plt.subplots()

ValueError: assignment destination is read-only

Making a copy of the image first fixes the problem. image_copy = image.copy()

tobyhodges commented 2 years ago

Thanks for reporting this and the other issues, @gcapes - you are finding wrinkles that it is very helpful to iron out!

That said, I'm afraid I have not been able to reproduce this one.

editing image object

Could you share the whole code block and output please, so we can try to figure out what is going on here?

gcapes commented 2 years ago

MWE:

import numpy as np
import matplotlib.pyplot as plt
import ipympl
import imageio.v3 as iio
import skimage
%matplotlib widget

"""
* Python script to ignore low intensity pixels in an image.
*
"""
import imageio.v3 as iio

# read input image
image = iio.imread(uri="data/maize-root-cluster.jpg")

# display original image
fig, ax = plt.subplots()
plt.imshow(image)

image

# keep only high-intensity pixels
image[image < 128] = 0

# display modified image
fig, ax = plt.subplots()
plt.imshow(image)

image

tobyhodges commented 2 years ago

Hmmm. Can confirm this works with no errors on my end. I will go looking for an explanation of why this might be happening to you elsewhere on the Internet...

tobyhodges commented 2 years ago

Ok, I believe this issue thread provides an explanation for the behaviour you're observing: https://github.com/imageio/imageio/issues/877

I need to spend some time reading through it, and then I think it will need to be raised with the rest of the Maintainer team, and potentially the Curriculum Advisors, to figure out how we will handle this consistently throughout the lesson.

uschille commented 2 years ago

I can reproduce this. Will dig into it while addressing #252.

uschille commented 2 years ago

Based on the discussions in https://github.com/imageio/imageio/issues/877 and https://github.com/python-pillow/Pillow/issues/6581, image = np.array(image) may be a quick fix. A downside of this is the added cognitive load of thinking about writeable/non-writeable images. Maybe this is unavoidable in view of how the imageio backend works.

@mkcor Do you have any better suggestions?