一 利用PIL实现图像的缩放
1, python 利用PIL库实现图像的缩放、模糊、平滑等操作
2, 若想获取高质量的照片,则需要注意以下两点:
- 在处理的时候,需要使用Image.ANTIALIAS
- 在最后保存时,要设置quality
3,实例1:
- 该实例,实现批量修改图片的尺寸,可以自定义输入和输出文件以及缩放比例。
#encoding=utf-8
from PIL import Image
from PIL import ImageGrab
if __name__ == '__main__':
path = '~/Desktop/project/crawler/crawler_airline/image1/'
img = Image.open(path+'1.jpg')
print(img.size) # 获取图形的大小
print(img.size[0]) # 获取图像的宽度
print(img.size[1]) # 获取图像的高度
width = int(img.size[0]) * 10 #可以根据自己需求扩大倍数
height = int(img.size[1]) * 10
img = img.resize((width, height), Image.ANTIALIAS)
img.save(path+'1_new.jpg') #将照片保存到本地上
- 运行结果:
二 利用pytesseract实现图像的缩放
1,在python爬虫时候,可能会遇到下载链接上的内容,识别图片中文字或者数字。
2, 在image_to_string()方法中有一个config参数,即config =’ –psm ’ , 其中psm 选项参数如下:
0 Orientation and script detection (OSD) only.
1 Automatic page segmentation with OSD.
2 Automatic page segmentation, but no OSD, or OCR.
3 Fully automatic page segmentation, but no OSD. (Default)
4 Assume a single column of text of variable sizes.
5 Assume a single uniform block of vertically aligned text.
6 Assume a single uniform block of text.
7 Treat the image as a single text line.
8 Treat the image as a single word.
9 Treat the image as a single word in a circle.
10 Treat the image as a single character.
11 Sparse text. Find as much text as possible in no particular order.
12 Sparse text with OSD.
13 Raw line. Treat the image as a single text line,
-
实例: target = pytesseract.image_to_string(image, lang=‘eng’, boxes=False,
config=’–psm 10 –oem 3 -c tessedit_char_whitelist=0123456789’)
3,实例
-
该实例实现对小个或者模糊图片内容的识别,该图片如下图所示:
#encoding=utf-8
import pytesseract
# lang只用哪个库来识别 默认有个eng库,config 指代识别单行还是多行--psm 7只的是单行。
text = pytesseract.image_to_string(Image.open(path+'1.jpg'), lang='eng',config="--psm 7")
print(text)
-
运行结果
版权声明:本文为wodedipang_原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。