import os, time, random, zipfile, re
from PIL import Image, ImageDraw, ImageFont, ImageFilter

def main():
    testPIL()
    testZip()
    testRe()

def testPIL():
#     testPIL1()
#     testPIL2()
    testPIL3()    
    
def testPIL1():
    print("swe")
    # 打开一个jpg图像文件,注意是当前路径:
    im = Image.open(r'D:\work_space\eclipse\Pb\test.jpg')
    # 获得图像尺寸:
    w, h = im.size
    print('Original image size: %sx%s' % (w, h))
    # 缩放到50%:
    im.thumbnail((w // 2, h // 2))
    print('Resize image to: %sx%s' % (w // 2, h // 2))
    # 把缩放后的图像用jpeg格式保存:
    im.save('thumbnail.jpg', 'jpeg')
    
def testPIL2():
    # 打开一个jpg图像文件,注意是当前路径:
    im = Image.open('test.jpg')
    # 应用模糊滤镜:
    im2 = im.filter(ImageFilter.BLUR)
    im2.save('blur.jpg', 'jpeg')

# 随机字母:
def rndChar():
    return chr(random.randint(65, 90))

# 随机颜色1:
def rndColor():
    return (random.randint(64, 255), random.randint(64, 255), random.randint(64, 255))


# 随机颜色2:
def rndColor2():
    return (random.randint(32, 127), random.randint(32, 127), random.randint(32, 127))


def testPIL3():
    # 240 x 60:
    width = 60 * 4
    height = 60
    image = Image.new('RGB', (width, height), (255, 255, 255))
    # 创建Font对象:
    font = ImageFont.truetype('simsun.ttc', 36)
    # 创建Draw对象:
    draw = ImageDraw.Draw(image)
    # 填充每个像素:
    for x in range(width):
        for y in range(height):
            draw.point((x, y), fill=rndColor())
    # 输出文字:
    for t in range(4):
        draw.text((60 * t + 10, 10), rndChar(), font=font, fill=rndColor2())
    # 模糊:
    image = image.filter(ImageFilter.BLUR)
    image.save('code.jpg', 'jpeg')

def testZip():
    with open('a.log', "a+") as fp:
        fp.write("123456")
        
    with open('data.data', "a+") as fp:
        fp.write("abcdef")
        
    z = zipfile.ZipFile('laxi.zip', 'w')
    z.write('a.log')
    z.write('data.data')    
    z.close()
    os.remove('a.log')
    os.remove('data.data')
    
    time.sleep(3)
    z = zipfile.ZipFile('laxi.zip', 'r')
    z.extractall()
    z.close()
    time.sleep(3)
    os.remove('a.log')
    os.remove('data.data')
    os.remove('laxi.zip')
    
def testRe():
    # match()方法判断是否匹配,如果匹配成功,返回一个Match对象,否则返回None
    # 强烈建议使用Python的r前缀,就不用考虑转义的问题了
    result = re.match(r'^\d{3}\-\d{3,8}$', '010-12345')
    if result :
        print("match!")
    else:      
        print("not match!")
    
    s1 = "a b   c"
    print(s1.split(' '))  # ['a', 'b', '', '', 'c']
    # '\s'匹配任何不可见字符,包括空格、制表符、换页符等等
    print(re.split(r'\s+', s1))  # ['a', 'b', 'c']
    s1 = 'a, b c'
    s2 = re.split(r'[\s\,]+', s1)
    print(s2)  # ['a', 'b', 'c']
    
    # 分组:正则表达式还有提取子串的强大功能。用()表示的就是要提取的分组(Group)
    # ^(\d{3})-(\d{3,8})$分别定义了两个组,可以直接从匹配的字符串中提取出区号和本地号码:
    m = re.match(r'^(\d{3})-(\d{3,8})$', '010-12345')
    print(m.groups())  # ('010', '12345')
    # group(0)永远是原始字符串,group(1)、group(2)……表示第1、2、……个子串
    print(m.group(0))  # 010-12345
    print(m.group(1))  # 010
    print(m.group(2))  # 12345
    
    # 贪婪匹配:正则匹配默认是贪婪匹配,也就是匹配尽可能多的字符
    s = '102300'
    # 由于\d+采用贪婪匹配,直接把后面的0全部匹配了,结果0*只能匹配空字符串了
    print(re.match(r'^(\d+)(0*)$', s).groups())  # ('102300', '')
    # 加个?就可以让\d+采用非贪婪匹配
    print(re.match(r'^(\d+?)(0*)$', '102300').groups())  # ('1023', '00')    
    
    print("tea for too".replace("too", "two")) # "tea for two"
    #\b匹配一个单词的边界,也就是指单词和空格间的位置
    print(re.findall(r'\bf[a-z]*', 'which foot f fell')) #['foot', 'f', 'fell']
    # 将( 和 ) 之间的表达式定义为“组”(group),并且将匹配这个表达式的字符保存到一个
    #临时区域(一个正则表达式中最多可以保存9个),它们可以用 \1 到\9 的符号来引用。
    print(re.sub(r'(\b[a-z]+) \1', r'\1', 'cat in the the hat')) #'cat in the hat'
    
if __name__ == '__main__':
    main()

match!
['a', 'b', '', '', 'c']
['a', 'b', 'c']
['a', 'b', 'c']
('010', '12345')
010-12345
010
12345
('102300', '')
('1023', '00')
tea for two
['foot', 'f', 'fell']
cat in the hat

优质内容筛选与推荐>>
1、3.数据存储
2、当心!删除小程序,这些东西都会丢
3、Mybatis#BaseExecutor源码解析BaseExecutor源码解析
4、Dora.Interception,为.NETCore度身打造的AOP框架[3]:Interceptor的注册
5、业内称新零售将助推数字经济重塑经济格局


长按二维码向我转账

受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。

    阅读
    好看
    已推荐到看一看
    你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
    已取消,“好看”想法已同步删除
    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号