【问题标题】:Convert JPG image from grayscale to RGB using imagemagick使用 imagemagick 将 JPG 图像从灰度转换为 RGB
【发布时间】:2016-04-24 19:22:20
【问题描述】:

我正在尝试使用 imagemagick command-line tools 将灰度图像转换为 RGB。

它适用于 PNG 图像,使用:

convert image.png -define png:color-type=2 result.png

(取自对"How to convert gray scale png image to RGB from comand line using image magick"的回答)

虽然使用identify -format %r result.png 进行检查仍会返回DirectClass Gray,但我可以看到它使用gdalinfo 可以正常工作,因为现在列出了3 个波段/频道: p>

gdalinfo [成功转换 PNG]:

Driver: PNG/Portable Network Graphics
Files: result.png
Size is 567, 479
Coordinate System is `'
Image Structure Metadata:
  INTERLEAVE=PIXEL
Corner Coordinates:
Upper Left  (    0.0,    0.0)
Lower Left  (    0.0,  479.0)
Upper Right (  567.0,    0.0)
Lower Right (  567.0,  479.0)
Center      (  283.5,  239.5)
Band 1 Block=567x1 Type=Byte, ColorInterp=Red
Band 2 Block=567x1 Type=Byte, ColorInterp=Green
Band 3 Block=567x1 Type=Byte, ColorInterp=Blue

但是,-define option 似乎只适用于 PNG 图像。

我的问题:我怎样才能为 JPG 图片实现同样的效果?

当我为 JPG 尝试上述命令时,它不起作用:

convert image.jpg -define jpg:color-type=2 result.jpg

gdalinfo [未成功转换 JPG]:

Driver: JPEG/JPEG JFIF
Files: result.jpg
Size is 1500, 1061
Coordinate System is `'
Metadata:
  EXIF_ColorSpace=1
  EXIF_PixelYDimension=2480
  ...
  EXIF_YCbCrPositioning=1
  EXIF_YResolution=(300)
Corner Coordinates:
Upper Left  (    0.0,    0.0)
Lower Left  (    0.0, 1061.0)
Upper Right ( 1500.0,    0.0)
Lower Right ( 1500.0, 1061.0)
Center      (  750.0,  530.5)
Band 1 Block=1500x1 Type=Byte, ColorInterp=Gray
  Overviews: 750x531, 375x266, 188x133
  Image Structure Metadata:
    COMPRESSION=JPEG

【问题讨论】:

    标签: command-line imagemagick jpeg rgb grayscale


    【解决方案1】:

    PNG 颜色类型不适用于 JPEG 图像,因此您不能使用相同的技术。尝试将色彩空间强制为 sRGB,和/或将类型设置为 TrueColor,这样您就不会得到调色板:

    convert input.jpg -colorspace sRGB -type truecolor result.jpg
    

    【讨论】:

    • 非常感谢您的快速回复。记录在案:convert input.jpg -type truecolor result.jpg 工作正常。虽然添加 -colorspace sRGB 产生了相同的结果,但单独使用它并不能完成工作。
    • 不适用于 PNG 文件,我试过:-colorspace sRGB -type truecoloralpha 我的图像是 8 位的,我不想将其转换为 PNG24。
    • @Shayan 如果您有任何问题,请按照通常的方式提出问题,并附上您的图片。
    • @Shayan 根据this post on the ImageMagick forum,您还必须使用-define colorspace:auto-grayscale=off。我想在您保存时应用灰度检测,并且在此之前进行的任何转换都不会以某种方式强制色彩空间。
    • 对于 PNG,您必须指定前缀 PNG24:filename.png 才能从 8 位灰度转换为 24 位彩色。
    【解决方案2】:

    image-magick 拒绝处理 png,但 ffmpeg 不处理。

    ffmpeg -loglevel 警告 -i ${file} -sws_flags “lanczos+accurate_rnd+full_chroma_int+full_chroma_inp+bitexact” -y -pix_fmt rgb8 test.png

    报告的深度结束文件是 8 位。 (对于 rgba → rgb 也很有用。)

    【讨论】:

      猜你喜欢
      • 2016-03-22
      • 2014-02-26
      • 2014-12-22
      • 2018-08-15
      • 1970-01-01
      • 2013-10-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多