PIL包里面的Image模块里面的函数讲解,不能直接对numpy存储成图像,要进行转化再存取
发布日期:2021-06-29 11:45:00 浏览次数:3 分类:技术文章

本文共 4948 字,大约阅读时间需要 16 分钟。

 导入该模块

from PIL import Image # 导入Image模块

它内置的属性如下:

class Image(object):    def __init__(self):        # FIXME: take "new" parameters / other image?        # FIXME: turn mode and size into delegating properties?        self.im = None        self.mode = ""  # 图片的模式,比如说RGB,RGBA,L等        self.size = (0, 0)  # 图片的大小,在这里显示的顺序是(W,H),和平常的顺序相反        self.format = ""  # 图片的格式,比如说JPEG        self.palette = None        self.info = {}        self.category = NORMAL        self.readonly = 0        self.pyaccess = None    @property    def width(self):        return self.size[0]    @property    def height(self):        return self.size[1]

因此

_llg = Image.open('/home/zzp/Test/g31.png') # 这是一张(1024,2048,4)的0-255的图片_llg.size  # (2048,1024)_llg.im    #  
_llg.mode # 'RGBA'_llg.info # {}_llg.category # 0_llg.readonly # 0_llg.pyaccess # 无输出_llg.width # 2048_llg.height # 1024type(_llg) # PIL.PngImagePlugin.PngImageFile_img = Image.open('/home/zzp/Test/t3.png') # 这是一张(1024,2048,3)的0-255的图片_img.mode # 'RGB'_img.info # {'aspect': (72, 72), 'gamma': 0.45455}# 其他的参数和上面相同_lg = Image.open('/home/zzp/Test/g34.png') # 这是一张(1024,2048)的0-255的图片_lg.mode # 'L'_lg.info # {}# 其他参数和上面一样

它内置的方法如下:

def tobytes(self, encoder_name="raw", *args):  # 下面的方法都只写它的名字和调用参数了    pass #  Return image as a bytes object.def tobitmap(self, name="image"):    pass # Returns the image converted to an X11 bitmap.def frombytes(self, data, decoder_name="raw", *args):    pass  # Loads this image with pixel data from a bytes object.def load(self):    pass  # Allocates storage for the image and loads the pixel data.  In          # normal cases, you don't need to call this method, since the          # Image class automatically loads an opened image when it is          # accessed for the first time. This method will close the file          # associated with the image.def verify(self):    '''        Verifies the contents of a file. For data read from a file, this        method attempts to determine if the file is broken, without        actually decoding the image data.  If this method finds any        problems, it raises suitable exceptions.  If you need to load        the image after using this method, you must reopen the image        file.'''    passdef copy(self):    pass  # Copies this image. Use this method if you wish to paste things into an image, but still retain the original.def crop(self, box=None):    pass  # Returns a rectangular region from this image. The box is a 4-tuple defining the left, upper, right, and lower pixel coordinate.

下面的方法比较常用

def open(fileppath,mode='r'):    pass    # 功能是打开路径的图片文件。返回一个PIL对象。这是一个lazy的operation,文件在使用的时候才真正的读进去。# 以下的self表示PIL对象def resize(self, size, resample=NEAREST):    pass  # Returns a resized copy of this image.    ''' size: The requested size in pixels, as a 2-tuple:(width, height).        resample: An optional resampling filter.  This can be one of :py:attr:`PIL.Image.NEAREST`, :py:attr:`PIL.Image.BOX`, :py:attr:`PIL.Image.BILINEAR`, :py:attr:`PIL.Image.HAMMING`, :py:attr:`PIL.Image.BICUBIC` or :py:attr:`PIL.Image.LANCZOS`. If omitted(省略), or if the image has mode "1" or "P", it is set :py:attr:`PIL.Image.NEAREST`.    '''def rotate(self, angle, resample=NEAREST, expand=0, center=None, translate=None):    pass # Returns a rotated copy of this image.  This method returns a copy of this image, rotated the given number of degrees counter clockwise around its centre.    ''' angle: 逆时针方向,是一个number,不要求是整数        resample:同上        expand: If True,expands the output image to make it large enough to hold the entire rotated image. If False or omitted,make the output image the same size as the input image.        center: center of rotation (a 2-tuple)旋转中心,默认是图片的中点。    '''def save(self, filename, format=None, **params):    pass  # 保存图片在filename路径下,如果format默认则该图片保存的类型是filename写明的文件类型,如果filename只有路径没有文件名字,那么format一定要指定文件格式def show(self, title=None, command=None):    pass  # Displays this image. This method is mainly intended for debugging purposes.def transpose(self, method):    pass  # Transpose image (flip or rotate in 90 degree steps)# method = PIL.Image.FLIP_LEFT_RIGHT左右颠倒,PIL.Image.FLIP_TOP_BOTTOM上下颠倒,PIL.Image.ROTATE_90/180/270顺时针旋转90度,以及180度,270度,PIL.Image.TRANSPOSE不明def convert(self, mode=None, matrix=None, dither=None,palette=WEB, colors=256):    pass  # 转换mode  mode = 'RGB','L'等# 例如在读取图片的时候 Image.open('1.jpg').convert('RGB')def split(self):    pass  # 将图片分成单独的图片层(bands),返回一个元组。如果是RGB图像那么返回三张图片,分别是RGB图层的复制,如果仅仅向获取某一通道的图层,使用下面的函数def getchannel(self,channel):    pass  # channel是通道的索引,比如RGB图像channel=0或者channel='R'获取R通道的图层def getbands(self):    pass  # 返回一个元组,得到channel的名字。如果是RGB图则得到('R','G','B')

 PIL和numpy之间的转化:

"""    If you have an image in NumPy::      from PIL import Image      import numpy as np      im = Image.open('hopper.jpg')      a = np.asarray(im)    Then this can be used to convert it to a Pillow image::      im = Image.fromarray(a)"""

 

还可以参考

转载地址:https://blog.csdn.net/zz2230633069/article/details/84667236 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:pytorch 训练数据以及测试 全部代码(9)---deeplab v3+ 对Cityscapes数据的处理
下一篇:numpy与Image互转以及它们的size不同,还有关于plt

发表评论

最新留言

网站不错 人气很旺了 加油
[***.192.178.218]2024年04月16日 17时39分36秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章