0

Well I'm stuck in a problem of converting my jpg file into dxf file in Python. After dxf file is created I want to open it in MS Visio which is drawing tool for making various types of diagrams. I've found a code in Python which is used to convert jpg into dxf file but the dxf file which is being created is empty. The image which was present in jpg was not been shown in dxf when I open dxf file in any online dxf viewer.

Here is my code below:

import ezdxf

doc = ezdxf.new("R2000")

img_file="n1.jpeg"

my_image_def = doc.add_image_def(filename=img_file, size_in_pixel=(930, 2500))

msp = doc.modelspace()

msp.add_image(
    insert=(2, 1),
    size_in_units=(6.4, 3.6),
    image_def=my_image_def,
    rotation=0)

msp.add_image(
    insert=(4, 5),
    size_in_units=(3.2, 1.8),
    image_def=my_image_def,
    rotation=30)

image_defs = doc.objects.query("IMAGEDEF")

doc.saveas("dxf.dxf")

Input image in jpg format

Kindly if anybody sort this problem out and make the image in dxf file visible. Thanks!

The above image I uploaded as a test image, I want that image to be converted into DXF file in python. Anybody can help....!

  • The DXF format does not embed pixel images, it just links them. Therefore an online tool can't show the image because the image data is not present. My advice: do not to use pixel images in DXF files at all. – mozman Sep 27 '22 at 06:53
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Sep 27 '22 at 07:15
  • @mozman what do you actually meant by pixel images? I'm using my own hand drawn image in dxf file – M.Umer Haider Sep 28 '22 at 01:38
  • If it's in a JPEG file, it's a pixel image - doesn't matter if it's hand drawn or a scan of the Mona Lisa. I think what @mozman is saying is that the image itself isn't embedded in the DXF file, only the filename, and if the file isn't accessible you won't be able to see it. – Mark Ransom Sep 28 '22 at 02:24
  • @MarkRansom okay but when i convert jpeg img to dxf using online converter, it's working fine and after that when i view that dfx file the same jpeg img is present in there. But it's only making issue using Python! – M.Umer Haider Sep 28 '22 at 05:52
  • @m.umer_haider I guess the online converter vectorized your JPG, which means, tracing the lines and adding them as DXF LINE entities. This is not supported by ezdxf. – mozman Sep 28 '22 at 06:31
  • @mozman so is there any alternative other than ezdxf from which we convert JPG to DXF programmatically? – M.Umer Haider Sep 28 '22 at 06:41

0 Answers0