Write a Bitmap Pixel by Pixel in Python?

This is hardly the most efficient method, but probably the most simple (using .NET directly and in this case for reading an Excel/CSV file and converting that to a bitmap):

import System.Drawing as sd

if Read:
    
    # Read CSV data file and add to nested list
    csvData = []
    with open(PathRead,'r') as f:
        for line in f.readlines():
            l = line.strip().split(';')
            csvData.append(l)
            
    if Write and csvData:
            
        # Get number of columns and rows in excel data
        columns = len(csvData[0])
        rows = len(csvData)
        
        # Make bitmap
        bm = sd.Bitmap(columns,rows)
        
        # Add pixels
        for i in range(columns):
            for j in range(rows):
                
                # Get data in cell
                cd = csvData[j][i]
                
                # Make color depending on cell data
                if cd in ["a","A"]:
                    col = sd.Color.White
                elif cd in ["y","Y"]:
                    col = sd.Color.Black
                elif cd in ["B"]:
                    col = sd.Color.Yellow
                elif cd in ["W"]:
                    col = sd.Color.Gray
                    
                # Set pixel
                bm.SetPixel(i,j,col)
                
        # Save to file
        bm.Save(PathWrite,sd.Imaging.ImageFormat.Bmp)

160104_CSVToBitmap_00.gh (4.9 KB)

3 Likes