Creating Thumbnails With Smalltalk
By Ramon Leon - 6 October 2006 under Smalltalk
Here's a quick little bit of code I wrote recently for an interesting little side project. This is a recursive function that will scan a directory path, and create thumbnails of a given scale for every jpg it finds. It looks for any existing thumbnails, and deletes them if there's a name clash, then it creates a new on for each jpg in the folder. After all the thumbs are built, it loops over each subdirectory calling itself on them, starting the whole process over for each sub-folder.
The image is scaled to fit withing the give bounds, they only scale it, not size it.
scaleImagesOnPath: aPath toWidth: aWidth andHeight: aHeight | dir form | dir := FileDirectory on: aPath. Transcript cr; show: dir pathName. (dir fileNames select: [:each | (each asLowercase endsWith: 'jpg') and: [(each includesSubString: 'thumb') not]]) do: [:key | form := Form fromBinaryStream: (dir fileNamed: key). dir deleteFileNamed: (dir fullPathFor: (FileDirectory baseNameFor: key) , '-thumb.jpg'). JPEGReadWriter2 putForm: (form scaledToSize: aWidth @ aHeight) onFileNamed: (dir fullPathFor: (FileDirectory baseNameFor: key) , '-thumb.jpg')]. dir directoryNames do: [:each | self scaleImagesOnPath: dir pathName , each , dir slash toWidth: aWidth andHeight: aHeight]
It's the quick and dirty stuff like this I have the most fun with.
Comments (automatically disabled after 1 year)
Ramon Leon 6535 days ago
Good call, fixed! I wasn't aware of #slash, thanks.
Hi,
This code should be portable by using slash message instead of '\', especially for Unix user. Thus, it gets the good pathNameDelimiter for your dir object according to the system.
Martial