SoFunction
Updated on 2025-03-01

Basic use of python PIL module

Introduction to basic functions of PIL

from PIL import Image
from PIL import ImageEnhance
 
img = (r'E:\img\')
()
#Image Binaryimg = ('L')
# Image enlargementimg = (( * int(3),  * int(4)), )
# # Contrast enhancementenh_con = (img)
contrast = 2
img_contrasted = enh_con.enhance(contrast)
# Brightness enhancementenh_bri = (img_contrasted)
brightness = 2.5
image_brightened = enh_bri.enhance(brightness)
#Color enhancementenh_col = (img)
color = 50
image_colored = enh_col.enhance(color)
# # Sharpness enhancementenh_sha = (img)
sharpness = 2
image_sharped = enh_sha.enhance(sharpness)
image_sharped.save(r'E:\img\', dpi=(300, 300), quality=95)
# image_sharped.save(r'E:\img\')
 
# Picture Chinese character recognitionimg2 = (r'E:\img\')
code2 = pytesseract.image_to_string(img2, lang='chi_sim')
# print(code2)
#Picture croppingimage_cro = (r'E:\img\')
image_cropped = image_cro.crop(res)
image_cropped.save(u'E:\img\\') 

Black-and-whitening of pictures

img_main = (u'E:/')
img_main = img_main.convert('L')
threshold1 = 138
table1 = []
for i in range(256):
  if i < threshold1:
    (0)
  else:
    (1)
img_main = img_main.point(table1, "1")
img_main.save(u'E:/')

Calculate the coordinates of the small graph in the large graph

def get_screenxy_from_bmp(main_bmp, son_bmp):
  # Get the coordinates of the specified screenshot matching on the screen ->(x,y,width,height) 
  img_main = (main_bmp)
  img_main = img_main.convert('L')
  threshold1 = 138
  table1 = []
  for i in range(256):
    if i &lt; threshold1:
      (0)
    else:
      (1)
  img_main = img_main.point(table1, "1")
 
  img_son = (son_bmp)
  img_son = img_son.convert('L')
  threshold2 = 138
  table2 = []
  for i in range(256):
    if i &lt; threshold2:
      (0)
    else:
      (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 implementation screenshot

im = ()
('D:/')
 
# # # # Parameter description# # # # First parameter The x-coordinate of the start screenshot# # # # Second parameter The y-coordinate of the start screenshot# # # # The third parameter The x-coordinate of the end screenshot# # # # Fourth parameter The y-coordinate of the end screenshotbbox = (897, 131, 930, 148)
im = (bbox)
('D:/')

The above is the detailed content of the basic use of python PIL module. For more information about python PIL module, please pay attention to my other related articles!