您好,登錄后才能下訂單哦!
這篇文章主要講解了“python PIL Image圖像處理基本操作有哪些”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“python PIL Image圖像處理基本操作有哪些”吧!
from PIL import Image img = Image.open('01.jpg') imgGrey = img.convert('L') img.show() imgGrey.show() img.save('img_copy.jpg') imgGrey.save('img_gray.jpg')
from PIL import Image import numpy as np img = Image.open('01.jpg') width, height = img.size channel_mode = img.mode mean_value = np.mean(img) print(width) print(height) print(channel_mode) print(mean_value)
from PIL import Image width = 200 height = 100 img_white = Image.new('RGB', (width,height), (255,255,255)) img_black = Image.new('RGB', (width,height), (0,0,0)) img_L = Image.new('L', (width, height), (255)) img_white.show() img_black.show() img_L.show()
from PIL import Image img = Image.open('01.jpg') width, height = img.size # 獲取指定坐標位置像素值 pixel_value = img.getpixel((width/2, height/2)) print(pixel_value) # 或者使用load方法 pim = img.load() pixel_value1 = pim[width/2, height/2] print(pixel_value1) # 設置指定坐標位置像素的值 pim[width/2, height/2] = (0, 0, 0) # 或使用putpixel方法 img.putpixel((w//2, h//2), (255,255,255)) # 設置指定區域像素的值 for w in range(int(width/2) - 40, int(width/2) + 40): for h in range(int(height/2) - 20, int(height/2) + 20): pim[w, h] = (255, 0, 0) # img.putpixel((w, h), (255,255,255)) img.show()
from PIL import Image img = Image.open('01.jpg') # 通道分離 R, G, B = img.split() R.show) G.show() B.show() # 通道合并 img_RGB = Image.merge('RGB', (R, G, B)) img_BGR = Image.merge('RGB', (B, G, R)) img_RGB.show() img_BGR.show()
from PIL import Image, ImageDraw, ImageFont img = Image.open('01.jpg') # 創建Draw對象: draw = ImageDraw.Draw(img) # 字體顏色 fillColor = (255, 0, 0) text = 'print text on PIL Image' position = (200,100) draw.text(position, text, fill=fillColor) img.show()
from PIL import Image img = Image.open('01.jpg') width, height = img.size img_NEARESET = img.resize((width//2, height//2)) # 縮放默認模式是NEARESET(最近鄰插值) img_BILINEAR = img.resize((width//2, height//2), Image.BILINEAR) # BILINEAR 2x2區域的雙線性插值 img_BICUBIC = img.resize((width//2, height//2), Image.BICUBIC) # BICUBIC 4x4區域的雙三次插值 img_ANTIALIAS = img.resize((width//2, height//2), Image.ANTIALIAS) # ANTIALIAS 高質量下采樣濾波
from PIL import Image img = Image.open('01.jpg').convert('L') width, height = img.size pim = img.load() for w in range(width): for h in range(height): if pim[w, h] > 100: img.putpixel((w, h), 255) # pim[w, h] = 255 else: img.putpixel((w, h), 0) # pim[w, h] = 0 img.show()
from PIL import Image img = Image.open('01.jpg').convert('L') width, height = img.size threshold = 125 for w in range(width): for h in range(height): if img.getpixel((w, h)) > threshold: img.putpixel((w, h), 255) else: img.putpixel((w, h), 0) img.save('binary.jpg')
from PIL import Image img = Image.open('01.jpg') width, height = img.size # 前兩個坐標點是左上角坐標 # 后兩個坐標點是右下角坐標 # width在前, height在后 box = (100, 100, 550, 350) region = img.crop(box) region.save('crop.jpg')
# 邊界擴展 from PIL import Image img = Image.open('test.png') width, height = img.size channel_mode = img.mode img_makeBorder_full = Image.new(channel_mode, (2*width, height)) img_makeBorder_part = Image.new(channel_mode, (width+200, height)) # 圖像水平擴展整個圖像 img_makeBorder_full.paste(img, (0, 0, width, height)) img_makeBorder_full.paste(img, (width, 0, 2*width, height)) # 前兩個坐標點是左上角坐標 # 后兩個坐標點是右下角坐標 # width在前, height在后 box = (width-200, 0, width, height) region = img.crop(box) # 圖像水平右側擴展一個ROI img_makeBorder_part.paste(img, (0, 0, width, height)) img_makeBorder_part.paste(region, (width, 0, width+200, height)) img_makeBorder_part.show() img_makeBorder_full.show()
from PIL import Image import numpy as np img = Image.open('01.jpg') array = np.array(img) # PIL.Image 轉 numpy img1 = Image.fromarray(array) # numpy轉 PIL.Image img1 = Image.fromarray(array.astype('uint8')) img1.save('from_array.jpg')
感謝各位的閱讀,以上就是“python PIL Image圖像處理基本操作有哪些”的內容了,經過本文的學習后,相信大家對python PIL Image圖像處理基本操作有哪些這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。