3

I have a folder with scans of a book numbered 001.jpg through 092.jpg

The scans have two pages of the book scanned together. What I want is split these by two from the middle vertically and regenerate filenames starting from 001.jpg to 184.jpg. So 001.jpg originally will be split as 001.jpg and 002.jpg. To avoid any problems I need to write target files in another directory.

I found out Imagemagick does this though I think I need other tools to manage it for batch processing all these images.

Could somebody help me with this?

3 Answers 3

3

If you have a more recent version of ImageMagick you can batch process the images via the magick command e.g. assuming you're running the command in the target directory (so your original jpgs are in a different directory):

magick /path/to/*.jpg -scene 1 -crop 2x1@ +repage %03d.jpg
2

I don't know imagemagick's command line necessary to split things, but that's also not really a problem here, as you already found a way to do that; I'll use GraphicsMagick instead here; the same idea applies, just that I find ImageMagick's command line to be very confusing.

So, what I'd do is the following:

#!/usr/bin/zsh
# this is a ZSH script. Would probably also work under bash, haven't tried.

targetdir=/home/Plato/newimgs

getwidth=gm identify -format '%w'


for number in {001..092}; do
  infile="${number}.jpg"
  width=$(getwidth) "${infile}"

  leftnum=$((number * 2))
  rightnum=$((leftnum + 1))

  gm convert \
    -crop "50%x100%+0+0" \
    "${infile}" \
    "${targetdir}/${(l:3::0:)leftnum}.jpg"
  gm convert \
    -crop "50%x100%+$((width / 2))+0" \
    "${infile}" \
    "${targetdir}/${(l:3::0:)rightnum}.jpg"  
done
5
  • Thanks it worked pretty well.. Is there a way to crop 190 pixels from left and right before splitting image? Commented Mar 4, 2023 at 18:14
  • 1
    @Plato sure! Just adjust the -crop specification; it's -crop {width}x{height}[+-offsetleft+-offsettop]. I show how you can do simple calculations in your shell: $(( width / 2 - 190)) for example would half the image width and subtract 190 pixels. Commented Mar 4, 2023 at 21:10
  • I need that 190 pixel gone from left for left image and right from right image though, so the formula will be: -crop "50%x100%+190+0" -crop "50%x100%+$((width / 2 - 190))" right? Commented Mar 6, 2023 at 5:30
  • on second thought, these are not correct probably. If we move the offset after getting half, in the first case it'll get 190 pixels from the right image because we move offset 190 pixels not crop 190 pixels more from left. It's the same for the other one. If we do it like this, it'll probably move it left 190 pixels. Isn't that right. Or does it crop with offsets after splitting the image in half? I thought that 50% just gives the width of the image. Commented Mar 6, 2023 at 5:41
  • 1
    Yeah you'll have to explicitly give the desired width in pixels. You want your right image to still start at half the pixels, but your left at 190. Commented Mar 6, 2023 at 6:46
1

Using imagemagick's convert:

#!/bin/bash

inputdir=/path/to/images
outputdir=$inputdir/out
mkdir -p "$outputdir"

cnt=0
for i in "$inputdir/"*.jpg; do
    if convert "$i" -crop 50%x100% "$outputdir/%d.tmp"; then
        printf -v fname '%03d.jpg' $((++cnt))
        mv "$outputdir/0.tmp" "$outputdir/$fname"
        printf -v fname '%03d.jpg' $((++cnt))
        mv "$outputdir/1.tmp" "$outputdir/$fname"
    else
        echo "failed to convert $i" >&2
        exit 1
    fi
done

I haven't found a way to directly specify the output filenames.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.