Git Product home page Git Product logo

Comments (7)

Knightowl3128 avatar Knightowl3128 commented on August 11, 2024 1

I found the solution and it has to do with scaling in Microsoft Windows and DPI awareness. The solution is to set DPI awareness by adding this in the code.

import ctypes

ctypes.windll.shcore.SetProcessDpiAwareness(2)

from pymupdf-utilities.

JorjMcKie avatar JorjMcKie commented on August 11, 2024

You already have found the doc-browser.py script, which contains the ability to zoom into a page - however at a fixed constant rate: the available window for page content in zooming mode, can be used for showing one of the 3 x 3 parts of the original page.

Look at the logic to achieve zooming: Use a matrix mat = fitz.Matrix(zoom, zoom) when making a pixmap: page.get_pixmap(matrix=mat,...).
The zoom factor is a float which magnifies (or shrinks) if >1 or <1. So if zoom=2 then magnification is 200% etc.
Assuming your display window has the dimensions WIDTH and HEIGHT, and the sub-rectangle of the page you want to show is rect = fitz.Rect(x0, y0, x1, y2), then do this to calculate the correct zoom factor:

if rect.width / rect.height < WIDTH / HEIGHT:
    zoom = HEIGHT / height
else:
    zoom = WIDTH / width
mat = fitz.Matrix(zoom, zoom)
pix = page.getPixmap(matrix=mat, clip=rect, alpha=False)

from pymupdf-utilities.

Knightowl3128 avatar Knightowl3128 commented on August 11, 2024

I'm aware on how to achieve zoom by changing the fitz.matrix. But, what I'm asking is how to get a better resolution at 1x Zoom as shown in the image, where Sumatra gives a better resolution than the doc-browser. If I do a 2x Zoom, I can get a better resolution but I can only see a part of the PDF. I'm a beginner at all of this and I'm confused on how to work with tkinter to fit a higher resolution image to the display window where every part is visible. I hope you understood.

from pymupdf-utilities.

JorjMcKie avatar JorjMcKie commented on August 11, 2024

I understand. I am not sure either.
You can try the following things:

  • change the image format handed to tkinter from "ppm" to "png" and hope this will trigger TK using a different algorithm.
  • simply use a larger zoom factor (e.g. twice at large than computed in previous post) and then see what TK does with it.
  • if the preceeding doesn't work, use Pillow's thumbnail to shrink the image so it fits into TK's window. But after first try, the previous seemss to work ...

from pymupdf-utilities.

JorjMcKie avatar JorjMcKie commented on August 11, 2024

Just looked up Pillow again.
You can do e.g. this:

  • compute the pixmap with a doubled zoom factor
  • convert it to a Pillow Image img, and then use img.resize(size, resample=3, box=None, reducing_gap=1). Play with the resample and reducing_gap values to meet your requirements.

set size =(img.width//2, img.height//2) to restore the original dimensions.

from pymupdf-utilities.

Knightowl3128 avatar Knightowl3128 commented on August 11, 2024

Just looked up Pillow again.
You can do e.g. this:

  • compute the pixmap with a doubled zoom factor
  • convert it to a Pillow Image img, and then use img.resize(size, resample=3, box=None, reducing_gap=1). Play with the resample and reducing_gap values to meet your requirements.

set size =(img.width//2, img.height//2) to restore the original dimensions.

I've tried this and it doesn't work. The final quality is the same as when I just used a zoom factor of 1. I guess I have to read more about downscaling an image. Here's the sample code which I've used:

import fitz
from PIL import Image

pdf = fitz.open('test.pdf')
page_number = 5
page = pdf.loadPage(page_number)

scaling_matrix = fitz.Matrix(2, 2)
pix = page.getPixmap(matrix=scaling_matrix, alpha=False)

img = Image.frombytes('RGB', [pix.width, pix.height], pix.samples)
img.show()
img = img.resize((img.width // 2, img.height // 2),resample=3, box=None, reducing_gap=1)
img.show() 

from pymupdf-utilities.

JorjMcKie avatar JorjMcKie commented on August 11, 2024

Woah! How interesting - never heard of this before.
Thansk for digging this out!

from pymupdf-utilities.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.