Я хочу извлечь каждое значение RGB из возможных фотографий в Haskell.Как извлечь RGB-значения из (большинства) изображений?
Что было бы самым простым способом получить исходные значения (0-255)?
Я уже получил некоторые результаты с библиотекой Juicy пикселей, но почему-то я всегда получаю исключение:
*** Exception: ./Data/Vector/Generic.hs:249 ((!)): index out of bounds (660000,660000)
Это соответствующий код.
{-# LANGUAGE TypeSynonymInstances #-}
module Main where
import Codec.Picture (readImage, pixelAt, PixelRGB8(..))
import Codec.Picture.Types
import System.FilePath.Posix (splitExtension)
toRGBRaw :: FilePath -> IO()
toRGBRaw fp = do
image <- readImage fp
case image of
Left _ -> putStrLn $ "Sorry, not a supported codec for " ++ fp
Right dynimg -> do
let imgrgba8 = fromDynamicImage dynimg
let (name, _) = splitExtension fp
writeFile (name ++ ".txt") (concat $ accumPixels imgrgba8)
accumPixels :: Image PixelRGBA8 -> [String]
accumPixels [email protected](Image w h _) = [ format (pixelAt img x y) x y | x <- [0..w], y <- [0..h]]
where format (PixelRGBA8 r g b _) j k = "#" ++ show r ++ "$"
++ show g ++ "$"
++ show b ++ "$"
++ show j ++ "$"
++ show k ++ "*\n"
-- Copied from
-- See http://hackage.haskell.org/package/JuicyPixels-util-0.2/docs/Codec-Picture-RGBA8.html
class ToPixelRGBA8 a where
toRGBA8 :: a -> PixelRGBA8
instance ToPixelRGBA8 Pixel8 where
toRGBA8 b = PixelRGBA8 b b b 255
instance ToPixelRGBA8 PixelYA8 where
toRGBA8 (PixelYA8 l a) = PixelRGBA8 l l l a
instance ToPixelRGBA8 PixelRGB8 where
toRGBA8 (PixelRGB8 r g b) = PixelRGBA8 r g b 255
instance ToPixelRGBA8 PixelRGBA8 where
toRGBA8 = id
fromDynamicImage :: DynamicImage -> Image PixelRGBA8
fromDynamicImage (ImageY8 img) = pixelMap toRGBA8 img
fromDynamicImage (ImageYA8 img) = pixelMap toRGBA8 img
fromDynamicImage (ImageRGB8 img) = pixelMap toRGBA8 img
fromDynamicImage (ImageRGBA8 img) = img
-- end of Codec.Picture.RGBA8
Это пример изображения: Link
Код в настоящее время немного неупорядоченный, потому что я должен был скопировать некоторые определения из Codec.Picture.RGBA8
, который не доступен через LTS. Не обращайте внимания на строковое представление, я разбираю их через Arduino с экраном WiFi.
Ну, это неловко. Вы совершенно правы! Я приму свой ответ, как только смогу. (Возможно, по крайней мере, кто-то может использовать этот код в будущем) – Cirquit
[pixelFold] (https://hackage.haskell.org/package/JuicyPixels-3.2.5.3/docs/Codec-Picture-Types.html#v:pixelFold) вспомогательная функция позволит вам полностью исключить индексацию здесь. – jhickner