Showing posts with label pdf. Show all posts
Showing posts with label pdf. Show all posts

Sunday, November 19, 2023

Comparing PDFs, Round 2

I've found an even better application for the idea mentioned in my last post. It can be very hard to see all the copy editor's corrections. Well, PDF comparison to the rescue! This script is slightly more complex, since I have a number of different files from them, one for each chapter. But not a problem. First, explode the PDFs into images using pdftocairo, as before. Now:

#!/bin/bash

#DEBUG=echo;
# basename of the images for this
# chapter from copy editor
TYP=$1;
# basename for 'original' pages
BASE=
SenseBook-19-IX-2023-Revised
# first page number of 'original' pages
XPAGE=$2;

for PG in $TYP*tif; do
    NEXT=$XPAGE;
    if (($XPAGE < 10)); then NEXT=0$NEXT; fi
    if (($XPAGE < 100)); then NEXT=0$NEXT; fi
    $DEBUG compare $PG $BASE-$NEXT.tif Comp-$NEXT.tif;
    XPAGE=$(($XPAGE + 1));
done

When that's done, it gives me the comparison images for that chapter. So now just:

convert Comp*tif Edited-ChN.pdf

And now it's easy to see where the corrections are:



Saturday, November 18, 2023

Comparing PDFs

I'm working on final corrections for my book Modes of Presentation, which I'm again typesetting myself via LyX and LaTeX (as I did Frege's Theorem and Reading Frege's Grundgesetze). I'm paranoid about something weird creeping into the book and have been comparing the new and old pages as I go. I figured there had to be a better way to do that than flipping back and forth between the two PDFs. All the more so given that doing so feels like one of those change-blindness experiments.

Well, Linux and the command line to the rescue. The ImageMagick suite contains a 'compare' command that takes two images and produces a new one that shows the differences between them. So all I have to do is explode the PDF into a bunch of page images and run the compare command on them. Here's a generic script to do it:

#!/bin/bash

# Uncomment to test with just a few pages
#D2="-l 10";
# Image resolution
RES=100;

pdftocairo $D2 -gray -tiff -r $RES NEWPDF.pdf New;
pdftocairo $D2 -gray -tiff -r $RES OLDPDF.pdf Old;
for NEW in New*tif; do
    BASE=${NEW#New};
    OLD=Old$BASE;
    compare $NEW $OLD Comp$BASE;
done

Here's an example of what you get:

It's not readable, but you can easily see where the changes have been made and, if need be, check the actual page. Mostly, I want to make sure that nothing dramatic has changed with the page breaks, etc, anyway.

Of course, you can also do this on other operating systems, but they do not encourage you, as Linux does, to use the command line.


Friday, August 5, 2016

Converting "Stitched" Pages from PDFs

More and more great books (including philosophy) are going out of copyright and so are appearing on public archives, like Project Gutenberg and archive.org. Unfortunately, though, many of these PDFs are constructed in a somewhat odd way, with each page consisting of several separate images that get "stitched" together. So if you try to extract the pages to run them through OCR, say, it ends up looking like you ran the pages through a shredder.

Fortunately, as I noted in an earlier post, we can use ImageMagick to fix this up. I'm having to do this often enough now that I've written a small script to automate the process. Here it is:
#!/usr/bin/perl
my @splits = @ARGV;
my $start = shift @splits;
my $stop = shift @splits;
sub normalize {
        my $in = shift;
        if ($in < 100) { $in = "0$in"; }
        if ($in < 10) { $in = "0$in"; }
        return $in;
}
my $newpage = 1;
while (1) {
        my @files;
        for (my $i = $start; $i < $stop; $i++) {
                push @files, "*" . normalize($i) . ".pbm";
        }
        my $cmd = "convert " . join(" ", @files) . " -append outpage" . normalize($newpage) . ".tiff";
        print "$cmd\n\n";
        system($cmd);
        my $lasttime = scalar @splits;
        last if $lasttime == 0;
        $start = $stop;
        $stop = shift @splits;
        $newpage++;
}

The script can also be downloaded here.

There are two ways to invoke the program.

stitch_pages -n INIT STEP PAGES

In this case, INIT gives the number of the first image (this will usually be 0 or 1); STEP tells how many images are used to construct each page; and PAGES tells how many pages we are constructing.

Obviously, this assumes that there are the same number of partial images for each page. If that is not true, you can use the other form and specify the "splits" manually.

stitch_pages -s SPLIT1 SPLIT2 ... SPLITn

In this case, we will stitch together the partial images SPLIT1 - (SPLIT2 - 1), etc. The last split given should thus be one greater than the last image available.

Wednesday, January 14, 2015

PDF of "From a Logical Point of View"

More and more cool stuff appears in electronic form, this time a PDF of Quine's From a Logical Point of View, free on archive.org.

While we're at it, how about Russell's Inquiry Into Meaning and Truth?