Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

In a djvu file, it has two book pages in one djvu page. I would like to split it so that one book page per djvu page. For example,enter image description here

I was wondering if this can be done by some software, preferably command line utilities? Thanks and regards!

PS: This is a file that can be used for test.

share|improve this question

5 Answers 5

Taking bot2417's script as base, here my own

#!/bin/bash
echo "################################################"
echo Usage: djvusplit2 LASTPAGE FILE.DJVU
echo "################################################"

if [ ! -f $2 ];
then
    echo "file $2 not exists!\n"
    exit
fi

start=1    
mkdir ./tmp

for i in $(seq $start +1 $1)
do
    j=$(($i*2-1))
    k=$(($i*2))

    # extract pages to tiff format
ddjvu -format=pbm -page=$i $2 ./tmp/$i.tiff

# split pages
convert -crop 2x1@ ./tmp/$i.tiff ./tmp/$i-%d.tiff

#delete extracted tiff
#rm ./tmp/$i.tiff

# convert tiff to djvu pages
cjb2 ./tmp/$i-0.tiff ./tmp/$j.djvu
cjb2 ./tmp/$i-1.tiff ./tmp/$k.djvu

#delete splitted tiffs
#rm ./tmp/$i-0.tiff
#rm ./tmp/$i-1.tiff

# create djvu file and add pages
if [ $i -eq 1 ];
    then
        djvm -c "(new) $2" ./tmp/$i.djvu
        echo "create new $2 OK"
        djvm -i "(new) $2" ./tmp/$(($i+1)).djvu
        echo "insert page $(($i+1)) OK"
    else
        djvm -i "(new) $2" ./tmp/$j.djvu
        echo "insert page $j OK"
        djvm -i "(new) $2" ./tmp/$k.djvu
        echo "insert page $k OK"
fi

#delete djvu pages
#rm ./tmp/$j.djvu
#rm ./tmp/$k.djvu

done

echo "\nfile (new) $2 created!!!"
echo "\n"

# cleanup temp dir
rm -r ./tmp
share|improve this answer
    
Do you have one or two lines of explanation what is changed/better? For those of us to lazy to analyse the script. –  Anthon May 3 at 5:40

Here is my complete working script. It needs djvulibre-bin library for ddjvu (decode to tiff format), cjb2 (because i don't know how to directly convert tiff to djvu), djvm (for insertions/deletions of pages in djvu file) tools and imagemagick library for convert (for splitting page and converting to mystical pbm file format) tool. All libraries available through apt-get.

Scheme:

file.djvu --./ddjvu--> bifold tiff --./convert--> single page tiff --./convert--> pbm --./djvm--> out.djvu

Example: djvusplit 3 10 file.djvu which means split pages from 3 to 10.

Complete code:

#!/bin/bash
echo Usage: djvusplit PAGEFROM PAGETO FILE.DJVU

#make temp folder
mkdir ./tmp
cp "$3" ./tmp/
cd tmp

for i in $(seq -w $2 -1 $1) # Descending
do
    ddjvu -format=tiff -page=$i "$3" t$i.tiff
    convert -crop 2x1@ t$i.tiff t$i-%d.tiff
    convert t$i-0.tiff t$i-0.pbm
    convert t$i-1.tiff t$i-1.pbm
    cjb2 t$i-0.pbm t$i-0.djvu
    cjb2 t$i-1.pbm t$i-1.djvu
    djvm -i t$i-0.djvu t$i-1.djvu 2
    djvm -d $3 $i
    djvm -i $3 t$i-0.djvu $i
    rm ./t*
done

# total clean
mv $3 ../out.djvu
cd ..
rm -r ./tmp

Unfortunately...

  1. It works slow because of generating many tiff/pbm/djvu files.
  2. Split page looses any OCR text.

I use this script with some other to maintaining my electronic library. They located here on GitHub.

share|improve this answer

There aren't so many tools that can operate directly on DjVu files, compared with other more common formats such as PDF or JPEG. With image manipulation programs, there's the added hurdle that most of these operate on a single image at a time, but the DjVu file contains multiple pages.

One possibility is to go via pdf. With ddjvu from DjVuLibre, a PDF un2up filter, and pdf2djvu:

ddjvu -format=pdf 2up.djvu 2up.pdf
un2up <2up.pdf | pdf2djvu /dev/stdin >1up.djvu

You might be able to cobble up an un2up for djvu inspired by my pdf version using python-djvulibre. I haven't checked how hard that API is to get into.

share|improve this answer

The following is untested, but in principle it should work (I will test it if I have more time).

You could convert the djvu file for example to jpg's like this:

#!/bin/sh
# djvu -> jpgs converter

i=1

# number of pages (392)
while [ $i -ne 392 ]
do
ddjvu -page=$i -format=pnm 1.djvu $i.pnm
pnmtojpeg $i.pnm > $i.jpg
rm -f $i.pnm
echo "page $i done"
i=`expr $i + 1`
done

(from http://caree.livejournal.com/74639.html)

Then you could use scantailor to split the pages and produce a new output (consisting of tif files).

Apply in a third step djvubind to that folder and you get your desired djvu file.

share|improve this answer
    
Thanks! I wonder if it is possible to operate on djvu files directly, without converting to other formats. I knew that the job can be done by converting djvu files to pdf files, and then operate on pdf files and finally convert back to djvu files. –  Tim Dec 6 '11 at 21:38

http://en.wikisource.org/wiki/Help:DjVu_files#Splitting_DjVu_files

Hope you find your answer here.

share|improve this answer
    
Thanks, but the link is to extract some pages from a djvu file into a new djvu file. I would like to split each djvu page into two djvu pages in a djvu file. –  Tim Dec 6 '11 at 19:58
    
yeah, idea is to extract all the pages as individual from djvu file and then merge the even number of files into single djvu file and repeat this for every 2 files. Finally, merge all the doubly-paged single djvu files into single djvu file. –  Nikhil Mulley Dec 6 '11 at 20:12
    
I don't quite understand your comment. How do you solve the question in my post? –  Tim Dec 6 '11 at 20:31
    
I need to get djvu tools installed on my system and will try to give you a work around. –  Nikhil Mulley Dec 6 '11 at 20:39

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.