Я использую PyMuPDF для обработки PDF-файла и последующего его повторного сохранения, но полученный файл теряет исходную ориентацию страницы и рамки обрезки. Некоторые страницы исходного PDF-файла больше или имеют различную ориентацию (например, повернуты или имеют пользовательские области обрезки), но после вызова pdf.save() все страницы становятся одинакового размера и ориентации.
Пример:
import pymupdf
pdf = pymupdf.open(pdf_path, filetype="pdf")
pdf.save("pymupdf-exported.pdf")
Исходный файл: https://static.vitra.com/media/asset/86 ... 024-EN.pdf
Экспортированный PDF: https://drive.google.com/file/d/1mVzAoS ... sp=sharing
Как сохранить исходную ориентацию страницы и поля обрезки при использовании PyMuPDF, чтобы повторно сохраненный PDF-файл соответствует исходному макету?
Моя конечная цель:
def convert_pdf_to_image_arrays(pdf_path: str, zoom: int, dpi: int) -> list[np.ndarray]:
"""
Convert a PDF to high-resolution image arrays, preserving color fidelity.
:param pdf_path: Path to the PDF file.
:param dpi: DPI (dots per inch) for rendering high-resolution images.
:return: List of NumPy arrays representing images of the PDF pages.
"""
pdf = pymupdf.open(pdf_path, filetype="pdf")
images: list[np.ndarray] = []
for page in pdf:
# Render the page to a pixmap with the desired DPI
pix = page.get_pixmap(dpi=dpi)
# Convert the raw pixel data to a PIL image (preserving color accuracy)
img_pil = Image.frombytes(
mode="RGB" if pix.n == 3 else "RGBA",
size=(pix.width, pix.height),
data=pix.samples,
)
# Convert the PIL image to a NumPy array
img_array = np.array(img_pil)
# Convert RGBA to BGR if the image has an alpha channel
if pix.n == 4:
img_array = cv2.cvtColor(img_array, cv2.COLOR_RGBA2BGR)
else:
img_array = cv2.cvtColor(img_array, cv2.COLOR_RGB2BGR)
images.append(img_array)
pdf.close()
return images
Изменить: я попробовал распечатать рамку обрезки всех страниц.
pdf = pymupdf.open(pdf_path, filetype="pdf")
images: list[np.ndarray] = []
for page in pdf:
print(f"Cropbox {page.number}: {page.cropbox}")
Вывод:
Cropbox 0: Rect(0.0, 0.0, 612.0, 792.0)
Cropbox 1: Rect(0.0, 0.0, 612.0, 792.0)
Cropbox 2: Rect(0.0, 0.0, 612.0, 792.0)
Cropbox 3: Rect(0.0, 0.0, 612.0, 792.0)
Cropbox 4: Rect(0.0, 0.0, 612.0, 792.0)
Cropbox 5: Rect(0.0, 0.0, 612.0, 792.0)
Cropbox 6: Rect(0.0, 0.0, 612.0, 792.0)
Cropbox 7: Rect(0.0, 0.0, 612.0, 792.0)
Cropbox 8: Rect(0.0, 0.0, 612.0, 792.0)
Cropbox 9: Rect(0.0, 0.0, 612.0, 792.0)
Cropbox 10: Rect(0.0, 0.0, 612.0, 792.0)
Cropbox 11: Rect(0.0, 0.0, 612.0, 792.0)
Cropbox 12: Rect(0.0, 0.0, 612.0, 792.0)
Cropbox 13: Rect(0.0, 0.0, 612.0, 792.0)
Cropbox 14: Rect(0.0, 0.0, 612.0, 792.0)
Cropbox 15: Rect(0.0, 0.0, 612.0, 792.0)
Cropbox 16: Rect(0.0, 0.0, 612.0, 792.0)
Cropbox 17: Rect(0.0, 0.0, 612.0, 792.0)
Cropbox 18: Rect(0.0, 0.0, 612.0, 792.0)
Cropbox 19: Rect(0.0, 0.0, 612.0, 792.0)
Cropbox 20: Rect(0.0, 0.0, 612.0, 792.0)
Cropbox 21: Rect(0.0, 0.0, 612.0, 792.0)
Cropbox 22: Rect(0.0, 0.0, 612.0, 792.0)
Cropbox 23: Rect(0.0, 0.0, 612.0, 792.0)
Cropbox 24: Rect(0.0, 0.0, 612.0, 792.0)
Cropbox 25: Rect(0.0, 0.0, 612.0, 792.0)
Cropbox 26: Rect(0.0, 0.0, 612.0, 792.0)
Cropbox 27: Rect(0.0, 0.0, 612.0, 792.0)
Cropbox 28: Rect(0.0, 0.0, 612.0, 792.0)
Cropbox 29: Rect(0.0, 0.0, 612.0, 792.0)
Cropbox 30: Rect(0.0, 0.0, 612.0, 792.0)
Cropbox 31: Rect(0.0, 0.0, 612.0, 792.0)
Подробнее здесь: https://stackoverflow.com/questions/792 ... to-cropped
PyMuPDF - запретить автоматическое обрезание PDF-страниц [закрыто] ⇐ Python
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение