import unittest import numpy as np import pymupdf from copienator.crop_blank_margins import apply_bounds class CropBlankMarginsTests(unittest.TestCase): def test_cropbox_coordinates_with_rotation_and_existing_crop(self): for rotation in (0, 90, 180, 270): with self.subTest(rotation=rotation), pymupdf.open() as doc: page = doc.new_page(width=600, height=800) page.set_cropbox(pymupdf.Rect(30, 40, 570, 760)) page.set_rotation(rotation) before = page.rect page.insert_text((80, 220), 'Visible content', fontsize=20) pix = page.get_pixmap() original = np.frombuffer(pix.samples, np.uint8).reshape(pix.height, pix.width, 3) apply_bounds(page, .2, .8) self.assertAlmostEqual(page.rect.width, before.width) self.assertAlmostEqual(page.rect.height, before.height*.6, places=3) self.assertEqual(page.rotation, rotation) pix = page.get_pixmap() cropped = np.frombuffer(pix.samples, np.uint8).reshape(pix.height, pix.width, 3) np.testing.assert_array_equal(cropped, original[int(before.height*.2):int(before.height*.8)]) if __name__ == '__main__': unittest.main()