图像处理如何实现才最简单?用Python几分钟就搞定了!

114 阅读6分钟

今天,我们生活在一个数字世界,数据在我们周围流动。我们看到了很多数字图像数字图像处理这对我们非常有益,因为它使我们能够增强图像,并帮助从这些图像中提取有用的信息。

Python是一种通用的编程语言,它提供了许多图像处理库,用于向数字图像中添加图像处理功能。Python中一些最常见的图像处理库是OpenCV , Python成像库(PIL) , Scikit-图像等。

这个**Python成像库(PIL)**在新版本中也被称为Pillow,尽管Pillow已经分叉了PIL存储库并添加了Python3.x支持。

在处理图像时通常遵循以下步骤:

  • 通过图像采集工具导入图像。
  • 分析和操纵图像。
  • 输出可以是更改后的图像,也可以是基于该图像的报表。

PIL是执行简单图像处理的最佳选择,例如调整大小(缩放)、旋转、裁剪(部分截断),以及将各种过滤器应用于图像,因为它比OpenCV等其他库更简单和更容易理解。它支持多种图像格式,如“jpeg”、“png”、“gif”、“ppm”、“tiff”等,这使得图像处理相对容易。

今天,我们将深入研究PythonPIL及其特性以及代码实现。

安装:

Windows:

我们可以在Windows系统上使用PIP安装枕头。只需在命令提示符窗口中运行以下命令:

​pip install pillow

或:

easy_install Pillow

MacOS:

要在MacOS上安装Pillow,请在Bash终端上运行以下代码:

$ sudo pip install Pillow

Linux:

要在Linux机器上安装Pillow,只需使用:

sudo pip install Pillow

加载图像:

要加载映像,首先需要导入Image从PIL,然后调用Image.open()函数,传递图像路径。属性返回Image对象数据类型的值。 Image.open() 函数,并且该图像对象可以在其他方法的帮助下执行各种图像操作。可以将对图像对象所做的所有修改保存到图像文件中。 save() 方法。这个**show()** 方法显示已加载到图像对象中的图像。

# importing the module
from PIL import Image 

# opening the image stored in the local path.
im = Image.open('D:/Image/Kingfisher.jpg') 

im.show()

产出:

基本图像属性:

利用图像模块中不同的功能,可以找到图像的大小(宽度、高度)、格式和模式等基本特性。

# importing the module
from PIL import Image 

im = Image.open('D:/Image/Kingfisher.jpg')

# Find image dimensions i.e. width and height
print("Image size:", im.size) 

# Find the image format
print("Image format:", im.format)

# Find the image mode i.e. RGB, RGBA, HSV etc.
print("Image mode:", im.mode)

产出:

Image size: (1280, 853)

Image format: JPEG

Image mode: RGB

将图像转换为其他格式:

​​我们可以使用 save() 方法在图像模块中。该方法有两个参数,第一个是新文件的名称,另一个是我们要转换图像的格式。

#importing the module
from PIL import Image 

im = Image.open('D:/Image/Kingfisher.jpg')

# Changing image format and saving the new image
im.save('Kingfisher.png', 'png')

产出:

以下是格式更改的图像:

裁剪图像:

这个 Image.crop() 方法帮助裁剪图像,我们可以指定作物的尺寸。这个crop()方法采用定义左、上、右和下像素坐标的4值元组.

#importing the module
from PIL import Image 

im = Image.open('D:/Image/Kingfisher.jpg')

# Cropping the loaded image using crop()
img_crop = im.crop((450,20,1000,500))

img_crop.show()

产出:​

旋转图像:

这个 Image.rotate() 函数可用于旋转图像。它以旋转度值作为参数,并返回图像的旋转副本。

from PIL import Image

im = Image.open('D:/Image/Kingfisher.jpg')

# Rotating image using rotate() function by 90 degrees.
img_rotate = im.rotate(90)

img_rotate.save('Kingfisher_Rotated.jpg')

产出:​

调整图像大小:

这个 Image.resize() 函数可用于将图像调整到所需的高度和宽度。该函数以宽度和高度作为元组,并返回大小调整的图像对象。

from PIL import Image

im = Image.open('D:/Image/Kingfisher.jpg')

# Resizing an image using resize() function.
img_resize = im.resize((500,150))

img_resize.show()

产出:​

模糊图像:

这个ImageFilter模块由预先定义的一组过滤器的定义组成。这些过滤器可以与filter()方法,我们甚至可以通过 ImageFilter.BLUR 在原始图像上。然后,过滤器函数返回包含原始图像模糊副本的图像对象。

# importing Image and ImageFilter modules
from PIL import Image, ImageFilter 

im = Image.open("D:/Image/Kingfisher.jpg")

# Applying blur operation on the image
img_blur = im.filter(ImageFilter.BLUR) 

img_blur.show()

产出:​

灰度图像:

这个 Image.convert() 方法允许我们将彩色图像转换为灰度图像(黑白)。

# importing Image and ImageFilter modules
from PIL import Image, ImageFilter 

im = Image.open("D:/Image/Kingfisher.jpg")
grayscale = im.convert('L')
grayscale.show()

产出:

图像增强滤波器:

这个 ImageFilter 模块包含可用于图像增强的各种过滤器,如轮廓,细节,边缘增强,浮雕,光滑,锐利这些过滤器中的一些显示在下面:

# importing modules
from PIL import Image, ImageFilter
im = Image.open("D:/Image/Kingfisher.jpg")

# Applying filters
img1 = im.filter(ImageFilter.CONTOUR)
img2 = im.filter(ImageFilter.EMBOSS)

img1.show()
img2.show()

产出:

轮廓过滤器:

浮雕过滤器:

利用图像绘制:

使用ImageDraw模块,可以方便地在现有图像对象的基础上绘制简单的二维图形。导入ImageDraw模块后,我们只需创建一个 ImageDraw.Draw 对象将Image对象作为参数传递。然后,我们可以用内置的函数绘制各种对象,如直线、矩形、圆圈、三角形等。这里我们在现有的图像上画了一个矩形。这个 rectangle() 函数采用三个参数:四个角点的位置、轮廓颜色和填充颜色值。

# importing required modules
from PIL import Image, ImageDraw

im = Image.open("D:/Image/Kingfisher.jpg")

draw = ImageDraw.Draw(im)

# Drawing a rectangle using rectangle method()
draw.rectangle((200,200,400,400), fill = "red", outline = "blue")
im.show()

产出:

获取RGB值:

ImageColor模块由与RGB操作相关的功能组成。 ImageColor.getrgb() 方法将作为字符串传递的颜色值转换为等效的RGB格式。它以颜色字符串作为输入,并返回RGB值的元组。

# importing ImageColor module
from PIL import ImageColor

im1 = ImageColor.getrgb("#00ff00")
print(im1)

im2 = ImageColor.getrgb("yellow")
print(im2)

产出:

(0, 255, 0)

(255, 255, 0)

我们甚至可以使用 new()getrgb() 方法:

# importing required modules
from PIL import Image, ImageColor

img = Image.new("RGB", (500, 500), ImageColor.getrgb("#FF7103"))
img.show()

产出:

结语..。

这都是关于PythonImagingLibrary及其基本功能的。PIL库使得根据我们的需求调整和处理图像相对容易,只需几行代码即可。你还在等什么?开始你的图像处理之旅吧需要全部完整代码记得点这里获取