0

I'm new to wxPython and pymupdf, and have had a look at the samples for wxPython + pymupdf. They work, however the quality of the pdf page (rendered) is pretty poor. I'm certain this can be improved. Basically I'm looking for an anti-aliasing solutoin. However I don't know how and haven't been able to find a sample online. Please can someone provide a sample for displaying PDFs using wxpython + pymupdf.

Here's what I have tried:

# https://pymupdf.readthedocs.io/en/latest/tutorial/#rendering-a-page
# if you used alpha=True (or letting default it):
bitmap = wx.Bitmap.FromBufferRGBA(pix.width, pix.height, pix.samples)

# if you used alpha=False:
bitmap = wx.Bitmap.FromBuffer(pix.width, pix.height, pix.samples)

And also this:

# http://code.activestate.com/recipes/580621-wxpython-pdf-xps-viewer-using-pymupdf-binding-for-/
pix = page.getPixmap(matrix = self.matrix)
bmp = wx.BitmapFromBuffer(pix.w, pix.h, pix.samplesRGB())
Don
  • 6,632
  • 3
  • 26
  • 34

2 Answers2

1

The following produces a pretty high quality image for me using just pymupdf:

doc = fitz.open(fname)
for idx, page in enumerate(doc):
    pix = page.getPixmap(alpha = False)
    mat = fitz.Matrix(2.0, 2.0)
    pix = page.getPixmap(matrix = mat)
    pix.writeImage(dest + idx + '.jpg')
James Shapiro
  • 4,805
  • 3
  • 31
  • 46
1

Here is an example on how to set anti-aliasing levels in PyMuPDF:

>>> import fitz
>>> fitz.TOOLS.anti_aliasing_values()
{'graphics': 8, 'text': 8, 'graphics_min_line_width': 0.0}
>>> fitz.TOOLS.set_aa_level(4)
>>> fitz.TOOLS.anti_aliasing_values()
{'graphics': 4, 'text': 4, 'graphics_min_line_width': 0.0}
>>> # now generate your pixmap etc., do not forget to also use gamma correction:
>>> pix.gammaWith(factor)  # factor > 1.0
>>> # change the AA values again anytime

For the full discussion thread that motivated the implementation, see here https://github.com/pymupdf/PyMuPDF/issues/467

Jorj McKie
  • 2,062
  • 1
  • 13
  • 17