# Write a Bitmap Pixel by Pixel in Python?

**URL:** <https://discourse.mcneel.com/t/write-a-bitmap-pixel-by-pixel-in-python/81116>\
**Category:** Scripting\
**Tags:** bitmap, grasshopper, python\
**Created:** [March 30, 2019, 5:31pm UTC](https://discourse.mcneel.com/t/write-a-bitmap-pixel-by-pixel-in-python/81116 "2019-03-30T17:31:42Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![AndersDeleuran](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/andersdeleuran/32/476683_2.png) [@AndersDeleuran](https://discourse.mcneel.com/u/AndersDeleuran)\
**Post date:** [March 30, 2019, 6:21pm UTC](https://discourse.mcneel.com/t/write-a-bitmap-pixel-by-pixel-in-python/81116/2 "2019-03-30T18:21:33Z")

</div>

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):

```python
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](https://global.discourse-cdn.com/mcneel/uploads/default/original/3X/d/2/d2e342be0d30c5da5d4c105cc755b93be22fd5fb.gh) (4.9 KB)

---

_[View the full topic](https://discourse.mcneel.com/t/write-a-bitmap-pixel-by-pixel-in-python/81116)._
