Batch Resize Image Files with Racket

Before posting pictures to Diaspora*, pixelfed, or writefreely I often select the worthwhile parts in Gimp. After selecting the worthwile part of a pictures, if the part is still larger than 700 pixels (for Diaspora*) or 1400 pixels (for pixelfed) I try to re-size the snippet before uploading. Re-sizing seems like a decent, or “ethical”, thing to do. I haven't been able to donate to some of the servers that make my free SNS accounts possible, and I hope to avoid burdening any viewers. Resizing before uploading lessens burdens on servers and devices (I imagine). Besides the general ethical reasons, some servers have volume limits, so keeping image sizes down should let me share more photos before approaching my limits.

I re-size the image in Gimp before exporting the photo to an “edited” folder. The photo manager shotwell organizes photos in year/month/day directories. So one day a program could go through the directories created by Shotwell, find all the edited pictures, and organize them in a useful way. In the meantime, I just want save a step in Gimp. With this Racket script I can just save selections to the “edited” directoy without the extra step of resizing the image canvas. It will save time to avoid working with menus…

DrRacket Screenshot: Resize jpg files script

This time I got the script working in DrRacket first, then made an executable script in Emacs…

Emacs Screenshot: Resize jpgs script with * shell *

code ~/.jpg-resize-to-n-or-copy.rkt

#! /usr/bin/env racket #lang racket

(define dir (vector-ref (current-command-line-arguments) 0)) (define resize-to-n (if (< 1 (vector-length (current-command-line-arguments))) (vector-ref (current-command-line-arguments) 1) 1400))

(define working-directory (build-path (current-directory-for-user) dir))

;; Pictures from Shotwell's date-based directory, saved to “edited” ;; file from Gimp (define edited-dir-path (build-path working-directory “edited”)) (define sized-dir-path (build-path working-directory “sized”))

(make-directory* sized-dir-path)

(define to-resize-files (filter (lambda (p) (path-has-extension? p #“.jpg”)) (directory-list edited-dir-path)))

(define to-resize-paths (map (lambda (p) (path->complete-path p edited-dir-path)) to-resize-files))

;; dev test (define samp-edtd (car to-resize-paths))

(define sized-paths (map (lambda (p) (path->complete-path p sized-dir-path)) to-resize-paths))

(define identify (find-executable-path “identify”))

;; ImageMagick identify keys (define im-id-keys '(“name” “type” “geometry” “offset” “bit” “color” “size” “u” “time”)) ;; for associative list, reference the value from the key (define als-ref (lambda (key als) (if (assoc key als) (second (assoc key als)) #f)))

(define samp-alst (map list im-id-keys (string-split (with-output-to-string ; thunk (lambda () (system* identify samp-edtd))))))

(define im-resize-img (lambda (img-pth lmt) (let* ((identify (find-executable-path “identify”)) ;ImageMagick utilities (idfy-alst (map list im-id-keys (string-split (with-output-to-string (lambda () (system* identify img-pth)))))) (fname (als-ref “name” idfy-alst)) (geom-strn (als-ref “geometry” idfy-alst)) (geom-numb-list (map string->number (string-split geom-strn “x”))) (width (car geom-numb-list)) (height (cadr geom-numb-list)) (convert (find-executable-path “convert”)) (szd-pth (path->complete-path (file-name-from-path img-pth) sized-dir-path)) (lmt-str (number->string lmt))) (cond ; conditional statement ((and (< width lmt) (< height lmt))(copy-file img-pth szd-pth #t)) ((> width height) (system* convert “-geometry” (string-append lmt-str “x”) img-pth szd-pth)) (#t (system* convert “-geometry” (string-append “x” lmt-str) img-pth szd-pth)) ))))

(for-each (lambda (pth) (im-resize-img pth resize-to-n)) to-resize-paths)

The first post using an image re-sized with this script:

After checking this image I see that the images should be re-sized to 1080 an not 1400...

#BatchConvertImages #Racket #LearningRacket #ScriptingRacket #Scheme #ResizeImages #ResizeJpgs #BatchConversion #DrRacket #Emacs #Shotwell