PIL(pillow)、matplotlib、opencv
PIL
import PIL.Image as Image
import PIL.ImageDraw as Draw
import PIL.ImageFont as ImageFont
import PIL.ImageFilter as ImageFilter
import numpy as np
font = ImageFont.truetype("C:\\Windows\\Fonts\\simfang.ttf", 40)
img1 = Image.open("1.jpg")
img1.show() # 使用系统默认工具打开
w, h = img1.size # 获取图片尺寸
print(w, h)
img2 = img1.resize((320, 180)) # 缩放
img2.show()
img3 = img2.rotate(45, expand=True) # 顺时针旋转角度
img3.show()
img3.save("2.jpg", quality=100) # 另存为
img = Draw.Draw(img2)
img.point((100, 100), fill="blue") # 点
img.rectangle((30, 30, 90, 90), outline="red") # 矩形
img.line((30, 30, 90, 90), fill="blue", width=2) # 线条
img.text((40, 100), text="ok", fill="blue", font=font) # 文本
img.chord((150, 80, 190, 120), 180, 0, fill="red", outline='black') # 弓形
img.arc((150, 80, 190, 120), 0, 180, fill="white") # 圆和弧
img.ellipse((165, 95, 175, 105), fill='white') # 椭圆
img2.show()
img4 = img1.convert("L") # 转成灰色图像
img5 = img1.convert("1") # 转成二值图像
img4.show()
img4.paste(img5, (-500, -500)) # 将参数图粘贴到调用图
img4.show()
img6 = img1.filter(ImageFilter.EDGE_ENHANCE_MORE) # 调用滤镜
img6.show()
Image = np.array(img1)
print(Image)
matplotlib
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
# 折线图
x = np.linspace(-2 * np.pi, 2 * np.pi, 100) # linspace(左,右,等分)
y = np.cos(x)
plt.title("cos")
plt.xlabel("x")
plt.ylabel("y")
plt.plot(x, y) # 散点,默认以'b-'连接
plt.scatter(x, y) # 散点,默认以'o'连接
plt.show()
# 柱状图
name_lst = ["A", "B", "C", "D"]
num_lst1 = [10, 8, 5, 6.1]
num_lst2 = [5, 7, 3, 8]
plt.bar(range(0, len(name_lst)), num_lst1, color=['r', 'g'], tick_label=name_lst)
plt.show()
# 堆叠柱状图
plt.bar(range(0, len(name_lst)), num_lst1, color="r", label='r', tick_label=name_lst)
plt.bar(range(0, len(name_lst)), num_lst2, color="g", label='g', tick_label=name_lst)
plt.legend() # 堆叠(后覆盖前)
plt.show()
# 并列柱状图
offset = list(range(0, len(name_lst)))
width = 0.4
plt.bar(offset, num_lst1, width=width, color="r", label='r', tick_label=name_lst)
for i in range(4):
offset[i] = offset[i] + width # 偏移一个宽度
plt.bar(offset, num_lst2, width=width, color="g", label='g', tick_label=name_lst)
plt.legend()
plt.show()
# 饼状图
ex = [0.1, 0, 0, 0]
plt.pie(x=num_lst1, autopct='%.2f%%', explode=ex, labels=name_lst, colors="rgby", shadow=True, startangle=30)
plt.show()
# 三维画点
x = np.random.normal(0, 10, 100)
y = np.random.normal(0, 10, 100)
z = np.random.normal(0, 10, 100)
fig = plt.figure()
ax = Axes3D(fig)
ax.scatter(x, y, z)
plt.show()
# 实时画图
x_lst = []
y_lst = []
plt.ion()
for i in range(50):
x_lst.append(i)
y_lst.append(i ** 2)
plt.clf()
plt.plot(x_lst, y_lst)
plt.pause(0.1)
plt.ioff()
opencv
import cv2
img = cv2.imread("2.jpg")
cv2.imshow("image", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
# 图片对象,起始点,终点,颜色,粗细
cv2.line(img, (10, 10), (100, 100), (255, 0, 0), thickness=2)
cv2.rectangle(img, (50, 50), (150, 150), color=(255, 0, 0))
# 图片对象,圆心,半径,颜色
cv2.circle(img, (50, 50), 50, color=(255, 0, 0))
cv2.imshow("image", img)
cv2.waitKey(0) # 按任意键继续
cv2.destroyAllWindows() # 关闭窗口
文档信息
- 本文作者:Weikang Yu
- 本文链接:https://AccelYu.github.io/2020/07/15/image/
- 版权声明:自由转载-非商用-非衍生-保持署名(创意共享3.0许可证)