0

Is it possible to enlarge the upper margin of the image, keeping the background the same color? I have tried cv2 and pillow, but could not. It is to improve the scraping of image web pages with scrapy and tesseract in python

Input enter image description here

Output enter image description here

enter image description here

from PIL import Image
import pytesseract
import imutils
import cv2
import numpy as np

pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract'
url = "https://www.yapo.cl/pg/0lu3quB3xJWOOJl5r7RBle3TME2kepf9ahuvYSfPYiA==.gif"

img = Image.open(urllib.request.urlopen(url))
img.save("image.png")
image = cv2.imread("image.png",0)
image = imutils.resize(image, width=400)
cv2.imshow('thresh', image)
cv2.waitKey()
kernel = np.ones((2,2), np.uint8)
cv2.imshow('thresh', image)
cv2.waitKey()
image = cv2.erode(image, kernel, iterations=1)
cv2.imshow('thresh', image)
cv2.waitKey()


image = cv2.dilate(image, kernel, iterations=1)
cv2.imshow('thresh', image)
cv2.waitKey()

image = cv2.GaussianBlur(image, (7,7), 0)
#result = 255 - thresh
image = cv2.threshold(image, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]
phone = pytesseract.image_to_string(image, config="-c tessedit_char_whitelist=0123456789+() --psm 6").strip()

print(phone)
Community
  • 1
  • 1
Dandal
  • 166
  • 1
  • 11
  • Stack Overflow is not a code-writing or tutorial service. Please [edit] your question and post [what you have tried so far](https://meta.stackoverflow.com/q/261592), including example input, expected output, the actual output (if any), and the **full text** of any errors or tracebacks, *all as formatted text in the question itself.* Do not post images of text. *"I have tried cv2 and pillow, but could not"* is not a valid problem statement. – MattDMo Jan 13 '21 at 01:01
  • 1
    Does this answer your question? [In Python, Python Image Library 1.1.6, how can I expand the canvas without resizing?](https://stackoverflow.com/questions/1572691/in-python-python-image-library-1-1-6-how-can-i-expand-the-canvas-without-resiz) – MattDMo Jan 13 '21 at 01:11
  • thx u @MattDMo but the image becomes blurry when doing the process – Dandal Jan 15 '21 at 03:46

0 Answers0