Skip to content

Instantly share code, notes, and snippets.

@n2o
Last active October 1, 2024 09:22
Show Gist options
  • Save n2o/c3f9df1343d0928303612af2005a23f7 to your computer and use it in GitHub Desktop.
Save n2o/c3f9df1343d0928303612af2005a23f7 to your computer and use it in GitHub Desktop.
Babashka Script to make a PDF file look like a fax by using ImageMagick

Inspired by this Gist and @coryodaniel's solution, I put the script inside a babashka script, called pdf-like-fax.clj:

#!/usr/bin/env bb
(defn pdf-like-fax [input output]
  (let [sign (rand-nth ["+" "-"])
        rotation (rand-int 1000)
        cmd (format "convert -density 150 %s -rotate %s0.%s -attenuate 0.4 +noise Multiplicative -attenuate 0.03 +noise Multiplicative -sharpen 0x1.0 -colorspace Gray %s" input sign rotation output)
        prepared-cmd (str/split cmd #" ")]
    (apply shell/sh prepared-cmd)))

(let [[input output] *command-line-args*]
  (pdf-like-fax input output))

Usage:

bb pdf-like-fax.clj input.pdf output.pdf
@britter
Copy link

britter commented Oct 1, 2024

I've converted this to a nix expression using writeShellApplication here. Thank you for the inspiration!

@n2o
Copy link
Author

n2o commented Oct 1, 2024

I've converted this to a nix expression using writeShellApplication here. Thank you for the inspiration!

Happy you like it! I've already checked your commit out, still fascinated about NixOS, thanks for your talk :-)

@britter
Copy link

britter commented Oct 1, 2024

I think what's really good about it is, that the ability to explicitly reference Image Magick from nixpkgs as well as the ghostshell runtime input that's required by Image Magick. This way you don't need to remember to manually install both tools.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment