问题描述:

在使用img2pdf转换png到pdf时候,报了如下错误

img2pdf.AlphaChannelError: Refusing to work on images with alpha channel

img2pdf 报 img2pdf.AlphaChannelError: Refusing to work on images with alpha channel 的解决方案

 

解决方案:

 通过一番查找,发现是pngAlpha通道的问题

不少解决方案都是自己写的函数去除这个通道,但尝试了,行不通。

最后找到个工具--Pngcrush,然后完美解决了这个问题。

来到官网

https://pmt.sourceforge.io/pngcrush/

img2pdf 报 img2pdf.AlphaChannelError: Refusing to work on images with alpha channel 的解决方案

 

下载二进制压缩文件

本地解压后,上传服务器(Ubuntu)

文件目录如下,是C语言的程序,后续需要构建编译

img2pdf 报 img2pdf.AlphaChannelError: Refusing to work on images with alpha channel 的解决方案

 

 编译:

make -j8

注:-j8是指多线程执行,提高效率

img2pdf 报 img2pdf.AlphaChannelError: Refusing to work on images with alpha channel 的解决方案

 

编译后的结果:生成pngcrush文件

img2pdf 报 img2pdf.AlphaChannelError: Refusing to work on images with alpha channel 的解决方案

 

执行pngcrush:

./pngcrush 

img2pdf 报 img2pdf.AlphaChannelError: Refusing to work on images with alpha channel 的解决方案

 

 

将pngcrush设置环境变量,待后续去除png的Alpha通道用:

export PATH=$PATH:/root/wwww/selenium_screenshot/pngcrush-1.8.11/pcr010811/

img2pdf 报 img2pdf.AlphaChannelError: Refusing to work on images with alpha channel 的解决方案

 

 

去除png图片(1234.png)的Alpha通道,重命名为4321.png:

pngcrush 1234.png 4321.png

 img2pdf 报 img2pdf.AlphaChannelError: Refusing to work on images with alpha channel 的解决方案

 

 

 然后再重新调用img2pdf,对4321.png进行转换,转换成outputl.pdf文件

 img2pdf 报 img2pdf.AlphaChannelError: Refusing to work on images with alpha channel 的解决方案

 

大功告成! !

 

 参考链接:

https://blog.csdn.net/qq_43668007/article/details/87197774

https://pmt.sourceforge.io/pngcrush

 https://mlog.club/article/4090462

 

相关文章:

  • 2021-10-04
  • 2022-12-23
  • 2021-10-12
  • 2021-09-18
  • 2021-11-03
  • 2021-06-10
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-02-02
  • 2022-12-23
  • 2022-12-23
  • 2021-12-28
  • 2022-12-23
  • 2022-02-08
相关资源
相似解决方案