PIL Basic Features
from PIL import Image from PIL import ImageEnhance img = Image.open(r'E:\img\f1.png') img.show() #Binary image img = img.convert('L') # Image to enlarge img = img.resize((img.width * int(3), img.height * int(4)), Image.ANTIALIAS) # # Contrast Enhancement enh_con = ImageEnhance.Contrast(img) contrast = 2 img_contrasted = enh_con.enhance(contrast) # Brightness enhancement enh_bri = ImageEnhance.Brightness(img_contrasted) brightness = 2.5 image_brightened = enh_bri.enhance(brightness) # Chroma enhancement enh_col = ImageEnhance.Color(img) color = 50 image_colored = enh_col.enhance(color) # # Sharpness enhancement enh_sha = ImageEnhance.Sharpness(img) sharpness = 2 image_sharped = enh_sha.enhance(sharpness) image_sharped.save(r'E:\img\f22.png', dpi=(300, 300), quality=95) # image_sharped.save(r'E:\img\f22.png') # Image Character Recognition img2 = Image.open(r'E:\img\f22.png') code2 = pytesseract.image_to_string(img2, lang='chi_sim') # print(code2)
# 图片裁剪
image_cro = Image.open(r'E:\img\f24.png')
image_cropped = image_cro.crop(res)
image_cropped.save(u'E:\img\\f25.png')
The picture in black and white treatment
img_main = Image.open(u'E:/login1.png') img_main = img_main.convert('L') threshold1 = 138 table1 = [] for i in range(256): if i < threshold1: table1.append(0) else: table1.append(1) img_main = img_main.point(table1, "1") img_main.save(u'E:/login3.png')
Coordinate calculation large panel of FIG.
def get_screenxy_from_bmp(main_bmp, son_bmp): # Get theme matching the specified coordinates on the screen -> (x, y, width, height) img_main = Image.open(main_bmp) img_main = img_main.convert('L') threshold1 = 138 table1 = [] for i in range(256): if i < threshold1: table1.append(0) else: table1.append(1) img_main = img_main.point(table1, "1") img_son = Image.open(son_bmp) img_son = img_son.convert('L') threshold2 = 138 table2 = [] for i in range(256): if i < threshold2: table2.append(0) else: table2.append(1) img_son = img_son.point(table2, "1") datas_a = list(img_main.getdata()) datas_b = list(img_son.getdata()) for i, item in enumerate(datas_a): if datas_b[0] == item and datas_a[i + 1] == datas_b[1]: yx = divmod(i, img_main.size[0]) main_start_pos = yx[1] + yx[0] * img_main.size[0] match_test = True for n in range(img_son.size[1]): main_pos = main_start_pos + n * img_main.size[0] son_pos = n * img_son.size[0] if datas_b[son_pos:son_pos + img_son.size[0]] != datas_a[main_pos:main_pos + img_son.size[0]]: match_test = False break if match_test: return (yx[1], yx[0], img_son.size[0], img_son.size[1]) return False
ImageGrab achieve Screenshots
(Im = ImageGrab.grab) im.save('D:/as1.png') # # # # Parameter Description # # # # The first parameter of the x coordinate of the start screenshot # # # # The second parameter y coordinate of the start screenshot # # # # End of the third parameter x coordinate screenshots # # # # End of the fourth parameter y coordinate screenshots Bbox = (897, 131, 930, 148) im = ImageGrab.grab (Bbox) im.save('D:/as2.png')