python PIL模塊的基本使用
更新時間:2020年09月29日 11:39:43 作者:傻白甜++
這篇文章主要介紹了python PIL模塊的基本使用,幫助大家更好的利用python處理圖片,感興趣的朋友可以了解下
PIL基本功能介紹
from PIL import Image
from PIL import ImageEnhance
img = Image.open(r'E:\img\f1.png')
img.show()
#圖像二值化
img = img.convert('L')
# 圖像放大
img = img.resize((img.width * int(3), img.height * int(4)), Image.ANTIALIAS)
# # 對比度增強
enh_con = ImageEnhance.Contrast(img)
contrast = 2
img_contrasted = enh_con.enhance(contrast)
# 亮度增強
enh_bri = ImageEnhance.Brightness(img_contrasted)
brightness = 2.5
image_brightened = enh_bri.enhance(brightness)
#色度增強
enh_col = ImageEnhance.Color(img)
color = 50
image_colored = enh_col.enhance(color)
# # 銳度增強
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')
# 圖片漢字識別
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')
對圖片進行黑白化處理
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')
計算小圖在大圖的坐標
def get_screenxy_from_bmp(main_bmp, son_bmp):
# 獲取屏幕上匹配指定截圖的坐標->(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實現(xiàn)屏幕截圖
im = ImageGrab.grab()
im.save('D:/as1.png')
# # # # 參數(shù)說明
# # # # 第一個參數(shù) 開始截圖的x坐標
# # # # 第二個參數(shù) 開始截圖的y坐標
# # # # 第三個參數(shù) 結(jié)束截圖的x坐標
# # # # 第四個參數(shù) 結(jié)束截圖的y坐標
bbox = (897, 131, 930, 148)
im = ImageGrab.grab(bbox)
im.save('D:/as2.png')
以上就是python PIL模塊的基本使用的詳細內(nèi)容,更多關(guān)于python PIL模塊的資料請關(guān)注腳本之家其它相關(guān)文章!
您可能感興趣的文章:
- python 實現(xiàn)PIL模塊在圖片畫線寫字
- Python圖片處理模塊PIL操作方法(pillow)
- python3使用Pillow、tesseract-ocr與pytesseract模塊的圖片識別的方法
- Python圖像處理庫PIL的ImageFilter模塊使用介紹
- Python圖像處理庫PIL的ImageEnhance模塊使用介紹
- Python圖像處理庫PIL的ImageFont模塊使用介紹
- Python圖像處理庫PIL的ImageGrab模塊介紹詳解
- Python圖像處理庫PIL的ImageDraw模塊介紹詳解
- python pillow模塊使用方法詳解
- Python圖像處理PIL各模塊詳細介紹(推薦)
- Python 3.6 -win64環(huán)境安裝PIL模塊的教程
- 詳解python3安裝pillow后報錯沒有pillow模塊以及沒有PIL模塊問題解決
- Python離線安裝PIL 模塊的方法
相關(guān)文章
python向json中追加數(shù)據(jù)的兩種方法總結(jié)
JSON用來存儲和交換文本信息,比xml更小/更快/更易解析,下面這篇文章主要給大家介紹了關(guān)于python向json中追加數(shù)據(jù)的兩種方法,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下2022-05-05
python統(tǒng)計列表中元素出現(xiàn)次數(shù)的三種方法
這篇文章主要介紹了python統(tǒng)計列表中元素出現(xiàn)次數(shù)的三種方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面跟著小編來一起學習學習吧2024-08-08
使用python+Flask實現(xiàn)日志在web網(wǎng)頁實時更新顯示
日志是一種可以追蹤某些軟件運行時所發(fā)生事件的方法,下面這篇文章主要給大家介紹了關(guān)于使用python+Flask實現(xiàn)日志在web網(wǎng)頁實時更新顯示的相關(guān)資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考下2022-08-08
python通過Matplotlib繪制常見的幾種圖形(推薦)
這篇文章主要介紹了使用matplotlib對幾種常見的圖形進行繪制方法的相關(guān)資料,需要的朋友可以參考下2021-08-08

