While I was trying to write a small program to preview DjVu files I encountered a peculiar problem. This is a small function that renders a page of a DjVu document into QImage class of PyQt4.

import djvu.decode as DjVu
from PyQt4.QtCore import QImage
from PyQt4.QtGui import QPixmap, QApplication, QLabel

def djvu2pixmap( filename, width, height ) :
    doc = DjVu.Context().new_document( DjVu.FileURI( filename ) )
    doc.decoding_job.wait()
    pg = doc.pages[ 0 ]
    pg.decode( wait = True )
    data = pg.thumbnail.render( ( width, height ), DjVu.PixelFormatRgbMask( 0xff0000, 0xff00, 0xff, bpp = 32 ) )
    image = QIamge( data[ 1 ], data[ 0 ][ 0 ], data[ 0 ][ 1 ], QImage.Format_RGB32 )
    image.save( "/tmp/image.png" )
    return QPixmap.fromImage( image )

if __name__ == '__main__' :

    import sys

    app = QApplication( sys.argv )

    lbl = QLabel()
    lbl.setPixmap( djvu2pixmap( "trial.djvu" ), 200, 300 )
    lbl.show()
    lbl.resize( lbl.pixmap().size() )

    exit( app.exec_() )

Say now I set the image width and height to ( 300, 500 ) the program crashes out with SegmentationFault. I also realized that there is no problem with the image. It does get saved properly. I just cannot display it. Not only that, but I can open the saved image and display it without any problem. Im curious as to why this happens.

To test out your program and possible help you, I installed
python-djvulibre-0.3.4.win32-py2.7.exe
from
http://pypi.python.org/pypi/python-djvulibre/0.3.4

This works:

import djvu

help(djvu)

'''output -->
Help on package djvu:

NAME
    djvu

FILE
    c:\python27\lib\site-packages\djvu\__init__.py

PACKAGE CONTENTS
    const
    decode
    dllpath
    sexpr
'''

However, this gives an error:

import djvu.decode

'''output -->
Traceback (most recent call last):
  File "C:/Python27/Atest27/Bull/djvu_test2.py", line 1, in <module>
    import djvu.decode
ImportError: DLL load failed: The specified module could not be found.
'''

Is there something missing that you have found?

Maybe this test print will help:

import djvu.decode as DjVu
from PyQt4.QtCore import QImage
from PyQt4.QtGui import QPixmap, QApplication, QLabel

def djvu2pixmap( filename, width, height ) :
    doc = DjVu.Context().new_document( DjVu.FileURI( filename ) )
    doc.decoding_job.wait()
    pg = doc.pages[ 0 ]
    pg.decode( wait = True )
    data = pg.thumbnail.render( ( width, height ), 
           DjVu.PixelFormatRgbMask( 0xff0000, 0xff00, 0xff, bpp = 32 ) )
    image = QIamge( data[ 1 ], data[ 0 ][ 0 ], data[ 0 ][ 1 ], 
            QImage.Format_RGB32 )
    image.save( "/tmp/image.png" )
    return QPixmap.fromImage( image )

if __name__ == '__main__' :

    import sys

    app = QApplication( sys.argv )

    lbl = QLabel()
    pixmap = djvu2pixmap( "trial.djvu", 200, 300 ) 
    print(pixmap)  # a test print
    lbl.setPixmap( pixmap, 200, 300 )
    lbl.show()
    lbl.resize( lbl.pixmap().size() )

    exit( app.exec_() )

Thanks Ene... But I donot use Windows. I am on Linux (GNU/Linux Debian Wheezy) and the version of python-djvulibre I use is 0.3.9 on python 2.7.3 and PyQt4 4.9.3 and Qt 4.8.2.

And yeah...! The test print works alright! I had tried even that. If you check, even the lbl.resize(...) works perfect. The test print print(lbl.sizeHint()) prints PyQt4.QtCore.QSize(300, 423). The Segmentation Fault comes up once it reaches the app.exec_() I think.

PS: Also I would like to correct two lines of my code. Line 2 and 3 should be replaced by from PyQt4.QtGui import QPixmap, QApplication, QLabel, QImage, Line 11 QIamge should be QImage and Line 22 should be lbl.setPixmap( djvu2pixmap( "trial.djvu" ) ).

I normally use:

from PyQt4.QtCore import *
from PyQt4.QtGui import *

to make life simpler

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.