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

  1. Academic
  2. 21 view

[Haskell]Make Inverse Image

Target

I’ve been able to read and output PGM format image.
So, I’ll make image processing program today.
At first is reverse image.

Program

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-
        pixels_handle <- hGetContents infile
        let src_contents = take (length pixels_handle) $ pixels_handle
        let src_contents_lines = lines src_contents
        let src_pixels = concat (map (\x -> words x) src_contents_lines)
        let int_pixels = map (\x -> read x::Int) src_pixels
        let dist_pixels = map (\x -> 255 - x) int_pixels

        -- Output PGM File
        fh <- openFile (args !! 1) WriteMode
        hPutStrLn fh pgm_type
        hPutStrLn fh pgm_comment
        hPutStrLn fh pgm_max_brightness
        hPutStrLn fh pgm_size
        hPutStr fh ((foldr (\x xs -> show(x) ++ " " ++ xs ) "") dist_pixels)

        hClose infile
        hClose fh

Result

Input

car

Output

car-out

It works.

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