Reputation: 11619
I have images like these:
What I would like to do is to make the background of the image more white so that the letters are more visible. Here is a good image from my perspective:
I am using Pillow in Python.
Upvotes: 1
Views: 1894
Reputation: 207345
The simplest is probably to use ImageOps.autocontrast()
to increase the contrast like this:
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')
A more sophisticated approach would be to use an Otsu thresholding to split the pixels optimally into 2 colours, but for that you would want scikit-image
like this:
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)
You can now either continue and make all pixels above the threshold white and leave those below as they are:
img[img>threshold] = 255
imsave('result.png',img)
Or, you can do a complete threshold where all pixels end up either solid black or solid white:
result = (img>threshold).astype(np.uint8) * 255
imsave('result.png',result)
Upvotes: 2