- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
使用 CoreImage 过滤照片,我发现保存为 JPG 文件会导致图像具有微妙但可见的蓝色色调。在这个使用黑白图像的示例中,直方图显示了颜色在保存的文件中是如何变化的。
---输入
输出 [ ] 直方图显示颜色层偏移
-- 使用 MacOS“预览”应用程序演示的问题
我可以仅使用预览应用程序显示类似的结果。
if let img = CIImage(contentsOf: url) {
let dest = procFolder.url(named: "InOut.jpg")
img.jpgWrite(url: dest)
}
extension CIImage {
func jpgWrite(url: URL) {
let prop: [NSBitmapImageRep.PropertyKey: Any] = [
.compressionFactor: 0.61
]
let bitmap = NSBitmapImageRep(ciImage: self)
let data = bitmap.representation(using: NSBitmapImageRep.FileType.jpeg, properties: prop)
do {
try data?.write(to: url, options: .atomic)
} catch {
log.error(error)
}
}
}
extension CIImage {
static let writeContext = CIContext(mtlDevice: MTLCreateSystemDefaultDevice()!, options: [
// using an extended working color space allows you to retain wide gamut information, e.g., if the input is in DisplayP3
.workingColorSpace: CGColorSpace(name: CGColorSpace.extendedSRGB)!,
.workingFormat: CIFormat.RGBAh // 16 bit color depth, needed in extended space
])
func jpgWrite(url: URL) {
// write the output in the same color space as the input; fallback to sRGB if it can't be determined
let outputColorSpace = colorSpace ?? CGColorSpace(name: CGColorSpace.sRGB)!
do {
try CIImage.writeContext.writeJPEGRepresentation(of: self, to: url, colorSpace: outputColorSpace, options: [:])
} catch {
}
}
}
最佳答案
这看起来像是一个色彩同步问题(正如 Leo 指出的那样)——更具体地说,是输入、处理和输出之间色彩空间的不匹配/误解。
当您调用 NSBitmapImageRep(ciImage:)
时,实际上发生了很多事情。系统实际需要渲染CIImage
您提供的是获取结果的位图数据。它通过创建 CIContext
来实现。使用默认(特定于设备)设置,使用它来处理您的图像(应用所有过滤器和转换),然后为您提供结果的原始位图数据。在此过程中,使用此 API 时会发生您无法控制的多种色彩空间转换(并且似乎不会导致您想要的结果)。我不喜欢这些用于渲染的“方便”API CIImage
出于这个原因,我看到很多关于 SO 的问题与他们相关。
我建议您改用 CIContext
呈现您的CIImage
成 JPEG 文件。这使您可以直接控制色彩空间等:
let input = CIImage(contentsOf: url)
// ideally you create this context once and re-use it because it's an expensive object
let context = CIContext(mtlDevice: MTLCreateSystemDefaultDevice()!, options: [
// using an extended working color space allows you to retain wide gamut information, e.g., if the input is in DisplayP3
.workingColorSpace: CGColorSpace(name: CGColorSpace.extendedSRGB)!,
.workingFormat: CIFormat.RGBAh // 16 bit color depth, needed in extended space
])
// write the output in the same color space as the input; fallback to sRGB if it can't be determined
let outputColorSpace = input.colorSpace ?? CGColorSpace(name: CGColorSpace.sRGB)!
context.writeJPEGRepresentation(of: input, to: dest, colorSpace: outputColorSpace, options: [kCGImageDestinationLossyCompressionQuality: 0.61])
关于swift - CoreImage : CIImage write JPG is shifting colors [macOS],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59330149/
我正在尝试创建一个将过滤器应用于视频文件的应用程序。我想使用核心图像过滤器 imageBuffer obtained from AVAssetReaderInput int heigh
我正在修改找到的代码 here .在代码中,我们使用 AVCaptureSession 从手机摄像头捕获视频,并使用 CIDetector 检测图像馈送中的矩形。提要有一个 640x842 的图像(纵
有什么方法可以将 CIImage 转换为 NSData 而无需将 CIImage 转换为 UIImage 或 CGImage? 我正在尝试制作一个相机应用程序。抓图时,我用CIImage处理图片,给图
我想从文件(文件系统上的 jpeg)旋转一个 UIImage 计算的弧度,但我想围绕图像中的一个点旋转它,并保持图像的原始大小(在图像数据不再存在的图像中有透明间隙,以及已移动到原始帧之外的裁剪图像数
因此,Apple 的文档说 CIImage 符合 Equatable。我认为这意味着以下单元测试将通过。然而,事实并非如此。我很想知道为什么。 func test_CIImageEqualitySho
我在 Playground 上有以下代码: import UIKit var str = "Hello, playground" func rescaledImage(_ image: UIImage
CIColorMap CIFilter 将 CIImage 作为输入。使用另一个 CIFilter(例如 CILinearGradient)动态生成该输入图像是有意义的,但是向颜色图过滤器提供过滤器的
我正在尝试水平翻转我的 CIImage : image = [image imageByApplyingTransform:CGAffineTransformMakeScale(1, -1)]; im
如何将两个或多个 CIImage 组合成另一个。我尝试使用 ciContext.drawImage。我如何从中获取 CGImage?我所理解的是,我正在写一个我应该能够从中获得图像的上下文。如果我理解
我正在使用 CIImage,虽然我知道它不是线性图像,但它确实包含一些数据。 我的问题是 CIImage 的范围属性是否返回像素或点?根据documentation ,说的很少,是工作空间坐标。这是否
我有一个接受 UIImage 的类,用它初始化一个 CIImage,如下所示: workingImage = CIImage.init(image: baseImage!) 然后使用图像以 3x3 的
如何在 CIAffineTransform 之后使我的 CIImage 有限,以便我可以将其传递给 NSBitmapImageRep:initWithCIImage? 总的来说,我想通过 CIAddi
我使用一些CIFilter来进行一些图像处理。我发现 UIImageView 太慢,无法实时显示输出图像。我关注了documentation并构建了GLKView。现在性能很完美,但旋转时图像呈现为黑
我有一个 258x258 的 CIImage。我应用过滤器 CIAreaAverage到它,以获得该图像的平均颜色的绿色值。 我正在使用此功能来获得... // channel 0,1,2 = red
我正在尝试裁剪图像并通过夹紧从边缘延伸颜色,但之后返回一个奇怪的图像大小: let image = UIImage(named: "frame_1") var ciimage = CIImage(im
核心图像不起作用: let fileUrl = Bundle.main.url(forResource: "image", withExtension: "png") let bega
我有一个 UIImageView 我想向我的 UIImageView 添加一个 CIImage这是我的代码 @IBOutlet weak var imgimgurl: UIImageView! img
我是 swift 新手,正在尝试使用 React Native 的 CoreML 功能。 我有一个 URL 为字符串的源文件。我使用 let imageURL = URL(fileURLWithPat
我正在使用一个 CIImage,我需要通过每个像素减去 128 然后除以 128 来对其进行标准化。我怎样才能用CIFilter来做到这一点 最佳答案 不确定这是否有效,但也许您可以应用两个具有静态颜
我正在对来自设备相机的图像运行多个 CoreImage 滤镜,但在将结果保存到相机胶卷时遇到了意想不到的麻烦。我的图像操作的结果是 CIImage,我可以保存到 ALAssetsLibrary 中的数
我是一名优秀的程序员,十分优秀!