zoukankan      html  css  js  c++  java
  • Python Pillow(PIL 第三方模块)和 cv2 (opencv第三方模块)对图片的 resize 操作 (缩放图片大小)

    PIL 模块的 resize  操作:

    1.  从文件中读取图片,然后  resize  大小:

    import matplotlib.pyplot as plt
    import numpy as np
    from PIL import Image
    
    img=Image.open(r"1.jpg")
    print("原图的height,weight分别为:", np.asarray(img).shape[:2])
    plt.imshow(np.asarray(img))
    plt.show()
    
    height, weight = (np.asarray(img).shape)[:2]
    height = height//10
    weight = weight//10
    img2 = Image.Image.resize(img, (weight, height))
    print("resized后图的height,weight分别为:", np.asarray(img2).shape[:2])
    plt.imshow(np.asarray(img2))
    plt.show()

    2.  从字节码(Bytes)中读取图片,然后  resize  大小:

    import matplotlib.pyplot as plt
    import numpy as np
    from PIL import Image
    from io import BytesIO
    
    
    img = open("1.jpg", "rb").read()   #读取序列化的二进制码
    img = BytesIO( img )
    img = Image.open( img )
    print("原图的height,weight分别为:", np.asarray(img).shape[:2])
    plt.imshow(np.asarray(img))
    plt.show()
    
    height, weight = (np.asarray(img).shape)[:2]
    height = height//10
    weight = weight//10
    img2 = Image.Image.resize(img, (weight, height))
    print("resized后图的height,weight分别为:", np.asarray(img2).shape[:2])
    plt.imshow(np.asarray(img2))
    plt.show()

    ---------------------------------------------------

    CV2 模块的 resize  操作:

    读入图像

    使用函数cv2.imread()来读取图像。图像应该在工作目录中,或者应该给出图像的完整路径。

    imread(filename[, flags]) -> retval

    函数imread从指定文件加载图像并返回一个numpy.ndarray对象类型像素值。 如果图像无法读取(由于文件丢失,权限不当,格式不受支持或格式无效),函数返回一个空矩阵

    第二个参数是一个标志,用于指定应读取图像的方式。

    • cv2.IMREAD_COLOR:加载彩色图像。图像的任何透明度都将被忽略。这是默认标志。 flags=1
    • cv2.IMREAD_GRAYSCALE:以灰度模式加载图像 flags=0
    • cv2.IMREAD_UNCHANGED:加载包含Alpha通道的图像 flags=-1

    注意

    而不是这三个标志,你可以简单地传递整数1,0或-1。

    取自于:  https://blog.csdn.net/hubingshabi/article/details/80144706

    CV2 读取图片,  CV2展示图片:

    import matplotlib.pyplot as plt
    import numpy as np
    import cv2
    # rgb图
    img=cv2.imread(r"1.jpg", 1)
    # 灰度图
    #img=cv2.imread(r"1.jpg", 0)
    
    print("原图的height,weight分别为:", np.asarray(img).shape[:2])
    #plt.imshow(np.asarray(img))
    #plt.show()
    cv2.imshow("img", mat=img)
    cv2.waitKey (0)
    
    
    height, weight = (img.shape)[:2]
    height = height//3
    weight = weight//3
    img2 = cv2.resize(img, (weight, height))
    print("resized后图的height,weight分别为:", img2.shape[:2])
    #plt.imshow(np.asarray(img2))
    #plt.show()
    cv2.imshow("img2", mat=img2)
    cv2.waitKey (0)
    cv2.destroyAllWindows()

    CV2 读取图片,  matplotlib展示图片:   把cv2的bgr转换为rgb,然后展示。       [...,::-1]

    import matplotlib.pyplot as plt
    import numpy as np
    import cv2
    # rgb图
    img=cv2.imread(r"1.jpg", 1)
    # 灰度图
    #img=cv2.imread(r"1.jpg", 0)
    
    print("原图的height,weight分别为:", np.asarray(img).shape[:2])
    plt.imshow(np.asarray(img)[...,::-1])
    plt.show()
    
    
    height, weight = (img.shape)[:2]
    height = height//3
    weight = weight//3
    img2 = cv2.resize(img, (weight, height))
    print("resized后图的height,weight分别为:", img2.shape[:2])
    plt.imshow(np.asarray(img2)[...,::-1])
    plt.show()

    -----------------------------------------------------------------

    参考网址:

    https://blog.csdn.net/sinat_26917383/article/details/78559709

    https://blog.csdn.net/hubingshabi/article/details/80144706

    ----------------------------

     1.jpg

    注:上面的代码均使用该图片(1.jpg)做测试。

    --------------------------------

  • 相关阅读:
    利用XShell上传、下载文件(使用sz与rz命令)
    我做的一个websocket的demo(php server)
    linux下如何实现mysql数据库每天自动备份定时备份
    php利用ob缓存机制实现页面静态化方法全解
    MySQL数据备份之mysqldump使用
    将jpg压缩成webp格式的图片
    ab网站压力测试命令的参数、输出结果的中文注解
    网页优化实例
    JQ 为未来元素添加事件处理器—事件委托
    Web前端优化最佳实践及工具集锦
  • 原文地址:https://www.cnblogs.com/devilmaycry812839668/p/13752118.html
Copyright © 2011-2022 走看看