diff --git a/README.md b/README.md index c1cf5d3..96c01ac 100644 --- a/README.md +++ b/README.md @@ -1,15 +1,15 @@ -# PDF to DXF Converter +# PDF / Image to DXF Converter -Windows desktop app that converts PDF drawings to **DXF** for BricsCAD (and other CAD software), with a live page preview. Optionally saves **DWG** through your installed BricsCAD. +Windows desktop app that converts PDF and image drawings to **DXF** for BricsCAD (and other CAD software), with a live preview. Optionally saves **DWG** through your installed BricsCAD. **License:** [MIT](LICENSE) ## Features -- PDF page preview with previous/next navigation +- PDF and image preview (`PNG`, `JPG`, `TIFF`, `BMP`, `GIF`, `WebP`) - Conversion modes: **auto**, **vector**, **vectorize**, **raster** - True vector PDFs → editable lines/polylines/text in DXF -- Flattened / image-only drawing PDFs → traced to editable polylines (`vectorize`) +- Image files and flattened PDFs → traced to editable polylines (`vectorize`) - Optional raster underlay mode for scans you want as images - Optional DWG export via BricsCAD `SAVEAS` @@ -51,7 +51,7 @@ Optional only: BricsCAD for DWG save. ## Usage -1. Click **Open PDF…** and pick a file +1. Click **Open PDF or Image…** and pick a supported file 2. Preview pages with ◀ / ▶ 3. Choose mode (`auto` recommended), pages, and scale (1.0 = PDF size; use your own multiplier) 4. Click **Convert & Save…** — review each page (vector / image / vectorize / skip), then pick the DXF path @@ -61,9 +61,9 @@ Optional only: BricsCAD for DWG save. | Mode | Best for | Result | |------|----------|--------| -| `auto` | Mixed PDFs | Native vectors when present; traces image-only drawing pages | +| `auto` | PDFs and images | Native vectors when present; traces image-only inputs | | `vector` | CAD-exported PDFs | Editable geometry and text (traces if the page is only a bitmap) | -| `vectorize` | Flattened / printed drawing PDFs | Raster-to-vector polylines (set **Trace detail** to high/maximum) | +| `vectorize` | Images and flattened drawing PDFs | Raster-to-vector polylines (set **Trace detail** to high/maximum) | | `raster` | Photos / underlays | Full-page image in the DXF | ### BricsCAD DWG diff --git a/app/__init__.py b/app/__init__.py index 1e45235..e435ede 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -1,3 +1,3 @@ """PDF to DXF converter application package.""" -__version__ = "1.4.1" +__version__ = "1.5.0" diff --git a/app/converter.py b/app/converter.py index 359b1e4..02c3c88 100644 --- a/app/converter.py +++ b/app/converter.py @@ -13,6 +13,7 @@ import ezdxf import fitz from ezdxf import units +from app.input_document import open_input_document from app.raster_vectorize import vectorize_page @@ -142,13 +143,13 @@ def classify_page( def classify_pdf_pages( - pdf_path: str | Path, + input_path: str | Path, pages: list[int] | None = None, *, vector_threshold: int = 20, ) -> list[PageClassification]: - """Classify selected pages of a PDF for the pre-convert review dialog.""" - pdf = fitz.open(str(pdf_path)) + """Classify selected pages of a PDF or image for pre-convert review.""" + pdf = open_input_document(input_path) try: indices = _page_indices(pdf, pages) return [ @@ -1134,7 +1135,7 @@ def convert_pdf_to_dxf( pdf_path = Path(pdf_path) output_path = Path(output_path) - pdf = fitz.open(str(pdf_path)) + pdf = open_input_document(pdf_path) try: indices = _page_indices(pdf, options.pages) if not indices: diff --git a/app/input_document.py b/app/input_document.py new file mode 100644 index 0000000..0d98988 --- /dev/null +++ b/app/input_document.py @@ -0,0 +1,45 @@ +"""Open supported PDF and image inputs as PDF documents.""" + +from __future__ import annotations + +from pathlib import Path + +import fitz + + +PDF_EXTENSIONS = {".pdf"} +IMAGE_EXTENSIONS = { + ".bmp", + ".gif", + ".jpeg", + ".jpg", + ".png", + ".tif", + ".tiff", + ".webp", +} +SUPPORTED_EXTENSIONS = PDF_EXTENSIONS | IMAGE_EXTENSIONS + + +def is_image_input(path: str | Path) -> bool: + """Return whether the selected input is a supported image.""" + return Path(path).suffix.lower() in IMAGE_EXTENSIONS + + +def open_input_document(path: str | Path) -> fitz.Document: + """Open a PDF directly or wrap an image in an in-memory PDF.""" + input_path = Path(path) + extension = input_path.suffix.lower() + if extension not in SUPPORTED_EXTENSIONS: + supported = ", ".join(sorted(SUPPORTED_EXTENSIONS)) + raise ValueError(f"Unsupported input type. Supported: {supported}") + + document = fitz.open(str(input_path)) + if extension in PDF_EXTENSIONS: + return document + + try: + pdf_bytes = document.convert_to_pdf() + finally: + document.close() + return fitz.open("pdf", pdf_bytes) diff --git a/app/pdf_preview.py b/app/pdf_preview.py index cad1ed1..b74d92f 100644 --- a/app/pdf_preview.py +++ b/app/pdf_preview.py @@ -7,9 +7,12 @@ from pathlib import Path import fitz from PIL import Image +from app.input_document import open_input_document + def open_document(path: str | Path) -> fitz.Document: - return fitz.open(str(path)) + """Open a supported PDF or image for preview.""" + return open_input_document(path) def page_count(doc: fitz.Document) -> int: diff --git a/app/ui.py b/app/ui.py index c82b54b..6f4feff 100644 --- a/app/ui.py +++ b/app/ui.py @@ -1,4 +1,4 @@ -"""Main CustomTkinter UI for PDF to DXF conversion.""" +"""Main CustomTkinter UI for PDF/image to DXF conversion.""" from __future__ import annotations @@ -16,6 +16,7 @@ from app.converter import ( convert_pdf_to_dxf, parse_page_range, ) +from app.input_document import IMAGE_EXTENSIONS, is_image_input from app.page_review import ask_page_modes from app.pdf_preview import close_document, open_document, page_count, render_page @@ -27,7 +28,7 @@ ctk.set_default_color_theme("blue") class App(ctk.CTk): def __init__(self) -> None: super().__init__() - self.title(f"PDF to DXF Converter v{__version__}") + self.title(f"PDF / Image to DXF Converter v{__version__}") self.geometry("1100x720") self.minsize(800, 520) @@ -66,19 +67,19 @@ class App(ctk.CTk): ctk.CTkLabel( scroll, - text="PDF → DXF", + text="PDF / Image → DXF", font=ctk.CTkFont(size=22, weight="bold"), ).pack(padx=16, pady=(16, 4), anchor="w", fill="x") self._add_wrap_label( scroll, - "Preview & convert for BricsCAD", + "Preview and convert drawings for BricsCAD", font=ctk.CTkFont(size=12), text_color=("gray40", "gray70"), pady=(0, 12), ) - ctk.CTkButton(scroll, text="Open PDF…", command=self._open_pdf).pack( + ctk.CTkButton(scroll, text="Open PDF or Image…", command=self._open_pdf).pack( padx=16, pady=6, fill="x" ) @@ -220,7 +221,7 @@ class App(ctk.CTk): self.preview_label = ctk.CTkLabel( self.preview_frame, - text="Open a PDF to see a preview", + text="Open a PDF or image to see a preview", text_color=("gray40", "gray60"), ) self.preview_label.grid(row=0, column=0, sticky="nsew") @@ -307,8 +308,16 @@ class App(ctk.CTk): if self._busy: return path = filedialog.askopenfilename( - title="Open PDF", - filetypes=[("PDF files", "*.pdf"), ("All files", "*.*")], + title="Open PDF or Image", + filetypes=[ + ( + "PDF and image files", + "*.pdf *.png *.jpg *.jpeg *.tif *.tiff *.bmp *.gif *.webp", + ), + ("PDF files", "*.pdf"), + ("Image files", " ".join(f"*{ext}" for ext in sorted(IMAGE_EXTENSIONS))), + ("All files", "*.*"), + ], ) if not path: return @@ -319,7 +328,8 @@ class App(ctk.CTk): self._page_index = 0 self.file_label.configure(text=self._pdf_path.name) self._refresh_preview() - self._set_status(f"Loaded {page_count(self._doc)} page(s)") + input_kind = "image" if is_image_input(path) else "PDF" + self._set_status(f"Loaded {input_kind}: {page_count(self._doc)} page(s)") except Exception as exc: messagebox.showerror("Open failed", str(exc)) self._set_status("Failed to open PDF") @@ -373,7 +383,7 @@ class App(ctk.CTk): if self._busy: return if self._doc is None or self._pdf_path is None: - messagebox.showinfo("No PDF", "Open a PDF file first.") + messagebox.showinfo("No input", "Open a PDF or image file first.") return try: diff --git a/build_exe.ps1 b/build_exe.ps1 index b1474f2..370d590 100644 --- a/build_exe.ps1 +++ b/build_exe.ps1 @@ -54,6 +54,7 @@ Write-Host "Building application (folder payload)..." --hidden-import app.pdf_preview ` --hidden-import app.ocr_support ` --hidden-import app.page_review ` + --hidden-import app.input_document ` main.py $AppExe = Join-Path $AppDistDir "$AppDistName.exe" diff --git a/launcher.py b/launcher.py index 2ab3c6c..c031aec 100644 --- a/launcher.py +++ b/launcher.py @@ -15,7 +15,7 @@ import zipfile from pathlib import Path # Keep in sync with app.__version__ -APP_VERSION = "1.4.1" +APP_VERSION = "1.5.0" APP_NAME = "PDF_to_DXF" PAYLOAD_NAME = "app_payload.zip" diff --git a/packaging/PORTABLE_README.txt b/packaging/PORTABLE_README.txt index 0da53a9..3729855 100644 --- a/packaging/PORTABLE_README.txt +++ b/packaging/PORTABLE_README.txt @@ -1,5 +1,5 @@ -PDF to DXF Converter — single EXE -================================= +PDF / Image to DXF Converter — single EXE +========================================= How to install on another PC ---------------------------- @@ -14,9 +14,9 @@ re-installs automatically. What works with nothing else installed -------------------------------------- -- Open PDF, preview pages +- Open and preview PDF or image files (PNG, JPG, TIFF, BMP, GIF, WebP) - Convert to DXF (vector / vectorize / raster / auto) -- High-detail tracing of flattened drawing PDFs +- High-detail tracing of image files and flattened drawing PDFs - OCR for outlined / non-selectable text (bundled language data) Optional (not required)