Comments (7)
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.
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.
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.
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.
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 useimg.resize(size, resample=3, box=None, reducing_gap=1)
. Play with theresample
andreducing_gap
values to meet your requirements.
set size =(img.width//2, img.height//2) to restore the original dimensions.
from pymupdf-utilities.
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 useimg.resize(size, resample=3, box=None, reducing_gap=1)
. Play with theresample
andreducing_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.
Woah! How interesting - never heard of this before.
Thansk for digging this out!
from pymupdf-utilities.
Related Issues (20)
- Suggestion for Jupyter notebooks HOT 3
- ModuleNotFoundError: No module named 'ParseTab' HOT 2
- export-toc script outputs invalid csv when bookmark entry has newline character
- anonymize.py raises UnicodeDecodeError HOT 2
- libcrypt.so.2: cannot open shared object file: No such file or directory HOT 3
- multi_column.py does not identify multiple columns in some cases HOT 3
- How to use fitz to delete tables in PDF? Need help, Thanks. HOT 1
- document rescale after replacing fonts HOT 1
- fitzcli.py open file as in-memory stream & parameter for printing instead of writing to .txt HOT 1
- multi_column.py errors with latest version of pymupdf HOT 4
- Use a template for documenting the examples
- Make sure the examples are PEP8 compliant HOT 1
- Merge the demo scripts into the examples folder HOT 2
- Update links in Read the Docs HOT 3
- Merge the conversion scripts into the examples folder
- Merge the image-replacement scripts into the examples folder
- Merge the font-replacement scripts into the examples folder
- Merge the text-extraction scripts into the examples folder
- Merge the textbox-extraction scripts into the examples folder
- Define a folder structure for the examples
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pymupdf-utilities.