Python 3.x Python图像/枕头:如何使图像的背景更白

Python 3.x Python图像/枕头:如何使图像的背景更白,python-3.x,python-imaging-library,Python 3.x,Python Imaging Library,我有这样的图像: 我想做的是使图像的背景更白,使字母更可见。在我看来,这是一个很好的形象: 我在Python中使用枕头。提前谢谢你 最简单的方法可能是使用ImageOps.autocontrast()这样增加对比度: from PIL import Image, ImageOps # Open image as greyscale im = Image.open('letter.png').convert('L') # Autocontrast result = ImageOps.aut

我有这样的图像:

我想做的是使图像的背景更白,使字母更可见。在我看来,这是一个很好的形象:


我在Python中使用枕头。提前谢谢你

最简单的方法可能是使用
ImageOps.autocontrast()
这样增加对比度:

from PIL import Image, ImageOps

# Open image as greyscale
im = Image.open('letter.png').convert('L')

# Autocontrast
result = ImageOps.autocontrast(im) 

# Save
result.save('result.png')
from skimage import filters
from skimage.io import imread, imsave

# Load image as greyscale
img = imread('letter.png', as_gray=True)

# Get Otsu threshold - result is 151
threshold = filters.threshold_otsu(img) 


一种更复杂的方法是使用大津阈值将像素最佳分割为2种颜色,但为此,您需要像这样
scikit image

from PIL import Image, ImageOps

# Open image as greyscale
im = Image.open('letter.png').convert('L')

# Autocontrast
result = ImageOps.autocontrast(im) 

# Save
result.save('result.png')
from skimage import filters
from skimage.io import imread, imsave

# Load image as greyscale
img = imread('letter.png', as_gray=True)

# Get Otsu threshold - result is 151
threshold = filters.threshold_otsu(img) 

现在,您可以继续并使阈值以上的所有像素变为白色,并保持以下像素不变:

img[img>threshold] = 255
imsave('result.png',img)

或者,您可以执行一个完整的阈值,其中所有像素以纯黑色或纯白色结束:

result = (img>threshold).astype(np.uint8) * 255 
imsave('result.png',result)