in which I curse my lack of omniscience

I want to push several hundred greyscale images through a threshold filter, so that an output pixel is black or white according to whether the corresponding input pixel is less or more than 1/2 bright. (My goal is a ‘woodcut’ effect.)

ImageMagick can apparently do this with one command, but I lack a library that it needs.

GIMP has a menu command with this function, but to do it from a script it seems I’d need to learn Scheme.

I could write a program using Python Imaging Library (which I’ve used to output most of my mathematical designs in recent years), but so far in RTFM I can’t tell how to, y’know, access the pixels it reads in. Later: Found a better FM.

Wish me luck.

This entry was posted in neep-neep. Bookmark the permalink.

2 Responses to in which I curse my lack of omniscience

  1. jonas says:

    I’d really recommend you to install ImageMagick for tasks like this, it’s great. (Just make sure you don’t use a too old version.)

    However, in a pinch you could use ffmpeg with the following command to threshold an image.

    ffmpeg -v 30 -i img.png -f rawvideo -pix_fmt gray pipe: | perl -e "binmode STDIN; binmode STDOUT; while (read STDIN, $_, 1<<18) { y/\x00-\x48/\x00/; y/\x00/\xff/c; print; }" | ffmpeg -v 30 -f rawvideo -video_size 640x480 -pix_fmt gray -i pipe: -y bw.png

    The drawback of this is that you need to put the exact size of the image in the command: replace the 640×480 part with the size of your image. Other parts to replace are the input and output filenames, and the \x48 in the perl code which serves as the threshold. If you don't know the size of the image, find it out with this command.

    ffprobe -v 20 -of csv -show_entries stream=width,height img.png | perl -ne "m((\d+),(\d+)$) and print qq(${1}x$2)"

  2. Anton says:

    I really thought I had installed ImageMagick.

Leave a Reply

Your email address will not be published. Required fields are marked *