How to disable line weights when saving a page to image? #1464
-
I am currently working on pages with vector graphics. I want to save the page as an image but I encounter issues with 'line weights'. What I expected: (with line weights disabled)What I got: (with line weights enabled)In adobe pdf, I can disable 'line weights' when ( and only when) viewing the pdf, following this link . I am using the following code to save the page as image: doc = fitz.open(pdffile)
page = doc.loadPage(page_id)
pix = page.getPixmap(annots=False, matrix=fitz.Matrix(scale, scale))
pix.writePNG(out_name) How can I disable the 'line weights' when saving it as image? Here is a minimal example. The difference becomes more prominent when zooming in. |
Beta Was this translation helpful? Give feedback.
Replies: 6 comments 3 replies
-
Beta Was this translation helpful? Give feedback.
-
I expect the following, with the 'line weight' disabled. This is viewed with adobe pdf. |
Beta Was this translation helpful? Give feedback.
-
So you want an option to maintain a line width of 1 even if the image is generated with 400% resolution? |
Beta Was this translation helpful? Give feedback.
-
But your first image shown with that CL600 text should not happen either. |
Beta Was this translation helpful? Give feedback.
-
My current workaround is to
import re
doc = fitz.open(pdffile)
page = doc.loadPage(page_id)
svg = page.get_svg_image(matrix=fitz.Identity)
svg = re.sub(r'stroke-width=".[0-9][0-9]"', 'stroke-width=".01"', svg)
print(svg, file=open("dummy.svg", "w"))
pix_doc = fitz.open("dummy.svg")
pix = pix_doc.load_page(0).get_pixmap(
annots=False, matrix=fitz.Matrix(scale, scale)
)
pix.save("thin_lines.png") The problem becomes how to change the line width of vector graphics in pdf. |
Beta Was this translation helpful? Give feedback.
-
This snippet also works ... at least with your example PDF: >>> doc=fitz.open("cl600.demo.pdf")
>>> page=doc[0]
>>> page.clean_contents() # enforce standard painting syntax
>>> cont_xref = page.get_contents()[0] # we only have 1 of those now
>>> # read an modify the paint commands
>>> lines = doc.xref_stream(cont_xref).splitlines()
>>> # walk through lines and replace each width value
>>> for i in range(len(lines)):
if lines[i].endswith(b" w"): # command to set line width!
lines[i] = b"0.02 w"
>>> # write back modified paint commands
>>> doc.update_stream(cont_xref, b"\n".join(lines))
>>> # now either make a pixmap of modified page ...
>>> pix=page.get_pixmap(dpi=300)
>>> pix.save("300.png")
>>> # ... or save a modified PDF
>>> doc.save("thin-lines.pdf")
>>> |
Beta Was this translation helpful? Give feedback.
This snippet also works ... at least with your example PDF: