以下是我的应用程序中事件的流程:
1. 应用程序:获取图像 2. 应用程序:将其转换为base64并发送到服务器 3. 服务器:将base64转换为图像以使用 4. 服务器:将图像转换回base64字符串 5. 服务器:将base64字符串发送到应用程序 6. 应用程序:将base64字符串转换为UIImage 服务器上测试图像的RGB版本 客户端的测试图像的BGR版本 在这一点上,显示的UIImage是以BGR格式显示的。我最好的猜测是,在第4步出了问题,因为在那之前,图像是以RGB格式(我已经将其写入文件并进行了检查)的。我已经添加了第4步的代码供参考。我正在积极寻找在客户端更改UIImage颜色空间的方法,但我不反对在服务器端解决这个问题。任何一种解决方案都可以。
第2步:将UIImage转换为base64字符串
let imageData: Data = UIImageJPEGRepresentation(map.image,0.95)!
let base64EnCodedStr: String = imageData.base64EncodedString()
第三步:将base64字符串转换为PIL图像。
import io
import cv2
import base64
import numpy as np
from PIL import Image
# Take in base64 string and return PIL image
def stringToImage(base64_string):
imgdata = base64.b64decode(base64_string)
return Image.open(io.BytesIO(imgdata))
第四步:将图像(numpy数组)转换回Base64字符串
# Convert a numpyArray into a base64 string in JPEG format
def imageToString(npArray):
# convert array to PIL Image
newImage = Image.fromarray(npArray.astype('uint8'), 'RGB')
# convert to JPEG format
file = io.BytesIO()
newImage.save(file, format="JPEG")
# reset file pointer to start
file.seek(0)
img_bytes = file.read()
# encode data
encodedData = base64.b64encode(img_bytes)
return encodedData.decode('ascii')
编辑:
正如之前提到的,我可以在两个位置进行转换:服务器端或客户端。感谢对这个问题的回答,我能够找到这两种情况的解决方案。
解决方案1:服务器端
参考第4步中的代码,将该函数的第一行更改为以下内容:
# convert array to PIL Image
newImage = Image.fromarray( npArray[...,[2,1,0]] ) # swap color channels which converts BGR -> RGB
解决方案2:客户端
参考@dfd的解决方案。它写得很好,而且非常有效。这是我在我的应用程序中测试过的稍微改编版本(使用swift 4)。
let data = NSData(base64Encoded: base64String, options: .ignoreUnknownCharacters)
let uiInput = UIImage(data: data! as Data)
let ciInput = CIImage(image: uiInput!)
let ctx = CIContext(options: nil)
let swapKernel = CIColorKernel( string:
"kernel vec4 swapRedAndGreenAmount(__sample s) {" +
"return s.bgra;" +
"}"
)
let ciOutput = swapKernel?.apply(withExtent: (ciInput?.extent)!, arguments: [ciInput as Any])
let cgImage = ctx.createCGImage(ciOutput!, from: (ciInput?.extent)!)
let rgbOutput = UIImage(cgImage: cgImage!)