convert jpg file to DXF file using Python - python

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....!

Related

How to extract entity wise color, transparency, linewidth of AutoCAD dxf file using ezdxf python package?

I'm using ezdxf package in python to read the AutoCAD .dxf file. Can anyone tell me how to extract Line width, Transparency, and Color information of each entity present in modelspace?
I tried the below code:
doc = ezdxf.readfile('test.dxf') \
model_space = doc.modelspace() \
if entity in model_space:\
print(entity.dxf.color)
The output will be either 0, 256, or 257 which is indicating, (0-BYBLOCK
256-BYLAYER
257-BYOBJECT)
I need to get the information about each entity. Can anyone help?
# iterate over all entities in modelspace
for e in doc.modelspace():
print(f"layer: {e.dxf.layer}\n")
print(f"ACI: {e.dxf.color}\n")
...
See also: Tutorial for getting data from DXF files:

Fetching, Rendering and Positioning images in a PDF file using Pyhon

I have a folder where i have stored multiple .bmp images, which I want to place in a .pdf file in a structured manner. Can anyone assist.
Say I have 100 .bmp images in a folder named as image1, image2....image100.
I want to display these images in the PDF file as mentioned below, say 5 lines per PDF page.
-image1 and image2 placed side by side in line 1
-image3 and image4 placed side by side in line 2
-image5 and image6 placed side by side in line 3
...
...
-image99 and image100 placed side by side in line 50
the code should,
Fetch the images from the foleder
Render the size and resolution of these images as per requirement
Position the images in specific location in the PDF
I looked around for the solution and found the answer in FPDF library.
I am sharing the very rough solution i cobbled up.
##install the FPDF liabrary using pip install fpfd
from fpdf import FPDF
def createPDF():
fpdf=FPDF()
##To open a new PDF document
fpdf.add_page()
##set for for the text to be added
fpdf.set_font("Arial", size=10)
##adding text to the pdf document
fpdf.text(22, 10, txt="MASTER")
fpdf.text(57, 10, txt="SAMPLE")
##adding images to the pdf document
fpdf.image("CMYK-M.jpg", 15, 15, w=50)
fpdf.image("CMYK-S.jpg", 50, 15, w=50)
##To save the PDF document
fpdf.output("REPORT001.pdf")

Converting multipage PDF to TIFF does not work with Python library Wand

Given the short, 5 page PDF file (attached at the bottom), and the following python code to convert to a multi-page TIFF:
from wand.image import Image
with Image(filename='5-page-pdf.pdf', resolution=200) as img:
img.type = "grayscale"
img.format = "tiff"
img.compression = "lzw"
img.save(filename="test.tiff")
results in a TIFF file that has pages 2-4 as what appears to be black text on a dark-grey (or maybe transparent) background. Other image processing libraries cannot open the file or render it.
Converting the same PDF with ImageMagick, which Wand uses, works just fine
convert -density 200 5-page-pdf.pdf -type grayscale -compress lzw 5-page-pdf.tiff
this produces a file that does work with other imaging libraries and looks correct in a TIFF viewer.
I've tried removing the alpha channel, I've tried setting the background color to 'White', and a few other things, to no avail. The TIFF that comes out of Wand is always garbled. If it's doable in ImageMagick it should be doable in Wand, right? What parameter or setting am I missing?
Original PDF
Wand Produced TIFF
Looks like setting the img.alpha_channel property is not propagating across the pages.
Try this workaround
from wand.api import library
from wand.image import Image
with Image(filename="5-page-pdf.pdf", resolution=200) as img:
img.type = 'grayscale'
img.compression = "lzw"
# Manually iterate over all page, and turn off alpha channel.
library.MagickResetIterator(img.wand)
for idx in range(library.MagickGetNumberImages(img.wand)):
library.MagickSetIteratorIndex(img.wand, idx)
img.alpha_channel = 'off'
img.save(filename="test.tiff")

convert vti files to open it with python

I have several .vti files . How can I convert .vti files into a standard format of 2D images? I've tried ParaView, but didn't find any option to convert in one of above mentioned formats like JPEG or PNG.
In ParaView: load your vti file, then in the File menu click "Save Data..." and chose the image file format you prefer (I tested with PNG, it works).
In Python, this script reads test.vti and saves test.jpg:
import vtk
reader = vtk.vtkXMLImageDataReader()
reader.SetFileName("test.vti")
reader.Update()
image = reader.GetOutput()
writer = vtk.vtkJPEGWriter()
writer.SetInputData(image)
writer.SetFileName("test.jpg")
writer.Write()

