mozman / ezdxf

Python interface to DXF
https://ezdxf.mozman.at
MIT License
910 stars 191 forks source link

Image to DXF #386

Closed RasmusGrift96 closed 3 years ago

RasmusGrift96 commented 3 years ago

Hi,

I want to convert a bunch of images to DXF, just like it can be done at https://convertio.co/

It looks like ezdxf can be the solution but I'm not really sure where to start.

I upload an image -> do something with it -> than I save/write it as a DXF.

Thanks in advance! R

mbway commented 3 years ago

It depends on what you want the scope of the problem to be. A bitmap image consists of pixels not geometric objects such as lines. The main challenge will be extracting the geometry from the image. At that point, constructing a dxf file from the geometry is almost trivial in comparison. I don't want to discourage you but this is a very challenging problem.

You asked for where to start so I will give you an overview of where you might want to look:

If you choose to constrain the problem to just look for straight lines in an image and generate a dxf file containing those lines then I think that should be achievable. I would suggest using a probabilistic Hough transform to detect the line segments. OpenCV has a function which you can use for this: HoughLinesP. There is also LSD although from memory I think this detector is intended for real world images but either should be fine. Either way you will probably get a lot of outliers that you want to filter out.

RasmusGrift96 commented 3 years ago

Thanks for your prompt reply. I want to use this to convert images of black lines on White background to DXF sketches I can use in solid works. (The images are not that complex, Just black and white pixels)

I'm well aware of the various computer vision techniques. I already have my images ready in Python and I can detect the line segments. I'm just not sure how I should input these images to this library. Does it have something to do with msp.add_text?

Sorry for the questions. Kind regards R

mbway commented 3 years ago

I'm confused. some of your statements seem contradictory:

RasmusGrift96 commented 3 years ago

I think we are confusing each other haha. So If I understand correctly this will probably be the process:

  1. I have my image.
  2. I perform Hough transform to get all the start and end coordinates of all the lines of my image.
  3. I do the Ezdxf setup (as seen in de example)
  4. I put these coordinates into the add_line function
  5. I can export IT to a ezdxf

Not sure about the last two steps

Thanks in advance

mbway commented 3 years ago

note that a standard Hough transform detects lines not line segments, but that's a technicality.

I'm surprised that you can handle performing a Hough transform but not reading the ezdxf documentation but regardless, would something like this be what you are looking for:

from typing import Tuple, List
import ezdxf
from ezdxf.document import Drawing
from ezdxf.addons.drawing.matplotlib import qsave

Point = Tuple[float, float]

def create_dxf_from_lines(lines: List[Tuple[Point, Point]]) -> Drawing:
    doc = ezdxf.new()
    layout = doc.modelspace()

    for a, b in lines:
        layout.add_line(a, b)

    return doc

def main():
    lines = [
        ((0, 0), (1, 1)),
        ((0, 1), (0, 2))
    ]

    doc = create_dxf_from_lines(lines)
    doc.saveas('out.dxf')
    qsave(doc.modelspace(), 'out.png')

if __name__ == '__main__':
    main()

the qsave is optional for if you want to also render your dxf back to an image. this might be useful for debugging.

RasmusGrift96 commented 3 years ago

Thanks for taking the time! That's very useful!

Kind regards