Closed johnathanchiu closed 1 week ago
I think the coordinate system here is really confusing. I tried cropping a piece of the page and got this:
ValueError: (0, 755.0, 612, 720.0) has a negative width or height.
Returns a version of the page cropped to the bounding box, which should be expressed as 4-tuple with the values (x0, top, x1, bottom). Is
top
not actually top?
As per the documentation, this should be fine.
I have another example of how weird this coordinate system is. Run the following:
with pdfplumber.open("background-checks.pdf") as pdf:
page = pdf.pages[0]
im = page.to_image()
im2 = page.to_image()
for obj in page.objects["char"]:
# correct orientation
im.draw_rect([obj["x0"], page.height - obj["y1"], obj["x1"], page.height - obj["y0"]])
# incorrect orientation
im2.draw_rect([obj["x0"], obj["y0"], obj["x1"], obj["y1"]])
im.show()
im2.show()
im
returns the correct orientation whereas im2
doesn't.
Upon further experimentation, I found that the objects in the pdf actually have a top
and bottom
key. This means the right way of doing this is:
with pdfplumber.open("background-checks.pdf") as pdf:
page = pdf.pages[0]
im = page.to_image()
for obj in page.objects["char"]:
im.draw_rect([obj["x0"], obj["top"], obj["x1"], obj["bottom"]])
im.show()
Describe the bug
Currently, the bounding box on a
CroppedPage
object is not relative to the original page but absolute to the crop itself. This doesn't make sense especially if the object coordinates of the cropped page are still relative to the parent page coordinates. I think these should be consistent. Either make both the coordinates of theCroppedPage
and objects relative to the parent page or make both absolute to the crop.Code to reproduce the problem
PDF file
https://github.com/jsvine/pdfplumber/blob/stable/examples/pdfs/background-checks.pdf
Actual behavior
Looking at the print, the
y0
andy1
values are far outside the37.0
and72.0
.This change of coordinates doesn't make sense if I want to use the bounding box crop without the parent crop.
Environment
0.11.4
3.11.5