74 lines
3.9 KiB
Python
74 lines
3.9 KiB
Python
import tempfile
|
|
import unittest
|
|
from pathlib import Path
|
|
from unittest.mock import patch
|
|
|
|
import pymupdf
|
|
from PIL import Image
|
|
|
|
from copienator.commands import cutleft
|
|
|
|
|
|
class NarrowPageCropTests(unittest.TestCase):
|
|
def test_mixed_pages_crop_narrow_margin_from_very_left(self):
|
|
normal = Image.new('RGB', (900, 600), 'red')
|
|
normal.paste('green', (100, 0, 400, 600))
|
|
narrow = Image.new('RGB', (450, 600), 'red')
|
|
narrow.paste('blue', (0, 0, 300, 600))
|
|
thin = Image.new('RGB', (200, 600), 'yellow')
|
|
with patch.object(cutleft, 'get_pdf_pages', return_value=[normal, narrow, thin]):
|
|
result = cutleft.process_single_pdf(Path('/tmp/no-original/Copy.pdf'), automatic_margin=False)
|
|
_, images, schema = result
|
|
image = images[0]
|
|
self.assertEqual(schema['full_width_pages'], [3])
|
|
self.assertEqual(schema['columns_per_file'], [3])
|
|
self.assertEqual(image.size, (910, 600))
|
|
self.assertEqual(image.getpixel((0, 0)), (0, 128, 0))
|
|
self.assertEqual(image.getpixel((299, 0)), (0, 128, 0))
|
|
self.assertEqual(image.getpixel((300, 0)), (0, 0, 0))
|
|
self.assertEqual(image.getpixel((305, 0)), (0, 0, 255))
|
|
self.assertEqual(image.getpixel((604, 0)), (0, 0, 255))
|
|
self.assertEqual(image.getpixel((610, 0)), (255, 255, 0))
|
|
self.assertEqual(image.getpixel((809, 0)), (255, 255, 0))
|
|
self.assertEqual(image.getpixel((810, 0)), (255, 255, 255))
|
|
self.assertEqual(image.getpixel((909, 0)), (255, 255, 255))
|
|
|
|
def test_narrow_margin_can_be_shifted_and_enlarged(self):
|
|
normal = Image.new('RGB', (900, 600), 'white')
|
|
narrow = Image.new('RGB', (450, 600), 'red')
|
|
narrow.paste('blue', (50, 0, 450, 600))
|
|
with patch.object(cutleft, 'get_pdf_pages', return_value=[normal, narrow]):
|
|
result = cutleft.process_single_pdf(
|
|
Path('/tmp/no-original/Copy.pdf'), shift_offset=50, width_offset=100, automatic_margin=False)
|
|
self.assertEqual(result[1][0].size, (805, 600))
|
|
self.assertEqual(result[1][0].getpixel((405, 0)), (0, 0, 255))
|
|
self.assertEqual(result[1][0].getpixel((804, 0)), (0, 0, 255))
|
|
|
|
def test_legacy_original_identifies_narrow_pages_without_normal_siblings(self):
|
|
with tempfile.TemporaryDirectory() as temporary:
|
|
root = Path(temporary)
|
|
originals = root / 'Copies Originales'
|
|
originals.mkdir()
|
|
with pymupdf.open() as document:
|
|
document.new_page(width=1191, height=842)
|
|
document.save(originals / 'Copy.pdf')
|
|
images = [Image.new('RGB', (900, 2339)), Image.new('RGB', (750, 2339))]
|
|
with patch.object(cutleft, 'get_pdf_pages', return_value=images):
|
|
result = cutleft.process_single_pdf(root / 'Copies' / 'Copy.pdf', automatic_margin=False)
|
|
self.assertEqual(result[2]['full_width_pages'], [])
|
|
self.assertEqual(result[1][0].width, 2 * int((1191 / 2 * 200 / 72) // 3) + 5)
|
|
|
|
def test_small_main_split_adjustments_keep_margin_cropping(self):
|
|
images = [Image.new('RGB', (1600, 2339)), Image.new('RGB', (1694, 2339))]
|
|
with patch.object(cutleft, 'get_pdf_pages', return_value=images):
|
|
result = cutleft.process_single_pdf(Path('/tmp/no-original/Copy.pdf'), automatic_margin=False)
|
|
self.assertEqual(result[2]['full_width_pages'], [])
|
|
self.assertEqual(result[1][0].width, 2 * (1694 // 3) + 5)
|
|
|
|
def test_fullpage_option_still_keeps_every_page_separately(self):
|
|
images = [Image.new('RGB', (900, 600)), Image.new('RGB', (450, 600))]
|
|
with patch.object(cutleft, 'get_pdf_pages', return_value=images):
|
|
result = cutleft.process_single_pdf(Path('/tmp/no-original/Copy.pdf'), max_per_file=1)
|
|
self.assertEqual([image.size for image in result[1]], [(900, 600), (450, 600)])
|
|
self.assertEqual(result[2]['columns_per_file'], [1, 1])
|