Image cropping:

webdriver the Selenium Import from 
Import Time
Import Random
# import PIL module and Image:
from PIL Import Image
# import expected_conditions expected package to determine whether the correct title:
from selenium.webdriver.support Import expected_conditions AS EC
# import WebDriverWait
from selenium.webdriver.support.wait WebDriverWait import
# introduced by:
from selenium.webdriver.common.by by import
Driver = webdriver.Chrome ()
driver.maximize_window ()
driver.get ( "http://www.5itest.cn/register")
# wheel is operated slide in the end part:
# driver.execute_script ( "window.scrollTo (0, document.body.scrollHeight);")
# save the image of the entire page with save_screenshot:
driver.save_screenshot ( "C: / I code / selenium test automation / Selenium3 and Python3 actual Web automated testing framework /imooc.png ")
Getting the location codes # picture element ID:
code_element = driver.find_element_by_id ( "getcode_num")
# get the image coordinate values LOCATION:
Print (code_element.location) # result is: { "x": 123, "y" : 345}
# x to get the value of the left:
left code_element.location = [ "x"]
# get the right values of y:
Top code_element.location = [ "y"]
# get the picture size by the width and height:
= code_element.size right [ "width"] + left
height = code_element.size [ "height"] + Top
# open Image:
# IM = Image.open ( "C: / I code / selenium test automation / Selenium3 and Python3 actual Web automated testing framework /imooc.png ")
# according to certain crop a picture with coordinates crop:
# im.crop img = ((left, Top, right, height))
# save the image as imooc1.png:
# img.save ( "C: / I code / selenium test automation / Selenium3 and Python3 actual Web automated testing framework /imooc1.png")
from PIL import Image
image = Image.open ( "C: / I code / selenium test automation / Selenium3 and Python3 actual Web automated testing framework /imooc.png")
cropped_image = image.crop ((left, Top, right, height))
cropped_image. save ( "C: / I code / selenium test automation / Selenium3 and Python3 actual Web automated testing framework /imooc1.png")
from PIL Import Image
Image = Image.open ( "C: / I code / selenium test automation / Selenium3 and Python3 actual Web automated testing framework /imooc1.png").convert("L ")
cropped_image = image.crop ((0,0,45,50))
cropped_image.save (" cropped_image.png ")

# image go heteroaryl, clean interference factors:
pixel_matrix cropped_image.load = ()
for COL in Range (0, cropped_image.height):
for Row in Range (0, cropped_image.width):
! IF pixel_matrix [Row, COL] = 0:
pixel_matrix [ row, col] = 255
image.save("thresholded_image.png")

#去除图像中的黑点:
for column in range(1,image.height - 1):
for row in range(1,image.width - 1):
if pixel_matrix[row,column] == 0 and pixel_matrix[row,column - 1] == 10 and pixel_matrix[row,column + 1] == 10:
pixel_matrix[row,column] = 255
if pixel_matrix[row,column] == 0 and pixel_matrix[row - 1,column] == 255 and pixel_matrix[row + 1,column] == 255:
pixel_matrix[row,column] = 255

Guess you like

Origin www.cnblogs.com/zhang-da/p/12143295.html