Getting pixmap shape without loading pixmap in memory #1894
-
Is there a way to get page pixels size without loading this page to memory? Now I'm using the code matrix = fitz.Matrix(dpi/72, dpi/72)
pix = doc.load_page(p).get_pixmap(matrix=matrix)
return (pix.width, pix.height) but it works slow for document with many pages and uses too much memory. Can I get the future image pixels size without its "construction" ? |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 10 replies
-
A typical Discussions item - no issue. |
Beta Was this translation helpful? Give feedback.
-
As to your question: xref=doc.page_xref(pno) # will not load the page
mediabox = doc.xref_get_key(xref, "MediaBox")
cropbox = doc.xref_get_key(xref, "CropBox") The return values are either |
Beta Was this translation helpful? Give feedback.
-
Sizes for other dpi values can be computes via |
Beta Was this translation helpful? Give feedback.
-
I made a comparison of the 3 methods: def test1(): def test2(): def test3():
|
Beta Was this translation helpful? Give feedback.
I made a comparison of the 3 methods:
´´´python
doc=fitz.open("adobe.pdf")
def test1():
t0=time.perf_counter()
for page in doc:
irect=page.rect.irect
pixsize= irect.width * irect.height * 3 + 88
t1=time.perf_counter()
return t1-t0
def test2():
t0=time.perf_counter()
for i in range(doc.page_count):
xref=doc.page_xref(i)
mb = doc.xref_get_key(xref, "MediaBox")
if mb[0]!="array":
raise ValueError("no mediabox for page",i)
w, h = mb[1][1:-1].split()[2:]
pixsize = w * h * 3 + 88
t1=time.perf_counter()
return t1-t0
def test3():
t0=time.perf_counter()
for page in doc:
pix=page.get_pixmap()
size=pix.size
t1=time.perf_counter()
return t1-t0