将图像投射到不同类别的PIL [英] Casting images to different class PIL
问题描述
我正在使用PIL,并尝试在图像上使用中值滤波器以对其进行降噪.但是当我尝试时,结果是黑色图像.我认为导致该问题的原因是该图像属于"PIL.Image.Image"类.
I'm working with PIL and trying to use the median filter on an image in order to de-noise it. But when I try, the result is a black image. What I think is the cause of the problem is the fact that the image is of class 'PIL.Image.Image'.
让我更深入地解释我在做什么/这个问题.我必须使用numpy进行一些图像操作.为此,我首先必须将PIL图像转换为numpyArray.我使用以下代码进行了此操作:
Let me explain what I am doing/the issue in more depth. I had to perform some image manipulations using numpy. In order to do that I first had to cast the PIL image to a numpyArray. I did this with the following code:
img = np.array(image)
执行所需的转换后,我使用以下代码将图像投射回PIL:
After performing the needed transformations I cast the image back to PIL with the following code:
def numpy_to_pil(image):
minv = np.amin(image)
maxv = np.amax(image)
img = Image.fromarray((255 * (image - minv) / (maxv - minv)).astype(np.uint8))
return img
如前所述,当我尝试使用中值过滤器过滤"img"时,结果是黑色图像.这就是我使用PIL的过滤器功能的方式:
When I try to filter 'img' with a median filter, as previously stated, the result is a black image. This is how I use the filter function of PIL:
img.filter(ImageFilter.MedianFilter(3))
我尝试将结果与未通过"img"进行转换的图像(我们称其为猫"(从字面上看是猫的图像))进行比较.我尝试打印出类型,以查看有何不同.结果是:
I tried comparing the results with an image (let's call it 'cat' (literally image of a cat)) that wasn't put through the casting process that 'img' was put through. I tried printing out the types to see what differed. This is the result:
cat = <class 'PIL.PngImagePlugin.PngImageFile'>
img = <class 'PIL.Image.Image'>
看到这个,我想知道问题是否在于"img"的类型为"PIL.Image.Image"而不是"PIL.PngImagePlugin.PngImageFile".将numpyArray强制转换为PIL时,我做错什么了吗?还是其他?
Seeing this I wonder if the problem is that 'img' is of type 'PIL.Image.Image' instead of 'PIL.PngImagePlugin.PngImageFile'. Did I do something wrong whilte casting form numpyArray to PIL? Or is it something else.
任何帮助将不胜感激! (我尝试尽可能具体)
Any help is greatly appreciated! (I tried being as specific as possible)
P.S:噪声的类型是盐和胡椒
P.P.S:我尝试使用img.convert('PIL.PngImagePlugin.PngImageFile')
,但它输出以下错误:
P.S: The type of noise is salt&pepper
P.P.S: I tried using img.convert('PIL.PngImagePlugin.PngImageFile')
but it outputs the following error:
不支持从RGB到PIL.PngImagePlugin.PngImageFile的转换
conversion from RGB to PIL.PngImagePlugin.PngImageFile not supported
推荐答案
此步骤有很多工作:(255 * (image - minv) / (maxv - minv)).astype(np.uint8)
.
There is a lot going on in this step: (255 * (image - minv) / (maxv - minv)).astype(np.uint8)
.
图像的数据类型为uint8,因此,如果先乘以255,则超出了8位整数的最大值.您可以将数据类型更改为int64
或中断步骤,然后将为您强制转换数据类型.
The image is of datatype uint8 so when you multiply by 255 first, you are exceeding the maximum value of an 8-bit integer. You can either change the datatype to int64
or break the steps out and the datatype will be cast for you earlier.
如果您突破这些限制并最后乘以255,则由于8位整数溢出,它不再显示黑屏:
If you break those steps out and multiply by 255 last, it no longer gives a black screen because of overflowing the 8-bit integer:
minv = np.amin(image)
maxv = np.amax(image)
image = image - minv
image = image / (maxv - minv)
image = image * 255
img = Image.fromarray(image.astype(np.uint8))
这篇关于将图像投射到不同类别的PIL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!