簡體   English   中英

使用 imagemagick 將 JPG 圖像從灰度圖像轉換為 RGB

[英]Convert JPG image from grayscale to RGB using imagemagick

我正在嘗試使用imagemagick 命令行工具將灰度圖像轉換為 RGB。

它適用於 PNG 圖像,使用:

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

(摘自“如何使用圖像魔法將灰度png圖像從命令行轉換為RGB”的答案)

盡管使用identify -format %r result.png檢查仍會返回DirectClass Gray ,但我可以看到它使用gdalinfo因為現在列出了3 個波段/頻道:

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選項僅適用於 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

PNG顏色類型不適用於JPEG圖像,因此您不能使用相同的技術。 嘗試將色彩空間強制轉換為sRGB,和/或將類型設置為TrueColour,這樣就不會得到一個palettised圖像:

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

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。)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM