• Python学习笔记--图像处理--Pillow


    from PIL import Image
    from PIL import ImageFilter
    
    image = Image.open(r"C:UsersADDesktop	est.jpg")
    # 通过Image对象的format属性获得图像的格式
    print(image.format) # JPEG
    # 通过Image对象的size属性获得图像的尺寸
    print(image.size)   # (1920, 1080)
    # 通过Image对象的mode属性获取图像的模式
    print(image.mode)   # RGB
    # 通过Image对象的show方法显示图像
    # image.show()
    # 通过Image对象的crop方法指定剪裁区域剪裁图像
    # image.crop((80, 20, 310, 360)).show()
    # 通过Image对象的thumbnail方法生成指定尺寸的缩略图
    # image.thumbnail((128, 128))
    # image.show()
    # 读取骆昊的照片获得Image对象
    image2 = Image.open(r"C:UsersADDesktop	est2.jpg")
    # 读取吉多的照片获得Image对象
    # 从吉多的照片上剪裁出吉多的头
    # guido_head = image.crop((80, 20, 310, 360))
    # width, height = guido_head.size
    # # 使用Image对象的resize方法修改图像的尺寸
    # # 使用Image对象的paste方法将吉多的头粘贴到image2的照片上
    # image2.paste(guido_head.resize((int(width / 1.5), int(height / 1.5))), (172, 40))
    # image2.show()
    # 使用Image对象的rotate方法实现图像的旋转
    # image.rotate(45).show()
    # # 使用Image对象的transpose方法实现图像翻转
    # # Image.FLIP_LEFT_RIGHT - 水平翻转
    # # Image.FLIP_TOP_BOTTOM - 垂直翻转
    # image.transpose(Image.FLIP_TOP_BOTTOM).show()
    
    # # 操作像素
    # for x in range(80, 310):
    #     for y in range(20, 360):
    #         # 通过Image对象的putpixel方法修改图像指定像素点
    #         image.putpixel((x, y), (128, 128, 128))
    # image.show()
    
    # # 滤镜效果
    # from PIL import ImageFilter
    # # 使用Image对象的filter方法对图像进行滤镜处理
    # # ImageFilter模块包含了诸多预设的滤镜也可以自定义滤镜
    # image.filter(ImageFilter.CONTOUR).show()
    # image2.filter(ImageFilter.CONTOUR).show()
    
    # 使用pillow绘图
    # Pillow中有一个名为ImageDraw的模块,
    # 该模块的Draw函数会返回一个ImageDraw对象,
    # 通过ImageDraw对象的arc、line、rectangle、ellipse、polygon等方法,
    # 可以在图像上绘制出圆弧、线条、矩形、椭圆、多边形等形状,
    # 也可以通过该对象的text方法在图像上添加文字。
    from PIL import Image,ImageDraw,ImageFont
    import random
    
    def create_random_color():
        return random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)
    
    width, height = 800, 600
    # 创建一个800*600的图像,背景色为白色
    image = Image.new(mode='RGB', size=(width, height), color=(255, 255, 255))
    # 创建一个ImageDraw对象
    drawer = ImageDraw.Draw(image)
    # # 通过指定字体和大小获得ImageFont对象
    # font = ImageFont.truetype('Kongxin.ttf', 32)
    # # 通过ImageDraw对象的text方法绘制文字
    # drawer.text((300, 50), 'Hello, world!', fill=(255, 0, 0), font=font)
    # 通过ImageDraw对象的line方法绘制两条对角直线
    drawer.line((0, 0, width, height), fill=(0, 0, 255), width=2)
    drawer.line((width, 0, 0, height), fill=(0, 0, 255), width=2)
    xy = width // 2 - 60, height // 2 - 60, width // 2 + 60, height // 2 + 60
    # 通过ImageDraw对象的rectangle方法绘制矩形
    drawer.rectangle(xy, outline=(255, 0, 0), width=2)
    # 通过ImageDraw对象的ellipse方法绘制椭圆
    for i in range(4):
        left, top, right, bottom = 150 + i * 120, 220, 310 + i * 120, 380
        drawer.ellipse((left, top, right, bottom), outline=create_random_color(), width=8)
    # 显示图像
    image.show()
    # 保存图像
    image.save('result.png')
  • 相关阅读:
    微信小程序之----加载中提示框loading
    微信小程序之----消息提示框toast
    微信小程序之----弹框组件modal
    浅析浏览器的回流与重绘 (Reflow & Repaint)
    关于input的一些问题解决方法分享
    关于js中 toFixed()的一个小坑
    浅谈js中null和undefined的区别
    浅谈JS中的闭包
    浅谈JS中的浅拷贝与深拷贝
    css设置居中的方案总结
  • 原文地址:https://www.cnblogs.com/jifeng0902/p/14711589.html
Copyright © 2020-2023  润新知