Python3小知识第二话:读取图片信息

写在最前:

需要用到 PIL 模块:

PIL:Python Imaging Library,已经是 Python 平台事实上的图像处理标准库了。PIL 功能非常强大,但 API 却非常简单易用。

由于 PIL 仅支持到 Python 2.7,加上年久失修,于是一群志愿者在 PIL 的基础上创建了兼容的版本,名字叫 Pillow,支持最新 Python 3.x,又加入了许多新特性,因此,我们可以直接安装使用 Pillow。

1、Mac 和其他版本的 Linux 可以使用 pip 安装 Pillow,Pillow 是从 PIL fork 过来的 Python 图片库。

安装命令:pip3 install Pillow

2、如果是使用 Anaconda(Python 基础第一话:安装 Anaconda 及简单使用

安装 PIL 模块:conda install -n higo_3.7 pillow

一、代码演示

#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
  
from PIL import Image
  
file_data = '/Users/wufei/PycharmProjects/higo/python_imggen/image/1575297215983.jpg'
im = Image.open(file_data)
print("im(一个Image对象): ", im)
print("format(图片生成时的原格式,不是以文件后缀名为依据): ", im.format)
print("mode(图片模式): ", im.mode)
print("size(图片尺寸,以像素为单位): ", im.size)
print("width(图片像素宽): ", im.width)
print("height(图片像素高): ", im.height)
print("palette(调色板): ", im.palette)
print("info(一个与图片有关的数据组成的字典): ", im.info)

二、结果展示

im(一个Image对象):  <PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=1440x900 at 0x109586A10>
format(图片生成时的原格式,不是以文件后缀名为依据):  JPEG
mode(图片模式):  RGB
size(图片尺寸,以像素为单位):  (1440, 900)
width(图片像素宽):  1440
height(图片像素高):  900
palette(调色板):  None
info(一个与图片有关的数据组成的字典):  {'jfif': 257, 'jfif_version': (1, 1), 'jfif_unit': 0, 'jfif_density': (1, 1)}

三、Image 类的属性介绍

1、PIL.Image.format

图片生成时的原格式,不是以文件后缀名为依据
类型:string or None

2、PIL.Image.mode

图片模式。图片使用的像素格式,典型的格式有 “1”,“L”,“RGB”,or “CMYK”
类型:string

3、PIL.Image.size

图片尺寸(以像素为单位)
类型:(width, height)

4、PIL.Image.width

图片像素宽
类型:int

5、PIL.Image.height

图片像素高
类型:int

6、PIL.Image.palette

调色板。如果模式是“P”,则是一个ImagePalette类的实例
类型:ImagePalette or None

7、PIL.Image.info

一个与图片有关的数据组成的字典
类型:dict

四、原始图展示

猜你喜欢

转载自blog.csdn.net/weixin_42018518/article/details/103894575
今日推荐