本文主要是介绍Java读取图片时抛Unable to render RenderedOp for this operation,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
后台http接口读取图片的代码如下,使用的是JAI方式:
byte[] buf = IOUtils.toByteArray(req.getInputStream());PlanarImage image = null;
ByteArraySeekableStream stream = new ByteArraySeekableStream(buf);
image = JAI.create("stream", stream);
然后在image.getWidth()的时候抛出了如下异常:
Error: IOException occurs when search for propriate codecs.
Occurs in: com.sun.media.jai.codec.ImageCodec
java.io.EOFExceptionat com.sun.media.jai.codec.SeekableStream.readFully(SeekableStream.java:320)at com.sun.media.jai.codec.SeekableStream.readFully(SeekableStream.java:297)at com.sun.media.jai.codec.ImageCodec.getDecoderNames(ImageCodec.java:267)at com.sun.media.jai.opimage.StreamRIF.create(StreamRIF.java:72)at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)at java.lang.reflect.Method.invoke(Method.java:606)at javax.media.jai.FactoryCache.invoke(FactoryCache.java:122)at javax.media.jai.OperationRegistry.invokeFactory(OperationRegistry.java:1674)at javax.media.jai.ThreadSafeOperationRegistry.invokeFactory(ThreadSafeOperationRegistry.java:473)at javax.media.jai.registry.RIFRegistry.create(RIFRegistry.java:332)at javax.media.jai.RenderedOp.createInstance(RenderedOp.java:819)at javax.media.jai.RenderedOp.createRendering(RenderedOp.java:867)at javax.media.jai.RenderedOp.getWidth(RenderedOp.java:2179)at com.soso.zhishi.proc.RatioCropProc.proc(RatioCropProc.java:32)at com.soso.zhishi.ImageFactory.ratioCropAndEncode(ImageFactory.java:117)at com.soso.zhishi.servlet.UploadFixedPicServlet.doPost(UploadFixedPicServlet.java:89)at javax.servlet.http.HttpServlet.service(HttpServlet.java:650)at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:169)at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:956)at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:436)at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1078)at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:625)at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1749)at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1708)at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)at java.lang.Thread.run(Thread.java:745)
java.lang.RuntimeException: - Unable to render RenderedOp for this operation.at javax.media.jai.RenderedOp.createInstance(RenderedOp.java:827)at javax.media.jai.RenderedOp.createRendering(RenderedOp.java:867)at javax.media.jai.RenderedOp.getWidth(RenderedOp.java:2179)at com.soso.zhishi.proc.RatioCropProc.proc(RatioCropProc.java:32)at com.soso.zhishi.ImageFactory.ratioCropAndEncode(ImageFactory.java:117)at com.soso.zhishi.servlet.UploadFixedPicServlet.doPost(UploadFixedPicServlet.java:89)at javax.servlet.http.HttpServlet.service(HttpServlet.java:650)at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:169)at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:956)at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:436)at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1078)at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:625)at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1749)at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1708)at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)at java.lang.Thread.run(Thread.java:745)
其实详细的异常信息应该是如下:
Error: Cannot decode the image for the type :
Occurs in: com.sun.media.jai.opimage.CodecRIFUtil
java.io.IOException: Premature end of input fileat com.sun.media.jai.codecimpl.CodecUtils.toIOException(CodecUtils.java:76)at com.sun.media.jai.codecimpl.JPEGImageDecoder.decodeAsRenderedImage(JPEGImageDecoder.java:48)at com.sun.media.jai.opimage.CodecRIFUtil.create(CodecRIFUtil.java:88)at com.sun.media.jai.opimage.JPEGRIF.create(JPEGRIF.java:43)at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)at java.lang.reflect.Method.invoke(Method.java:497)at javax.media.jai.FactoryCache.invoke(FactoryCache.java:122)at javax.media.jai.OperationRegistry.invokeFactory(OperationRegistry.java:1674)at javax.media.jai.ThreadSafeOperationRegistry.invokeFactory(ThreadSafeOperationRegistry.java:473)at javax.media.jai.registry.RIFRegistry.create(RIFRegistry.java:332)at com.sun.media.jai.opimage.StreamRIF.create(StreamRIF.java:102)at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)at java.lang.reflect.Method.invoke(Method.java:497)at javax.media.jai.FactoryCache.invoke(FactoryCache.java:122)at javax.media.jai.OperationRegistry.invokeFactory(OperationRegistry.java:1674)at javax.media.jai.ThreadSafeOperationRegistry.invokeFactory(ThreadSafeOperationRegistry.java:473)at javax.media.jai.registry.RIFRegistry.create(RIFRegistry.java:332)at javax.media.jai.RenderedOp.createInstance(RenderedOp.java:819)at javax.media.jai.RenderedOp.createRendering(RenderedOp.java:867)at javax.media.jai.RenderedOp.getHeight(RenderedOp.java:2188)at TestMongodbImage.testImage(TestMongodbImage.java:99)at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)at java.lang.reflect.Method.invoke(Method.java:497)at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:252)at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94)at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)at org.junit.runners.ParentRunner.run(ParentRunner.java:363)at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191)at org.junit.runner.JUnitCore.run(JUnitCore.java:137)at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:51)at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:237)at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)at java.lang.reflect.Method.invoke(Method.java:497)at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
Caused by: com.sun.image.codec.jpeg.TruncatedFileException: Premature end of input fileat sun.awt.image.codec.JPEGImageDecoderImpl.readJPEGStream(Native Method)at sun.awt.image.codec.JPEGImageDecoderImpl.decodeAsBufferedImage(JPEGImageDecoderImpl.java:213)at com.sun.media.jai.codecimpl.JPEGImage.<init>(JPEGImageDecoder.java:110)at com.sun.media.jai.codecimpl.JPEGImageDecoder.decodeAsRenderedImage(JPEGImageDecoder.java:46)... 56 more
Error: IOException occurs when decode the image.
Occurs in: com.sun.media.jai.opimage.StreamRIF
java.io.IOExceptionat com.sun.media.jai.codecimpl.CodecUtils.toIOException(CodecUtils.java:76)at com.sun.media.jai.codecimpl.JPEGImageDecoder.decodeAsRenderedImage(JPEGImageDecoder.java:48)at com.sun.media.jai.codec.ImageDecoderImpl.decodeAsRenderedImage(ImageDecoderImpl.java:140)at com.sun.media.jai.opimage.StreamRIF.create(StreamRIF.java:135)at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)at java.lang.reflect.Method.invoke(Method.java:497)at javax.media.jai.FactoryCache.invoke(FactoryCache.java:122)at javax.media.jai.OperationRegistry.invokeFactory(OperationRegistry.java:1674)at javax.media.jai.ThreadSafeOperationRegistry.invokeFactory(ThreadSafeOperationRegistry.java:473)at javax.media.jai.registry.RIFRegistry.create(RIFRegistry.java:332)at javax.media.jai.RenderedOp.createInstance(RenderedOp.java:819)at javax.media.jai.RenderedOp.createRendering(RenderedOp.java:867)at javax.media.jai.RenderedOp.getHeight(RenderedOp.java:2188)at TestMongodbImage.testImage(TestMongodbImage.java:99)at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)at java.lang.reflect.Method.invoke(Method.java:497)at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:252)at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94)at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)at org.junit.runners.ParentRunner.run(ParentRunner.java:363)at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191)at org.junit.runner.JUnitCore.run(JUnitCore.java:137)at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:51)at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:237)at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)at java.lang.reflect.Method.invoke(Method.java:497)at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)Caused by: com.sun.media.jai.codecimpl.util.ImagingExceptionat com.sun.media.jai.codecimpl.ImagingListenerProxy.errorOccurred(ImagingListenerProxy.java:63)at com.sun.media.jai.codecimpl.JPEGImage.sendExceptionToListener(JPEGImageDecoder.java:172)at com.sun.media.jai.codecimpl.JPEGImage.<init>(JPEGImageDecoder.java:113)at com.sun.media.jai.codecimpl.JPEGImageDecoder.decodeAsRenderedImage(JPEGImageDecoder.java:46)... 47 more
Caused by: com.sun.media.jai.codecimpl.util.ImagingException: Unable to process image stream, incorrect format.... 50 more
Caused by: com.sun.image.codec.jpeg.ImageFormatException: Not a JPEG file: starts with 0xff 0xd9at sun.awt.image.codec.JPEGImageDecoderImpl.readJPEGStream(Native Method)at sun.awt.image.codec.JPEGImageDecoderImpl.decodeAsBufferedImage(JPEGImageDecoderImpl.java:213)at com.sun.media.jai.codecimpl.JPEGImage.<init>(JPEGImageDecoder.java:110)... 48 morejava.lang.RuntimeException: - Unable to render RenderedOp for this operation.at javax.media.jai.RenderedOp.createInstance(RenderedOp.java:827)at javax.media.jai.RenderedOp.createRendering(RenderedOp.java:867)at javax.media.jai.RenderedOp.getHeight(RenderedOp.java:2188)at TestMongodbImage.testImage(TestMongodbImage.java:99)at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)at java.lang.reflect.Method.invoke(Method.java:497)at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:252)at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94)at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)at org.junit.runners.ParentRunner.run(ParentRunner.java:363)at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191)at org.junit.runner.JUnitCore.run(JUnitCore.java:137)at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:51)at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:237)at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)at java.lang.reflect.Method.invoke(Method.java:497)at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)INFO 2017-07-17 15:40:08 org.springframework.context.support.GenericApplicationContext Closing org.springframework.context.support.GenericApplicationContext@18d30f7: startup date [Mon Jul 17 15:40:07 CST 2017]; root of context hierarchy
Corrupt JPEG data: premature end of data segment
比较关键的一句话是:
Not a JPEG file: starts with 0xff 0xd9
看起来服务接口读取图片的时候认为图片格式不是标准JPEG格式的,所以我在调用上述接口上传图片的之前,先试图对图片进行格式转换,我的上传代码是python的,所以先用了如下代码
from PIL import Image
from PIL import ImageFile
ImageFile.LOAD_TRUNCATED_IMAGES = True
im = Image.open(waterImage)
im.save(waterImage)
一开始这样做,有部分图片可以成功上传,但是后来发现仍然有很大一部分图片上传失败
发现失败的图片mode类型有CMYK和RGBA以及RGB
于是,我试图将这三种格式都转换成RGB格式
我在网上查了很多资料试图对格式进行转换,最终发现一种可行的转换算法如下,需要对不同的模式采用不同的转换算法:
def parseImgMode(source, dest): #source是原图地址,dest是目标图地址sourceimg = Image.open(source)print sourceimg.modesize = sourceimg.sizewidth = size[0]height = size[1]destimg = Image.new('RGB', size, (255,255,255))if sourceimg.mode == 'CMYK':for i in range(width):for j in range(height):px = sourceimg.getpixel((i,j))c = px[0] / 255.0m = px[1] / 255.0y = px[2] / 255.0k = px[3] / 255.0nc = (c*(1-k)+k)nm = (m*(1-k)+k)ny = (y*(1-k)+k)r = (int)((1-nc)*255)g = (int)((1-nm)*255)b = (int)((1-ny)*255)destimg.putpixel((i,j), (r,g,b))destimg.save(dest, 'jpeg', quality=100)return destelif sourceimg.mode == 'RGBA':for i in range(width):for j in range(height):px = sourceimg.getpixel((i,j))r = px[0] / 255.0g = px[1] / 255.0b = px[2] / 255.0a = px[3] / 255.0bgcolor = 1r = (int)((bgcolor*(1-a) + r*a)*255)g = (int)((bgcolor*(1-a) + g*a)*255)b = (int)((bgcolor*(1-a) + b*a)*255)destimg.putpixel((i,j), (r,g,b))destimg.save(dest, 'jpeg', quality=100)return destelif sourceimg.mode == 'RGB':img = cv2.imread(source, cv2.IMREAD_COLOR)cv2.imwrite(dest, img)return dest
一开始网上找的CMYK转RGB格式的算法虽然转换后都能上传,但是发现颜色会失真,最终发现上面这种转换算法不会丢失颜色信息
RGBA转RGB的算法上述方式可行,而且下面的代码经测试也可以实现格式转换:
sourceimg = Image.open(source)
destimg = Image.new('RGB', sourceimg.size, (255,255,255))
destimg.paste(sourceimg)
destimg.save(dest, 'jpeg', quality=100)
return dest
但是原图本身就是RGB的情况下,服务接口仍然不识别,但是我用了下面这个地址将图片转换后,上传就成功了:
http://www.jinaconvert.com/cn/convert-to-jpg.php
转换后的图片也是RGB,为什么老的RGB不行,转换后的RGB就可以呢?而且奇怪的是,我对老的图片getpixel得到的值全都是(0,0,0)
这个我就搞不明白为什么了,难道要将RGB转成RGB?所以查了很多资料,尝试了很多图片处理的模块,最终发现opencv可以正确处理
代码如下:
img = cv2.imread(source, cv2.IMREAD_COLOR)
cv2.imwrite(dest, img)
return dest
cv2.IMREAD_COLOR = 1
表示读取的时候要处理颜色,如果这个参数给0,那么处理后的图片没有颜色,这里还可以设置其他的模式
后来我发现,这段代码不仅可以处理mode=RGB情况,其他的都能处理,所以我索性没有区分模式,所有的上传失败的都用如下代码统一处理一次再上传:
img = cv2.imread(source, cv2.IMREAD_COLOR)
cv2.imwrite(dest, img)
return dest
这篇关于Java读取图片时抛Unable to render RenderedOp for this operation的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!