zoukankan      html  css  js  c++  java
  • day30-20180610笔记

    笔记:Python3 图片处理

    一、图片处理

    图像处理是一门应用非常广的技术,而拥有非常丰富第三方扩展库的 Python 当然不会错过这一门盛宴。PIL (Python Imaging Library)是 Python 中最常用的图像处理库,如果你是python2.x,可以通过以下地址进行下载:http://www.pythonware.com/products/pil/index.htm,找到相对应的版本进行下载就可以了。
    注意:PIL模块在python3.x中已经替换成pillow模块,文档地址:http://pillow.readthedocs.io/en/latest/,直接使用pip3 install pillow即可安装模块,导入时使用from PIL import Image.

    爬虫:
    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    # @Time    : 2018/6/11 21:48
    # @Author  : yangyuanqiang
    # @File    : demon1.py
    
    
    #
    # urls = list()
    #
    # for i in range(1, 27):
    #     url = 'http://www.apelearn.com/study_v2/chapter{0}.html'.format(i)
    #     # print(url)
    #     urls.append(url)
    # # print(urls)
    import re
    import requests
    
    
    reg = re.compile(r"<h3>目录列表</h3>s+<ul>s+([sS]*?</ul>)")
    url = "http://www.apelearn.com/study_v2/"
    session = requests.session()
    r = session.get(url)
    # print(r.encoding)
    r.encoding = "utf-8"
    html = r.text
    # print(html)
    htmlli = reg.findall(html)
    # print(htmlli)
    regurl = re.compile(r'''href="(.*?)"''')
    if htmlli[0]:
        result = regurl.findall(htmlli[0])
        # print(result)
    
    urls = list()
    for i in result:
        url = "http://www.apelearn.com/study_v2/{0}".format(i)
        print(url)
        urls.append(urls)
    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    # @Time    : 2018/6/11 21:49
    # @Author  : yangyuanqiang
    # @File    : demon2.py
    
    
    import codecs
    
    import PyPDF2
    import os
    
    files = list()
    for fileName in os.listdir("aming"):
        if fileName.endswith(".pdf"):
            files.append(fileName)
    
    newFiles = sorted(files, key=lambda d: int(d.split(".pdf")[0]))
    print(newFiles)
    
    
    os.chdir("aming")
    pdfWriter = PyPDF2.PdfFileWriter()#生成一个空白的pdf
    for item in newFiles:
        pdfReader = PyPDF2.PdfFileReader(open(item, "rb"))
        for page in range(pdfReader.numPages):
            pdfWriter.addPage(pdfReader.getPage(page))
    
    with codecs.open("aminglinux.pdf", "wb") as f:
        pdfWriter.write(f)

    图片处理:

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    # @Time    : 2018/6/11 21:50
    # @Author  : yangyuanqiang
    # @File    : demon3.py
    
    
    '''python3 操作图片需要
    pip install pillow
    '''
    from PIL import Image
    image = Image.open("test.jpg")
    print(image.format, image.size, image.mode)
    image.show()
    
    #image的方法
    #image.show()
    #image.open(file)
    #image.save(outputfile)
    #image.crop(left, upper, right, lower)#抠图
    
    # Image的几何处理:
    # out = im.resize((128, 128))                     #调整图片大小
    # out = im.rotate(45)                             #逆时针旋转 45 度角。
    # out = im.transpose(Image.FLIP_LEFT_RIGHT)       #左右对换。
    # out = im.transpose(Image.FLIP_TOP_BOTTOM)       #上下对换。
    # out = im.transpose(Image.ROTATE_90)             #旋转 90 度角。
    # out = im.transpose(Image.ROTATE_180)            #旋转 180 度角。
    # out = im.transpose(Image.ROTATE_270)            #旋转 270 度角。
    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    # @Time    : 2018/6/11 21:50
    # @Author  : yangyuanqiang
    # @File    : demon4.py
    
    
    '''python3 操作图片需要
    pip install pillow
    '''
    from PIL import Image
    image = Image.open("test.jpg")
    
    cutjpg = image.crop((320, 65, 460, 220))
    cutjpg.show()
    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    # @Time    : 2018/6/11 21:51
    # @Author  : yangyuanqiang
    # @File    : demon5.py
    
    
    '''python3 操作图片需要
    pip install pillow
    '''
    from PIL import Image
    image = Image.open("test.jpg")
    position = (320, 65, 460, 220)
    cutjpg = image.crop(position).transpose(Image.ROTATE_180)
    image.paste(cutjpg, position)
    image.show()
    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    # @Time    : 2018/6/11 21:51
    # @Author  : yangyuanqiang
    # @File    : demon6.py
    
    
    '''python3 操作图片需要
    pip install pillow
    '''
    from PIL import Image
    image = Image.open("test.jpg")
    position = (320, 65, 460, 220)
    cutjpg = image.crop(position).transpose(Image.ROTATE_180)
    image.paste(cutjpg, position)
    
    (x, y) = image.size
    newx = 30
    newy = int(y*newx/x)
    newimage = image.resize((newx, newy))
    newimage.show()
    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    # @Time    : 2018/6/11 21:52
    # @Author  : yangyuanqiang
    # @File    : demon7.py
    
    
    import random
    import string
    
    from PIL import Image, ImageFont, ImageDraw, ImageFilter
    
    font_path = "msyh.ttf"
    number = 4
    size = (100, 30)
    bgcolor = (255, 255, 255)
    fontcolor = (0, 0, 255)
    linecolor = (255, 0, 0)
    draw_line = True
    # 加入干扰线条数的上下限
    line_number = 30
    
    
    #生成一个随机字符串
    
    def getNumber():
        source = list(string.ascii_letters) + list(string.digits)
        return "".join(random.sample(source, number))
    
    #绘制干扰线
    def getLine(draw, width, height):
        begin = random.randint(0, width), random.randint(0, height)
        end  = random.randint(0, width), random.randint(0, height)
        draw.line([begin, end], fill=linecolor)
    
    def getCode():
        width, height = size
        image = Image.new("RGBA", size, bgcolor)
        font = ImageFont.truetype(font_path, 25)
        draw = ImageDraw.Draw(image)
        text = getNumber()
        font_width, font_height = font.getsize(text)
        draw.text(((width - font_width) / 2, (height - font_height) / 2), text, font=font, fill=fontcolor)  # 填充字符串
        if draw_line:
            for i in range(line_number):
                getLine(draw, width, height)
    
        # image = image.transform((width + 20, height + 10), Image.AFFINE, (1, -0.3, 0, -0.1, 1, 0), Image.BILINEAR)  # 创建扭曲
        image = image.filter(ImageFilter.EDGE_ENHANCE_MORE)  # 滤镜,边界加强
        image.save('idencode.png')  # 保存验证码图片
        # image.show()
    if __name__ == '__main__':
        getCode()
  • 相关阅读:
    算法训练 表达式计算
    基础练习 十六进制转十进制
    基础练习 十六进制转十进制
    基础练习 十六进制转十进制
    New ways to verify that Multipath TCP works through your network
    TCP的拥塞控制 (Tahoe Reno NewReno SACK)
    Multipath TCP Port for Android 4.1.2
    How to enable ping response in windows 7?
    NS3
    Multipath TCP Port for Android
  • 原文地址:https://www.cnblogs.com/ivan-yang/p/9170235.html
Copyright © 2011-2022 走看看