Skip to content

Instantly share code, notes, and snippets.

@aristotle9
Created July 1, 2012 15:31
Show Gist options
  • Save aristotle9/3028754 to your computer and use it in GitHub Desktop.
Save aristotle9/3028754 to your computer and use it in GitHub Desktop.
Trans a txt file to png picture
import Graphics.GD
import System.IO
import Data.Char
import System.Environment
charToColor::Char -> Color
charToColor ch = rgb c c c
where c = ord ch
orderToPosition (width, height) pos = (x, y)
where (y, x) = pos `divMod` width
setPixels::Size -> String -> Image -> IO ()
setPixels (width,height) contents img = mapM_ (\(point, color, img) -> setPixel point color img) $ zip3 points colors imgs
where imgs = repeat img
points = map (orderToPosition (width, height)) [0..]
colors = map charToColor contents
main = do
fileName : xs <- getArgs
handle <- openFile fileName ReadMode
size <- hFileSize handle
let width = ceiling (fromIntegral size ** 0.5)
let height = width
let imgSize = (width, height)
--putStrLn $ show imgSize
img <- newImage imgSize
contents <- hGetContents handle
setPixels (width,height) contents img
hClose handle
--drawLine (0,0) imgSize 0xff0000 img
savePngFile (fileName ++ ".png") img
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment