pyqt5中使用GraphicsView显示图片
def show_selected_image(self, image):
height = image.shape[0]
width = image.shape[1]
ratio = float(height / width)
new_height = 300
new_width = int(300 / ratio)
img = cv2.resize(image, (new_width, new_height))
frame = QImage(img, new_width, new_height, QImage.Format_RGB888)
pix = QPixmap.fromImage(frame)
self.item = QGraphicsPixmapItem(pix)
self.scene = QGraphicsScene() # 创建场景
self.scene.addItem(self.item)
self.graphicsView.setScene(self.scene)
Python 图片转数组,二进制互转操作
https://www.jb51.net/article/207138.htm
import matplotlib.pyplot as plt
import cv2
from PIL import Image
from io import BytesIO
import numpy as np
# 图片转numpy数组
img_path = "images/1.jpg"
img_data = cv2.imread(img_path)
#numpy数组转图片
img_data = np.linspace(0,255,100*100*3).reshape(100,100,-1).astype(np.uint8)
cv2.imwrite("img.jpg",img_data) # 在当前目录下会生成一张img.jpg的图片
# 以 二进制方式 进行图片读取
with open("img.jpg","rb") as f:
img_bin = f.read() # 内容读取
# 将 图片的二进制内容 转成 真实图片
with open("img.jpg","wb") as f:
f.write(img_bin) # img_bin里面保存着 以二进制方式读取的图片内容,当前目录会生成一张img.jpg的图片
"""
以上两种方式"合作"也可以实现,但是中间会有对外存的读写
一般这些到磁盘的IO操作还是很耗时间的
所以在内存直接处理会较好
"""
#将数组转成 图片的二进制数据
img_data = np.linspace(0,255,100*100*3).reshape(100,100,-1).astype(np.uint8)
ret,buf = cv2.imencode(".jpg",img_data)
img_bin = Image.fromarray(np.uint8(buf)).tobytes()
#将图片二进制数据 转为数组
img_data = plt.imread(BytesIO(img_bin),"jpg")
print(type(img_data))
print(img_data.shape)
"""
out:
<class 'numpy.ndarray'>
(100, 100, 3)
"""
import matplotlib
import matplotlib.pyplot as plt
from matplotlib import rcParams
matplotlib.use('pgf')
pgf_config={
"font.family":"serief",
"font.size":20,
"pgf.rcfonts":False,
"text.usetex":True,
"pgf.preamble":'\n'.join