Capture screenshot from pdf page

447 views Asked by At

I have a pdf document and this page has an image of a graph plot, however legend of the plot is not part of the image. I am using pymupdf to extract get this image as following:

  for img in doc.getPageImageList(page_num, full=True):
    xref = img[0]
    pix = fitz.Pixmap(doc, xref)
    if pix.n - pix.alpha < 4:  # this is GRAY or RGB
      pix.writePNG(basePath+"/test_data/"+fund_type+"/%s-%s.png" % (filename+str(page_num), xref))
      print(filename + ' : ' + basePath + "/test_data/" + fund_type+ '/'+filename+ str(page_num) + '-'+str(xref), file=f)

Now, this gives me the image(a graph plot). I want to be able to capture some height below the image so that plot legend is also captured as part of the image. Is this possible using pymupdf? Any code pointers would also be helpful.

1

There are 1 answers

0
hellpanderr On

You can use page.get_pixmap method with a clipping area derived from your image:

page = doc[page_num]
images = page.get_images(full=True)
image = images[0] # the image we need
bbox = page.get_image_bbox(image)
bbox.y1 += 50 # add some space below
page.get_pixmap(dpi=300, clip=bbox).save('image.png')