import cv2 import numpy as np import base64 from tkinter import * from io import BytesIO # 數組轉base64 def numpy_to_base64(image_np): data = cv2.imencode('.jpg', image_np)[1] image_bytes = data.tobytes() image_base4 = base64.b64encode(image_bytes).decode('utf8') return image_base4 # 數組轉bytes def numpy_to_bytes(image_np): data = cv2.imencode('.jpg', image_np)[1] image_bytes = data.tobytes() return image_bytes # 數組保存 def numpy_to_file(image_np): filename = '你的文件名_numpy.jpg' cv2.imwrite(filename,image_np) return filename # bytes轉數組 def bytes_to_numpy(image_bytes): image_np = np.frombuffer(image_bytes, dtype=np.uint8) image_np2 = cv2.imdecode(image_np, cv2.IMREAD_COLOR) return image_np2 # bytes 轉 base64 def bytes_to_base64(image_bytes): image_base4 = base64.b64encode(image_bytes).decode('utf8') return image_base4 # bytes 保存 def bytes_to_file(image_bytes): filename = '你的文件名_bytes.jpg' with open(filename,'wb') as f: f.write(image_bytes) return filename # 文件 轉 數組 def file_to_numpy(path_file): image_np = cv2.imread(path_file) return image_np # 文件轉 字節 def file_to_bytes(path_file): with open(path_file,'rb') as f: image_bytes = f.read() return image_bytes # 文件轉base64 def file_to_base64(path_file): with open(path_file,'rb') as f: image_bytes = f.read() image_base64 = base64.b64encode(image_bytes).decode('utf8') return image_base64 # base64 轉 bytes def base64_to_bytes(image_base64): image_bytes = base64.b64decode(image_base64) return image_bytes # base64轉數組 def base64_to_numpy(image_base64): image_bytes = base64.b64decode(image_base64) image_np = np.frombuffer(image_bytes, dtype=np.uint8) image_np2 = cv2.imdecode(image_np, cv2.IMREAD_COLOR) return image_np2 # base64 保存 def base64_to_file(image_base64): filename = '你的文件名_base64.jpg' image_bytes = base64.b64decode(image_base64) with open(filename, 'wb') as f: f.write(image_bytes) return filename # base64圖像轉cv2的BGR圖片(PIL為RGB圖片) def base64Toimg(self,imgstr): # image = io.BytesIO(imgstr) base64_data = re.sub('^data:image/.+;base64,', '', imgstr) image = base64.b64decode(base64_data) image_data = BytesIO(image) img = Image.open(image_data) img = cv2.cvtColor(np.array(img), cv2.COLOR_BGR2RGB) return img