Spaces:
Running
Running
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() | |