File size: 904 Bytes
55c9f00
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
from PIL import Image
import os,cv2
import numpy as np
def listImages(d):
    images = []
    for f in os.scandir(d):
        if f.is_file() and f.name.split(".")[-1].lower() in (
            "jpg",
            "jpeg",  # 添加 "jpeg" 格式
            "png",
            "bmp",
            "svg",
            "webp",
        ):
            images.append(f.path)
    return images

def ImageToCV(img):
    return cv2.cvtColor(np.asarray(img), cv2.COLOR_RGB2BGR)

def ImageFromBytes (content):
    return Image.open(BytesIO(content))  

def ImageToBytes (img,format="JPEG"):
    return img.save(BytesIO(), format=format).getvalue()

def CVtoImage(img):
    return Image.fromarray(cv2.cvtColor(img,cv2.COLOR_BGR2RGB))

def CVfromBytes(img_bytes):
    return cv2.imdecode(np.frombuffer(img_bytes, dtype=np.uint8)  , 1) 

def CVtoBytes (img,format=".jpg"):
    return cv2.imencode(format,img)[1].tobytes()