如何解析和保存 Multipart/related type=image/jpeg 响应? (Dicom Wado 回应)
How to parse and save a Multipart/related type=image/jpeg response? (Dicom Wado Response)
我正在向 Dicom 服务器发出 Get 请求,其中 returns 一个 Multipart/Related Type=image/jpeg。我尝试使用 aiohttp libararies Multipart 功能来解析,但它没有用。保存的文件已损坏。
这是我的代码。
import asyncio
import aiohttp
'''
async def fetch(url,session,header):
async with session.get(url,headers=header) as response:
await response
async def multiHit(urls,header):
tasks = []
async with aiohttp.ClientSession() as session:
for i,url in enumerate(urls):
tasks.append(fetch(url,session,header))
result = await asyncio.gather(*tasks)
return result
loop = asyncio.get_event_loop()
res = loop.run_until_complete(multiHit(["FRAME URL"],{"Accept":"multipart/related;type=image/jpeg"}))
print(res)
'''
async def xyz(loop):
async with aiohttp.ClientSession(loop=loop).get(url="FRAME URL",headers={"Accept":"multipart/related;type=image/jpeg"}) as response:
reader = aiohttp.MultipartReader.from_response(response)
while True:
part = await reader.next()
if part is None:
break
filedata = await part.read(decode=False)
import base64
with open('m.jpeg','wb') as outFile:
outFile.write(part.decode(filedata))
return 1
loop = asyncio.get_event_loop()
res = loop.run_until_complete(xyz(loop))
如何解析 Multipart/related 响应并保存图像?
我发现我正在正确解析多部分响应,但我不得不使用另一个库(库名称:imagecodecs,方法名称:jpegsof3_decode)将单个部分解压缩到图像中。这是给出图像的 numpy 数组。这是更新后的代码
reader = aiohttp.MultipartReader.from_response(response)
while True:
part = await reader.next()
if part is None:
break
data = await part.read()
imageDecompressed = jpegsof3_decode(data)
此外,可以使用 cv2 库将 numpy 数组转换为图像
success, encoded_image = cv2.imencode('.png',imageDecompressed)
转换后的图像的字节版本可以这样得到
imageInBytes = encoded_image.tobytes()
我正在向 Dicom 服务器发出 Get 请求,其中 returns 一个 Multipart/Related Type=image/jpeg。我尝试使用 aiohttp libararies Multipart 功能来解析,但它没有用。保存的文件已损坏。
这是我的代码。
import asyncio
import aiohttp
'''
async def fetch(url,session,header):
async with session.get(url,headers=header) as response:
await response
async def multiHit(urls,header):
tasks = []
async with aiohttp.ClientSession() as session:
for i,url in enumerate(urls):
tasks.append(fetch(url,session,header))
result = await asyncio.gather(*tasks)
return result
loop = asyncio.get_event_loop()
res = loop.run_until_complete(multiHit(["FRAME URL"],{"Accept":"multipart/related;type=image/jpeg"}))
print(res)
'''
async def xyz(loop):
async with aiohttp.ClientSession(loop=loop).get(url="FRAME URL",headers={"Accept":"multipart/related;type=image/jpeg"}) as response:
reader = aiohttp.MultipartReader.from_response(response)
while True:
part = await reader.next()
if part is None:
break
filedata = await part.read(decode=False)
import base64
with open('m.jpeg','wb') as outFile:
outFile.write(part.decode(filedata))
return 1
loop = asyncio.get_event_loop()
res = loop.run_until_complete(xyz(loop))
如何解析 Multipart/related 响应并保存图像?
我发现我正在正确解析多部分响应,但我不得不使用另一个库(库名称:imagecodecs,方法名称:jpegsof3_decode)将单个部分解压缩到图像中。这是给出图像的 numpy 数组。这是更新后的代码
reader = aiohttp.MultipartReader.from_response(response)
while True:
part = await reader.next()
if part is None:
break
data = await part.read()
imageDecompressed = jpegsof3_decode(data)
此外,可以使用 cv2 库将 numpy 数组转换为图像
success, encoded_image = cv2.imencode('.png',imageDecompressed)
转换后的图像的字节版本可以这样得到
imageInBytes = encoded_image.tobytes()