FFMPEG在将tiff图片转换为png时反转颜色

Posted

技术标签:

【中文标题】FFMPEG在将tiff图片转换为png时反转颜色【英文标题】:FFMPEG Inverts colors when converting tiff pictures to png 【发布时间】:2021-08-16 12:51:59 【问题描述】:

我们需要将数千张 tiff 图片转换为 png。为了实现这一点,我们正在使用 FFMPEG。我们最近注意到,对于极少数转换后的文件(以 png 表示),颜色是反转的:输入的 tiff 文件全是黑白的(白色背景的黑色技术绘图),而输出的 png 文件是白色和黑色(绘图变为白色,背景变为黑色)。我们当然不希望这样,我们需要颜色保持不变。

这是我们正在使用的命令:

ffmpeg -y -v warning -i in_file.tif out_file.png

我们已经在这些文件上测试了 'negate' 参数,并且图片已正确转换。但是我们不能将此参数应用于所有输入的 tiff 文件。 下面是带有 negate 参数的命令:

ffmpeg -y -v verbose -i input.tif -vf negate output.png

我们正在使用的 Java 代码:

            String ffmpegPath="/Users/user11/ffmpeg//ffmpeg";
            List<String> commandArgs = new ArrayList<String>();
            commandArgs.add(ffmpegPath);
            //overwrite output file if it exists
            commandArgs.add("-y");      
            commandArgs.add("-v");
            commandArgs.add("verbose");
            //set input file 
            commandArgs.add("-i"); 
            commandArgs.add(picture.getAbsolutePath());
            commandArgs.add(dest.getAbsolutePath());
            
            String cmd = "";
            for(String cmdArg : commandArgs) 
                cmd += cmdArg + " ";
            


tif 有错误的日志:


31, 2021 2:15:51 PM 
INFO: Executing : /Users/user11/ffmpeg/ffmpeg -y -v verbose -i /Users/user11/ffmpeg/input.tif /Users/user11/ffmpeg/output.png 
May 31, 2021 2:15:51 PM 
INFO: ReencodePicture output: ffmpeg version 4.4-tessus  https://evermeet.cx/ffmpeg/  Copyright (c) 2000-2021 the FFmpeg developers
May 31, 2021 2:15:51 PM 
INFO: ReencodePicture output:   built with Apple clang version 11.0.0 (clang-1100.0.33.17)
May 31, 2021 2:15:51 PM 
INFO: ReencodePicture output:   configuration: --cc=/usr/bin/clang --prefix=/opt/ffmpeg --extra-version=tessus --enable-avisynth --enable-fontconfig --enable-gpl --enable-libaom --enable-libass --enable-libbluray --enable-libdav1d --enable-libfreetype --enable-libgsm --enable-libmodplug --enable-libmp3lame --enable-libmysofa --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenh264 --enable-libopenjpeg --enable-libopus --enable-librubberband --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libspeex --enable-libtheora --enable-libtwolame --enable-libvidstab --enable-libvmaf --enable-libvo-amrwbenc --enable-libvorbis --enable-libvpx --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxavs --enable-libxvid --enable-libzimg --enable-libzmq --enable-libzvbi --enable-version3 --pkg-config-flags=--static --disable-ffplay
May 31, 2021 2:15:51 PM 
INFO: ReencodePicture output:   libavutil      56. 70.100 / 56. 70.100
May 31, 2021 2:15:51 PM 
INFO: ReencodePicture output:   libavcodec     58.134.100 / 58.134.100
May 31, 2021 2:15:51 PM 
INFO: ReencodePicture output:   libavformat    58. 76.100 / 58. 76.100
May 31, 2021 2:15:51 PM 
INFO: ReencodePicture output:   libavdevice    58. 13.100 / 58. 13.100
May 31, 2021 2:15:51 PM 
INFO: ReencodePicture output:   libavfilter     7.110.100 /  7.110.100
May 31, 2021 2:15:51 PM 
INFO: ReencodePicture output:   libswscale      5.  9.100 /  5.  9.100
May 31, 2021 2:15:51 PM 
INFO: ReencodePicture output:   libswresample   3.  9.100 /  3.  9.100
May 31, 2021 2:15:51 PM 
INFO: ReencodePicture output:   libpostproc    55.  9.100 / 55.  9.100
May 31, 2021 2:15:51 PM 
INFO: ReencodePicture output: [tiff_pipe @ 0x7fa7f6008200] parser not found for codec tiff, packets or times may be invalid.
May 31, 2021 2:15:51 PM 
INFO: ReencodePicture output:     Last message repeated 1 times
May 31, 2021 2:15:51 PM 
INFO: ReencodePicture output: Input #0, tiff_pipe, from '/Users/user11/ffmpeg/input.tif':
May 31, 2021 2:15:51 PM 
INFO: ReencodePicture output:   Duration: N/A, bitrate: N/A
May 31, 2021 2:15:51 PM 
INFO: ReencodePicture output:   Stream #0:0: Video: tiff, 1 reference frame, monob, 2008x2481 [SAR 1:1 DAR 2008:2481], 25 fps, 25 tbr, 25 tbn, 25 tbc
May 31, 2021 2:15:51 PM 
INFO: ReencodePicture output: Stream mapping:
May 31, 2021 2:15:51 PM 
INFO: ReencodePicture output:   Stream #0:0 -> #0:0 (tiff (native) -> png (native))
May 31, 2021 2:15:51 PM 
INFO: ReencodePicture output: Press [q] to stop, [?] for help
May 31, 2021 2:15:51 PM 
INFO: ReencodePicture output: [graph 0 input from stream 0:0 @ 0x7fa7f4c08300] w:2008 h:2481 pixfmt:monob tb:1/25 fr:25/1 sar:1/1
May 31, 2021 2:15:51 PM 
INFO: ReencodePicture output: Output #0, image2, to '/Users/user11/ffmpeg/output.png':
May 31, 2021 2:15:51 PM 
INFO: ReencodePicture output:   Metadata:
May 31, 2021 2:15:51 PM 
INFO: ReencodePicture output:     encoder         : Lavf58.76.100
May 31, 2021 2:15:51 PM 
INFO: ReencodePicture output:   Stream #0:0: Video: png, 1 reference frame, monob(progressive), 2008x2481 (0x0) [SAR 1:1 DAR 2008:2481], q=2-31, 200 kb/s, 25 fps, 25 tbn
May 31, 2021 2:15:51 PM 
INFO: ReencodePicture output:     Metadata:
May 31, 2021 2:15:51 PM 
INFO: ReencodePicture output:       encoder         : Lavc58.134.100 png
May 31, 2021 2:15:51 PM 
INFO: ReencodePicture output: frame=    1 fps=0.0 q=0.0 size=N/A time=00:00:00.00 bitrate=N/A speed=   0x    
May 31, 2021 2:15:51 PM 
INFO: ReencodePicture output: No more output streams to write to, finishing.
May 31, 2021 2:15:51 PM 
INFO: ReencodePicture output: [AVIOContext @ 0x7fa7f4c14180] Statistics: 0 seeks, 1 writeouts
May 31, 2021 2:15:51 PM 
INFO: ReencodePicture output: frame=    1 fps=0.0 q=-0.0 Lsize=N/A time=00:00:00.04 bitrate=N/A speed=1.31x    
May 31, 2021 2:15:51 PM 
INFO: ReencodePicture output: video:37kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: unknown
May 31, 2021 2:15:51 PM 
INFO: ReencodePicture output: Input file #0 (/Users/user11/ffmpeg/input.tif):
May 31, 2021 2:15:51 PM 
INFO: ReencodePicture output:   Input stream #0:0 (video): 1 packets read (18100 bytes); 1 frames decoded; 
May 31, 2021 2:15:51 PM 
INFO: ReencodePicture output:   Total: 1 packets (18100 bytes) demuxed
May 31, 2021 2:15:51 PM 
INFO: ReencodePicture output: Output file #0 (/Users/user11/ffmpeg/output.png):
May 31, 2021 2:15:51 PM 
INFO: ReencodePicture output:   Output stream #0:0 (video): 1 frames encoded; 1 packets muxed (38013 bytes); 
May 31, 2021 2:15:51 PM 
INFO: ReencodePicture output:   Total: 1 packets (38013 bytes) muxed
May 31, 2021 2:15:51 PM 
INFO: ReencodePicture output: [AVIOContext @ 0x7fa7f4f041c0] Statistics: 18100 bytes read, 0 seeks
May 31, 2021 2:15:51 PM 
INFO: BinaryRepositoryHelper.convertPixTo ffmpeg Process returned : java.lang.UNIXProcess@27ddd392, ended with code 0, and returned in 160 ms, out=/Users/user11/ffmpeg/output.png exists=true





