如何使照片列表的文字更清晰?

时间:2020-05-05 16:39:35

标签: python-3.x image image-processing python-imaging-library

我有大约100张不是很清晰的照片,我想使其更清晰。

introducir la descripción de la imagen aquí

所以我用python创建了一个脚本,该脚本已经尝试过使用。我曾尝试使用PIL,OpenCV和OCR读取器从图像中读取文本。

# External libraries used for
# Image IO
from PIL import Image

# Morphological filtering
from skimage.morphology import opening
from skimage.morphology import disk

# Data handling
import numpy as np

# Connected component filtering
import cv2

black = 0
white = 255
threshold = 160

# Open input image in grayscale mode and get its pixels.
img = Image.open("image3.png").convert("LA")
pixels = np.array(img)[:,:,0]

# Remove pixels above threshold
pixels[pixels > threshold] = white
pixels[pixels < threshold] = black


# Morphological opening
blobSize = 1 # Select the maximum radius of the blobs you would like to remove
structureElement = disk(blobSize)  # you can define different shapes, here we take a disk shape
# We need to invert the image such that black is background and white foreground to perform the opening
pixels = np.invert(opening(np.invert(pixels), structureElement))


# Create and save new image.
newImg = Image.fromarray(pixels).convert('RGB')
newImg.save("newImage1.PNG")

# Find the connected components (black objects in your image)
# Because the function searches for white connected components on a black background, we need to invert the image
nb_components, output, stats, centroids = cv2.connectedComponentsWithStats(np.invert(pixels), connectivity=8)

# For every connected component in your image, you can obtain the number of pixels from the stats variable in the last
# column. We remove the first entry from sizes, because this is the entry of the background connected component
sizes = stats[1:,-1]
nb_components -= 1

# Define the minimum size (number of pixels) a component should consist of
minimum_size = 100

# Create a new image
newPixels = np.ones(pixels.shape)*255

# Iterate over all components in the image, only keep the components larger than minimum size
for i in range(1, nb_components):
    if sizes[i] > minimum_size:
        newPixels[output == i+1] = 0

# Create and save new image.
newImg = Image.fromarray(newPixels).convert('RGB')
newImg.save("newImage2.PNG")

但是它返回:

introducir la descripción de la imagen aquí introducir la descripción de la imagen aquí

我希望它不是黑白的,最好的输出是可以同时放大文字和图像的

1 个答案:

答案 0 :(得分:0)

如评论中所述,质量非常差。这不是一个容易的问题。但是,您可以尝试一些技巧。

这似乎是由于已对图像/扫描应用了某些抗锯齿功能。如果可能,我会尝试reversing anti-aliasing。如文章中所述,步骤将与此类似:

  1. 应用低通滤波器
  2. 差异=原始图片-低通图片
  3. sharpened_image =原始图像+ alpha *差异

代码可能看起来像这样:

from skimage.filters import gaussian

alpha = 1   # Sharpening factor

low_pass_image = gaussian(image, sigma=1)
difference = original_image - low_pass_image
sharpened_image = original_image + alpha*difference

此外,scikit映像具有unsharp maskwiener filter的实现。