=== Top of the Swiki === Attachments ===

Recipe: Processing each file in a directory with a specific extension

Problem


I wanted to import some images of kanji characters, but ones I got off the web had white background (with exception of color words, which had the corresponding color bg). So I wanted to mass convert them to be transparent.

Solution


I ran the following code:
| slash dir image transparent black dirName | "read all gif images, make all colors, except black, transparent, open as a SketchMorph - like from FileList menu"
slash _ FileDirectory slash.
black _ Color r:0 g:0 b: 0.
dirName _ (FileDirectory default pathName), slash, 'kanji', slash, 'matrix_kanji', slash.dir _ FileDirectory on: dirName.
dir fileNames withIndexDo: [:fname :fid | (fname asLowercase endsWith: '.gif')
ifTrue: [image _ GIFReadWriter formFromFileNamed: dirName, fname.
transparent _ (image colors select: [:c | c isTransparent]) first.
image colors withIndexDo: [:c :i |
(c isTransparent not and: [(c = black) not])
ifTrue: [image colors at: i put: transparent]].
World addMorph: (image _ SketchMorph new form: image).
image position: (fid*10)@(fid*10)]].

Source

Squeak mailing list - Bolot Kerimbaev -
Subject: [GOODIE] selecting multiple morphs, kanji fun
Date: Wed, 17 Nov 1999