# Transform 常用參數 ## Resize 將PIL影像進行影像縮放到固定大小 參數設定: * size: 可以設定一個固定長寬值,也可以長寬分別設定 ex: size=200 或是 size = (height, width) = (50,40) * interpolation: 圖在縮放採用的插值方法,default為PIL.Image.BILINEAR 還有其他方法PIL.Image.NEAREST, PIL.Image.BILINEAR and PIL.Image.BICUBIC.可以選擇 範例: ```python= size = 100 transform = transforms.Resize(size) new_img = transform(img_pil) new_img ``` ```python= size = (160, 80) transform = transforms.Resize(size) new_img = transform(img_pil) new_img ``` ## RandomHorizontalFlip 和 RandomVerticalFlip 圖片(PIL Image)會在給定的機率下隨機進行水平或是垂直翻轉。 參數設定: * p: 圖片要進行翻轉的機率。 範例: ```python= transform = transforms.Compose([ transforms.Resize((100,150)), transforms.RandomHorizontalFlip(p=0.9), ]) new_img = transform(img_pil) new_img ``` ```python= transform = transforms.Compose([ transforms.Resize((100,150)), transforms.RandomVerticalFlip(p=0.9), ]) new_img = transform(img_pil) new_img ``` ## GaussianBlur 圖片做高斯模糊化 參數設定: * kernel_size: 高斯kernel的大小。 * sigma: 高斯kernel生成的標準差,sigma值需為 1. float: (float),sigma固定在設定的float值 2. tuple: (min, max),sigma在(min, max)隨機取出一個值。 範例: ```python= transform = transforms.Compose([ transforms.GaussianBlur(7,3) ]) new_img = transform(img_pil) ``` ## Grayscale Grayscale將圖片轉換成灰階 參數設定: * num_output_channels (int,(1 or 3)): 輸出圖像要幾個channel * 1: image is single channel * 3: image is 3 channel with r == g == b 範例: ```python= transform = transforms.Compose([ transforms.Resize((100,150)), transforms.Grayscale(num_output_channels=1) ]) new_img = transform(img_pil) ```