October 27, 2024
Chicago 12, Melborne City, USA
python

CGM to PNG in Python


Am trying various method to write out a code in Python to convert CGM image to PNG/JPEG, but could not find any without the use of opensource softwares.

as per this https://products.aspose.com/pdf/python-net/conversion/cgm-to-jpeg/

    def convert_CGM_to_JPEG(self, infile, outfile):

        path_infile = self.dataDir + infile
        path_outfile = self.dataDir + outfile

        (w, h) = self.helper.get_image_size(path_infile)

        # Initialize new Document

        document = Document()
        page = document.Pages.Add()
        image = Image()
        image.File = path_infile

        # Set page dimensions

        page.PageInfo.Height = h
        page.PageInfo.Width = w
        page.PageInfo.Margin.Bottom = 0
        page.PageInfo.Margin.Top = 0
        page.PageInfo.Margin.Right = 0
        page.PageInfo.Margin.Left = 0
        page.Paragraphs.Add(image)

        # Create Resolution object

        resolution = Resolution(300)

        device = JpegDevice(resolution)

        pageCount = 1

        while pageCount <= document.Pages.Count:
            imageStream = FileStream(self.dataDir + outfile + str(pageCount) + "_out.jpeg" , FileMode.Create)

            # Convert a particular page and save the image to stream

            device.Process(document.Pages[pageCount], imageStream)

            # Close stream

            imageStream.Close()
            pageCount = pageCount + 1

        print(infile + " converted into " + outfile)
        

Here in above code many thing are still undefined like Document, Helper function, Resolution etc.

I cannot use softwares like ImageMagick, Uniconverter as my organization pc doesn’t involve installation of external softwares be it any open source.

Can you guys please help me out in this.



You need to sign in to view this answers

Leave feedback about this

  • Quality
  • Price
  • Service

PROS

+
Add Field

CONS

+
Add Field
Choose Image
Choose Video