PIL包裡面的Image模組裡面的函式講解
阿新 • • 發佈:2018-12-01
匯入該模組
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 = "" self.size = (0, 0) 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 # <ImagingCore at 0x7f3a382e52f0> _llg.mode # 'RGBA' _llg.info # {} _llg.category # 0 _llg.readonly # 0 _llg.pyaccess # 無輸出 _llg.width # 2048 _llg.height # 1024 type(_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.''' pass def 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 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