Python图像处理——PIL、skImage、OpenCV

PIL

from PIL import Image
def Image.open(fp, mode='r'):
	'''
	Opens and identifies the given image file.
	This is a lazy operation; this function identifies the file, 
	but the file remains open and the actual image data is not read 
	from the file until you try to process the data.
	
	file--A filename(string) or a file object.
	mode
	'''
	'''
	The Image Class
	open()
	new()
	frombytes()
	'''
	
def Image.load():
	'''
	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 Image.close():
	'''
	Close the file pointer, if possible.
	This operation will destroy the image core and release it's memory.
	The image data will be unusable afterward.
	This function is only required to close images that have not had
	their file read and closed by the load() method.
	'''
import numpy as np
# 'Image' object is not subscriptable
# 'Image' object is not callable
src = Image.open(fp=path, mode="r")	# image(color, 3d)
rows, cols = src.size
src_np = np.array(src)				# array(3d)
cols, rows, channels = src_np.shape
print(src_np[0, 700, 2])
# resize: change the arrage size, add the dim
# reshape: a new shape
src_reshape = src_np.reshape((rows*cols*channels))
# h*W*C + w*C + c
print(b[0*720*3 + 700*3 + 2])

gray = src.convert("L")				# image(gray, 2d)
rows, cols = gray.size
gray_np = np.array(gray)			# array(2d)
cols, rows = gray_np.shape
print(gray_np[0][1])

gray.show()

skimage

from skimage import io
src = io.imread(fname=path, as_gray=False)
gray1 = color.rgb2gray(rgb=src)
gray2 = io.imread(fname=path, as_gray=True)
cols, rows = gray2.shape

io.show()

OpenCV

import cv2
src = cv2.imread(filename=path, flags=1)
gray = cv2.imread(filename=path, flags=0)
cols, rows = gray.shape

cv2.rectangle(img=img, pt1=(left, upper), pt2=(right, lower), 
			  color=(0,0,255), thickness=2)
cv2.putText(img=img, text=draw_text, org=(left, upper - 5), 
			fontFace=cv2.FONT_HERSHEY_COMPLEX_SMALL, fontScale=.5, 
			color=(0,0,255), thickness=1)
cv2.namedWindow(winname="Result")
cv2.imshow(winname="Result", mat=img)
if cv2.waitKey(0) == 27:
	cv2.destroyAllWindows()

matplotlib

import matplotlib.pyplot as plt

plot array

import numpy as np
a = [[1,2],[2,3],[3,4]]     # list
b = np.array(a)             # list -> array
plt.plot(b[:,0], b[:,1], '.')
plt.show()

plot image

plt.figure(title)
plt.imshow(img)
# add rectange
rect = plt.Rectangle((xmin, ymin), xmax - xmin, ymax - ymin, fill=False, edgecolor="red", linewidth=2)
plt.gca().add_patch(rect)
# add text
plt.gca().text(xmin, ymin - 2, "{:s}".format(class_name), bbox=dict(facecolor="white", alpha=0.01), fontsize=12, color="red")
plt.axis('off')				# without coordinate axis
plt.pause(time)
plt.savefig("test.png")		# save image
plt.show()					# show image
plt.close()
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Tres_Lu

您的鼓励将是我创作最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值