Python 官方文档:入门教程 => 点击学习
这篇文章主要讲解了“python PIL Image图像处理基本操作有哪些”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Python PIL Imag
这篇文章主要讲解了“python PIL Image图像处理基本操作有哪些”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Python PIL Image图像处理基本操作有哪些”吧!
from PIL import Imageimg = 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 Imageimport numpy as npimg = Image.open('01.jpg')width, height = img.sizechannel_mode = img.modemean_value = np.mean(img)print(width)print(height)print(channel_mode)print(mean_value)
from PIL import Imagewidth = 200height = 100img_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 Imageimg = 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 Imageimg = 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, ImageFontimg = 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 Imageimg = Image.open('01.jpg')width, height = img.sizeimg_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 Imageimg = Image.open('01.jpg').convert('L')width, height = img.sizepim = 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] = 255else:img.putpixel((w, h), 0)# pim[w, h] = 0img.show()
from PIL import Imageimg = Image.open('01.jpg').convert('L')width, height = img.sizethreshold = 125for 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 Imageimg = 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 Imageimg = Image.open('test.png')width, height = img.sizechannel_mode = img.modeimg_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)# 图像水平右侧扩展一个ROIimg_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 Imageimport numpy as npimg = Image.open('01.jpg')array = np.array(img) # PIL.Image 转 numpyimg1 = Image.fromarray(array) # numpy转 PIL.Imageimg1 = Image.fromarray(array.astype('uint8'))img1.save('from_array.jpg')
感谢各位的阅读,以上就是“python PIL Image图像处理基本操作有哪些”的内容了,经过本文的学习后,相信大家对python PIL Image图像处理基本操作有哪些这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是编程网,小编将为大家推送更多相关知识点的文章,欢迎关注!
--结束END--
本文标题: python PIL Image图像处理基本操作有哪些
本文链接: https://www.lsjlt.com/news/326642.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0