所以我们的问题是:

    有没有办法识别将转换为带有反色的 png 的 tiff 文件?这样我们就可以知道必须将“negate”参数应用于哪些文件。 还有其他我们可以使用的 FFMPEG 参数吗?

在过去的几天里,这个问题一直让我们发疯。任何帮助将不胜感激。 谢谢。

Tif 图片示例

https://drive.google.com/file/d/1U1a91gTk1gaJb-XAPavNq6UhmjWXZjbM/view?usp=sharing

【问题讨论】:

显示第一个 ffmpeg 命令的完整日志。提供指向有问题的 TIFF 文件之一的链接。 您好 Mounia,请在代码括号中添加命令以获得更好的阅读体验。欢迎来到 SO :) 非常感谢您的干预。附加信息已添加到问题中。如果您能帮助我们检测负责将颜色从 tif 转换为 png 的参数,我们将不胜感激。 【参考方案1】:

看起来像一个损坏的文件。

ImageMagick convert 也不喜欢输入。 This comment in convert: improper image header #2717 建议使用 tiffset 设置 Photometric Interpretation 标签:

tiffset -s 262 0 input.tif

现在使用ffmpeg 进行转换应该可以正常工作。

【讨论】:

以上是关于FFMPEG在将tiff图片转换为png时反转颜色的主要内容,如果未能解决你的问题,请参考以下文章

使用 ffmpeg 从 png 文件制作 gif 时颜色混乱(扭曲)

将不同的图片格式(jpg、gif、png等)转换为TIFF格式

如何在将 eps 文件转换为 jpeg 或 png 时更改其背景颜色

使用 CMYK 和透明度将 PDF 转换为 Tiff

c#当将TIFF转换为PNG时,我如何获得与使用MS Paint相同的压缩

tiff格式如何转换成jpg格式