プログラミング + アカデミック + 何か面白いこと

  1. Academic
  2. 13 view

[Haskell] Make PGM File part2

I’ll do yesterday’s thing.
It’s making PGM file with Haskell.

Today is making random list.

import System.IO
import System.Random
import Control.Monad (replicateM)

main = do
    -- PGM Header
    let pgm_type = "P5\n"
    let pgm_comment = "# PGM type grayscale image\n"
    let pgm_size = (255,255)
    let pgm_max_brightness = 255

    let pgm_size_string = (show (fst pgm_size)) ++ " " ++ (show (snd pgm_size)) ++ "\n"
    let pgm_max_brightness_string = (show pgm_max_brightness) ++ "\n"
    let pgm_header = pgm_type ++ pgm_comment ++ pgm_size_string ++ pgm_max_brightness_string
    let pgm_filename = "test.pgm"

    fh <- openFile pgm_filename WriteMode
    hPutStr fh pgm_header

    -- Main
    pixels <- replicateM 255 $ (getStdRandom $ randomR (0,255) :: IO Int)
    hPrint fh pixels
    hClose fh

Result is like this.

$ghc pgm.hs
$./pgm
$cat test.pgm
P5
# PGM type grayscale image
255 255
255
[167,222,176,132,221,175,81,7,160,123,62,170,122,60,83,148,210,160,61,88,86,192,124,51,56,167,247,234,191,210,146,149,95,37,150,26,174,238,24,165,3,41,226,115,191,62,201,146,230,19,245,148,183,72,45,33,229,240,172,236,89,182,230,65,228,53,56,54,66,75,247,234,224,27,110,31,22,40,133,220,211,243,133,97,179,129,51,183,216,106,188,23,217,99,56,239,143,1,2,31,5,85,234,133,3,106,85,137,75,205,183,141,174,55,58,56,24,163,70,133,78,124,12,84,98,23,230,181,36,232,235,65,182,71,244,53,207,251,77,166,41,62,48,168,112,70,64,206,208,242,213,1,61,177,220,152,41,82,247,16,21,85,209,88,82,23,140,28,179,243,7,254,201,183,194,115,251,130,110,69,35,179,9,58,201,91,145,107,191,72,51,217,135,112,47,173,55,41,246,176,61,86,255,3,237,25,185,161,177,20,33,113,174,14,123,189,82,106,157,149,111,60,236,199,30,75,90,148,146,0,173,99,24,200,108,103,62,93,137,172,158,127,11,56,219,144,185,25,75,75,119,36,47,104,152]

The remaining thing is formatting output.

Academic recent post

  1. [Just Touch]Microsoft Quantum Development Kit…

  2. Visualization of Neural Network and its Train…

  3. [Machine Learning]Created docker image includ…

  4. [OpenCV][Ruby]Auto check web page design corr…

  5. [MachineLearning]How to introduce to real pro…

関連記事

PAGE TOP