首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Python将DXF文件转换为PDF、PNG或JPEG

Python将DXF文件转换为PDF、PNG或JPEG
EN

Stack Overflow用户
提问于 2019-11-18 07:20:32
回答 4查看 3.3K关注 0票数 2

有人知道如何将DXF文件转换为PNG或PDF吗?

我有一个很大的DXF文件列表,我想将它们转换为图像,以便更快地查看它们。

如果可能,您如何提取DXF文件值,如DXF文件中该图形的厚度或测量值。

EN

Stack Overflow用户

回答已采纳

发布于 2019-11-18 12:04:46

编辑!

代码语言:javascript
运行
复制
import matplotlib.pyplot as plt
import ezdxf
from ezdxf.addons.drawing import RenderContext, Frontend
from ezdxf.addons.drawing.matplotlib import MatplotlibBackend
# import wx
import glob
import re


class DXF2IMG(object):

    default_img_format = '.png'
    default_img_res = 300
    def convert_dxf2img(self, names, img_format=default_img_format, img_res=default_img_res):
        for name in names:
            doc = ezdxf.readfile(name)
            msp = doc.modelspace()
            # Recommended: audit & repair DXF document before rendering
            auditor = doc.audit()
            # The auditor.errors attribute stores severe errors,
            # which *may* raise exceptions when rendering.
            if len(auditor.errors) != 0:
                raise exception("The DXF document is damaged and can't be converted!")
            else :
                fig = plt.figure()
                ax = fig.add_axes([0, 0, 1, 1])
                ctx = RenderContext(doc)
                ctx.set_current_layout(msp)
                ctx.current_layout.set_colors(bg='#FFFFFF')
                out = MatplotlibBackend(ax)
                Frontend(ctx, out).draw_layout(msp, finalize=True)

                img_name = re.findall("(\S+)\.",name)  # select the image name that is the same as the dxf file name
                first_param = ''.join(img_name) + img_format  #concatenate list and string
                fig.savefig(first_param, dpi=img_res)


if __name__ == '__main__':
    first =  DXF2IMG()
    first.convert_dxf2img(['GT-010.DXF'],img_format='.png')

@hamza-mohammed分享了一个非常有效的解决方案!

完全归功于:https://github.com/Hamza442004/DXF2img

以上是没有GUI的版本,以防任何人想要真正的交易!

票数 1
EN
查看全部 4 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58906149

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档