-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpreconvert.hs
56 lines (45 loc) · 1.76 KB
/
preconvert.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
-- preconvert.hs
-- Compiles to executable.
-- We're passed the path to an image. We expect that image to be 1280 by
-- 720, and we chop that image into 144 80 by 80 pieces, saving the
-- pieces with the same file name as the original but in numbered
-- subdirectories, chopped/1, chopped/2, ...
import ImageToVector (loadImages, chop)
import System.Directory (createDirectoryIfMissing)
import System.Environment (getArgs)
import Vision.Image (RGB)
import Vision.Image.Storage.DevIL (save, PNG(..))
import Vision.Primitive.Shape((:.),Z)
import qualified Data.Vector.Storable as V
import Data.List (elemIndices, splitAt)
--defaultImg :: RGB
--defaultImg = Manifest (Z :. 1 :. 1) (V.singleton (RGBPixel 0 0 0))
getDir :: FilePath -> FilePath
getDir = fst . splitFileName
getFileName :: FilePath -> FilePath
getFileName = snd . splitFileName
splitFileName :: FilePath -> (FilePath,FilePath)
splitFileName f = case (elemIndices '/' f) of
[] -> (".", f)
x -> splitAt (last x) f
main :: IO ()
main = do
imagePath:_ <- getArgs
print imagePath
let imageFiles = [getFileName imagePath]
dir = getDir imagePath
maybeImages <- loadImages [imagePath]
let images = [(x,a) | (x,Just a) <- zip imageFiles maybeImages]
let pieces = (fmap . fmap) (chop 80 80) images
mapM_ (uncurry $ savePieces dir) pieces
--print $ manifestSize $ (last . head) pieces
--print $ map length pieces
savePiece :: FilePath -> RGB -> IO ()
savePiece name img = save PNG name img >> return ()
savePieces :: FilePath -> String -> [RGB] -> IO ()
savePieces dir basename pieces = do
let f n = concat [dir, "/chopped/", show n,"/"]
paths = fmap f [1..(length pieces)]
names = fmap (++basename) paths
mapM_ (createDirectoryIfMissing True) paths
mapM_ (uncurry $ save PNG) (zip names pieces)