Tech Tips

  1. Uncategorized
  2. 116 view

[Haskell]Input PGM file part2

Contents

What I want to do

Yesterday’s result can read header.
Next is reading pixel information.

Program

It’s like following.
import System.IO
import System.Environment (getArgs)

main = do
    args <- getArgs
    if length args <= 1 then do
        print "usage : ./pgm-input input.pgm output.pgm"
    else do
        infile <- openFile (head args) ReadMode

        -- Read PGM Header
        pgm_type <- hGetLine infile
        pgm_comment <- hGetLine infile
        pgm_max_brightness <- hGetLine infile
        pgm_size <- hGetLine infile
        -- Read PGM Pixels-
        src_pixels <- hGetContents infile

        -- Output PGM File
        fh <- openFile (args !! 1) WriteMode
        hPutStrLn fh pgm_type
        hPutStrLn fh pgm_comment
        hPutStrLn fh pgm_max_brightness
        hPutStrLn fh contents
        hPutStrLn fh pgm_size
        hPutStrLn fh src_pixels

        hClose infile
        hClose fh

Input

car

Output

car-out
It seems to work.

Uncategorized recent post

  1. Run Amazon FreeRTOS on M5Stack Core2 for AWS …

  2. Udacity Self-Driving Car Engineer Nanodegree …

  3. Install sbt 1.0.0 and run sample template

  4. Visualization of Neural Network and its Train…

  5. [Machine Learning]Created docker image includ…

関連記事

PAGE TOP