11| # importing modules
22| import cv2
33| import pytesseract
45| # reading image using opencv
56| image = cv2.imread(sample_image.png’)
67| #converting image into gray scale image
78| gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
89| # converting it to binary image by Thresholding
910| # this step is require if you have colored image because if you skip this part
1011| # then tesseract won't able to detect text correctly and this will give incorrect result
1111|threshold_img = cv2.threshold(gray_image, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
1212| # display image
1313| cv2.imshow(‘threshold image’, threshold_img)
1414| # Maintain output window until user presses a key
1515| cv2.waitKey(0)
1616| # Destroying present windows on screen
1717| cv2.destroyAllWindows()
18
1The OpenCV documentation itself is good to start with.
2
3https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_imgproc/py_table_of_contents_imgproc/py_table_of_contents_imgproc.html
1# The file format of the source file.
2print(image.format) # Output: JPEG
3
4# The pixel format used by the image. Typical values are "1", "L", "RGB", or "CMYK."
5print(image.mode) # Output: RGB
6
7# Image size, in pixels. The size is given as a 2-tuple (width, height).
8print(image.size) # Output: (1920, 1280)
9
10# Colour palette table, if any.
11print(image.palette) # Output: None
1image = Image.open('demo_image.jpg')
2new_image = image.resize((400, 400))
3new_image.save('image_400.jpg')
4
5print(image.size) # Output: (1920, 1280)
6print(new_image.size) # Output: (400, 400)