如何用imageio替代scipy.misc.imread读取RGB格式图像?
scipy.misc.imread with imageio.imread for RGB Images Great question! Let's break down how to replace that scipy.misc.imread call with imageio correctly—your initial attempt won't work as expected, but the fix is straightforward.
What's wrong with imref = imageio.imread(self.file_image, False)?
The second parameter in imageio.imread is not flatten (like in scipy). Instead, it's the format parameter (used to force a specific image format decoder). Passing False here will either throw an error or lead to unexpected image handling, so that's not the right approach.
Correct Replacement for Your Original Code
Your original scipy call:
imref = misc.imread(self.file_image, False, "RGB")
Tells scipy to:
- Read the image without flattening it (keep color channels)
- Force the image into RGB mode
In imageio, you achieve this with the pilmode parameter (which works with most common image formats, as imageio uses PIL under the hood for many cases). Here's the equivalent code:
For ImageIO v2 (current recommended version):
import imageio imref = imageio.v2.imread(self.file_image, pilmode="RGB")
For older ImageIO v1:
import imageio imref = imageio.imread(self.file_image, pilmode="RGB")
Key Notes:
- The
flatten=Falsefrom scipy is unnecessary here becauseimageiodefaults to preserving color channels (unless you specify a grayscalepilmodelike"L"). - The
pilmode="RGB"parameter explicitly tells imageio to read the image in RGB format, matching your originalmode="RGB"setting in scipy. This ensures the output array has the correct channel order and number of channels.
内容的提问来源于stack exchange,提问作者user288609

