Thought Flow

Tag: photos

  • Web-friendly images with ImageMagick

    Before uploading images to my blog or AI Tales, I run them through ImageMagick to resize and convert them to “web-friendly” JPGs:

    convert -format jpg \                                
        -strip \       
        -interlace Plane \
        -set filename:base "%[basename]" \
        -quality 50% *.png \
        "%[filename:base].jpg"

    The command converts all PNG images in a folder to JPG while preserving the filename. Even though quality is set to 50% (you can of course experience with this, I often don’t see a noticeable difference from the original — especially for images generated with AI such as Stable Diffusion.

    And the size is reduced significantly. For example, I can sometimes reduce the size of PNG images by more than 90% (553kb to 39kb i a recent example).

    Here are some more ImageMagick one-liners.

  • Simple photo effects with ImageMagick

    Simple photo effects with ImageMagick

    I recently explored the capabilities of converting photos to something that looks hand-drawn with a pencil using ImageMagick. It is difficult to get good results, but I thought I would share a few one-liners that I found useful to play around with, as well as a bonus effect that turns a photo into something resembling a miniature.

    I have collected all the commands in a gist here.

    ImageMagick, as the name implies, can do a lot of basic and advanced image manipulation out of the box. For example, this is my current go-to command for resizing all images in a folder:

    mogrify -resize 1200 *.jpg

    mogrify and convert are two commands included with ImageMagick, and they can do a lot more than just resize photos.

    I will be using this photo of the Geirangerfjorden in Norway as an example throughout:

    Photo of Geirangerfjorden in Norway.

    By the way, I also used Imagemagick to reduce the resolution and size of that photo so it was better suited for this post:

    convert -format jpg \
        -strip \
        -interlace Plane \
        -quality 50% photo.jpg \
        -resize 1200 photo_resized.jpg

    Note: Before running any of the commands in the following sections, you might want to make sure your terminal has extendedglob enabled:

    # zsh
    setopt extendedglob
    # bash
    shopt -s extglob

    Finding outlines

    My use case for finding outlines was to simplify photos into basic lines that I could use as models for my own pencil drawings.

    This command finds very sparse and minimal outlines using the canny edge detector:

    convert ^*canny*.jpg \
        -set filename:original %t \
        -resize 1200 \
        -canny 0x3+5%+15% \
        -negate '%[filename:original]_canny1.jpg'
    Image outline using canny edge detector – v1

    With a small adjustment to the parameters, we can get a bit more detail and more lines.

    convert ^*canny*.jpg \
        -set filename:original %t \
        -resize 1200 \
        -canny 0x1+10%+20% \
        -negate '%[filename:original]_canny2.jpg'
    Image outline using canny edge detector – v2

    You can play around with the canny parameters for different results..

    Pencil/sketch effect

    Trying to get closer to an actual pencil-like effect, I played around with various methods. Most of the commands were modified from this article.

    ImageMagick has a convert-to-pencil effect that is called sketch. It is a bit difficult to get good results though, and the command can be very slow if a large radius (one of the parameters) is used.

    This was the best I could do when playing around with the parameters for a few minutes. Notice the weird trick where I negate the colors — it was the only way to avoid the photo becoming completely white in the middle where the ship is. That is why the sky becomes so dark:

    convert geiranger.jpg \
        -resize 1200 \
        -colorspace gray \
        -negate \
        -sketch 0x20+20 geiranger_sketch.jpg
    Convert photo to pencil-like format using the sketch parameter.

    Another way to create pencil-like photo conversions is using the technique that the sketch parameter is based on. This involved creating an intermediate image which is used as a filter over the image to give the pencil-stroke effect.

    I have to admit, I have no idea what is going on in these commands, and I mostly got them from the article mentioned above. First, create some noise with motion blur:

    convert -size 256x256 \
        xc: +noise Random \
        -virtual-pixel tile \
        -motion-blur 0x20+20 \
        -charcoal 1 \
        -resize 50% pencil_tile.gif

    Then convert the photo:

    convert geiranger.jpg \
        -colorspace gray \
        \( +clone -tile pencil_tile.gif -draw "color 0,0 reset" \
        +clone +swap -compose color_dodge -composite \) \
        -fx 'u*.2+v*.8' geiranger_sketch2.jpg

    The result is slightly different, but not perfect:

    Convert photo to pencil-like format using intermediate noise tile.

    A third technique is to use the charcoal parameter which is actually being used to create the intermediate image above:

    convert geiranger.jpg \
        -charcoal 1 geiranger_char1.jpg
    Convert photo to pencil-like format using -charcoal parameter

    Miniature faking

    I have always been fascinated by those photos that have a blurry effect that make them look like they are miniatures. The effect is called miniature faking and is related to tilt-shift photography.

    The aforementioned article has an example. It works in some cases, but most of the time, the outcome is very bad. It happens to be ok for the Geiranger photo though. Slightly adjusted from the article:

    convert geiranger.jpg -sigmoidal-contrast 10x50% \
        \( +clone -sparse-color Barycentric '0,0 black 0,%h gray80' \
        -solarize 50% -level 30%,0 -write mpr:blur_map \) \
        -compose Blur -set option:compose:args 10x0 \
        -composite mpr:blur_map \
        -compose Blur \
        -set option:compose:args 0x10 \
        -composite geiranger_blur.jpg
    Convert photo to a “miniature” scene. This does not work well on all photos.

    That is it for now. Good night :-)

  • Photo Amaze

    Screenshot of Photo Amaze app

    About a month ago, I attended the wedding of a childhood friend. Since I have had some extra free time lately, I came up with an idea of combining my interest in 3D with an app that could be used for the wedding. The result was Photo Maze, a 3D maze where the guests at the wedding could upload a photo from their phones and it would appear immediately in the maze, giving the bride and groom a kind of interactive photo album from their party.

    I felt the urge to develop this idea a bit further and now, it has be re-named to Photo Amaze — a pun on maze and amaze. It is available for everyone, and I hope you will try it out!

  • Business Stock Photo

    Did you ever wonder where companies get their nice looking photos for the pages of their website? You know, the ones with a team with an equal amount of male and female employees that are just so happy about their jobs and dying to get some work done and have you as their next customer? Like this one:
    Businesswoman consulting a partner
    I wondered about it but in many cases the answer is probably: Stock Photo. Take a look at the pictures in this most entertaining blog post:
    Microstock: why would a reputable company do this to themselves?

    Quite weird having people on your corporate pictures that have never set foot in your company don’t you think? But it’s good fun.