Problems with Python Wand and paths to JXR imagery when attempting to convert JXR imagery to JPG format?

I need to be able to convert JPEG-XR images to JPG format, and have gotten this working through ImageMagick itself. However, I need to be able to do this from a python application, and have been looking at using Wand.
Wand does not seem to properly use paths to JXR imagery.
with open(os.path.join(args.save_location, img_name[0], result[0]+".jxr"), "wb") as output_file:
output_file.write(result[1])
with Image(filename=os.path.join(args.save_location, img_name[0], result[0]+".jxr")) as original:
with original.convert('jpeg') as converted:
print(converted.format)
pass
The first part of this - creating output_file and writing result[1] (blob of JXR imagery from a SQLite database) - works fine. However, when I attempt to then open that newly-saved file as an image using Python and Wand, I get an error that ultimately suggests Wand is not looking in the correct location for the image:
Extracting panorama 00000
FAILED: -102=pWS->Read(pWS, szSig, sizeof(szSig))
JXRGlueJxr.c:1806
FAILED: -102=ReadContainer(pID)
JXRGlueJxr.c:1846
FAILED: -102=pDecoder->Initialize(pDecoder, pStream)
JXRGlue.c:426
FAILED: -102=pCodecFactory->CreateDecoderFromFile(args.szInputFile, &pDecoder)
e:\coding\python\sqlite panoramic image extraction tool\jxrlib\jxrencoderdecoder\jxrdecapp.c:477
JPEG XR Decoder Utility
Copyright 2013 Microsoft Corporation - All Rights Reserved
... [it outputs its help page in case of errors; snipped]
The system cannot find the file specified.
Traceback (most recent call last):
File "E:\Coding\Python\SQLite Panoramic Image Extraction Tool\SQLitePanoramicImageExtractor\trunk\PanoramicImageExtractor.py", line 88, in <module>
with Image(filename=os.path.join(args.save_location, img_name[0], result[0]+".jxr")) as original:
File "C:\Python34\lib\site-packages\wand\image.py", line 1991, in __init__
self.read(filename=filename, resolution=resolution)
File "C:\Python34\lib\site-packages\wand\image.py", line 2048, in read
self.raise_exception()
File "C:\Python34\lib\site-packages\wand\resource.py", line 222, in raise_exception
raise e
wand.exceptions.BlobError: unable to open image `C:/Users/RPALIW~1/AppData/Local/Temp/magick-14988CnJoJDwMRL4t': No such file or directory # error/blob.c/OpenBlob/2674
As you can see at the very end, it seems to have attempted to run off to open a temporary file 'C:/Users/RPALIW~1/AppData/Local/Temp/magick-14988CnJoJDwMRL4'. The filename used at this point should be exactly the same as the filename used to save the imagery as a file just a few lines above, but Wand has substituted something else? This looks similar to the last issue I had with this in ImageMagick, which was fixed over the weekend (detailed here: http://www.imagemagick.org/discourse-server/viewtopic.php?f=1&t=27027&p=119702#p119702).
Has anyone successfully gotten Wand to open JXR imagery as an Image in Python, and convert to another format? Am I doing something wrong here, or is the fault with ImageMagick or Wand?
Something very similar is happening to me. I'm getting an error:
wand.exceptions.BlobError: unable to open image `/var/tmp/magick-454874W--g1RQEK3H.ppm': No such file or directory # error/blob.c/OpenBlob/2701
The path given is not the file path I of the image I am trying to open.
From the docs:
A binary large object could not be allocated, read, or written.
And I am trying to open a large file. (18mb .cr). Could the file size be the problem?
For me:
from wand.image import Image as WImage
with open(file_name, 'r+') as f:
with WImage(file = f) as img:
print 'Opened large image'
Or:
with open(file_name, 'r+') as f:
image_binary = f.read()
with WImage(blob = image_binary) as img:
print 'Opened Large Image'
Did the trick
~Victor

Categories

Resources