Convert BMP image data to CSV

What BMP-to-CSV conversion changes, which CSV layouts fit which jobs, and when pixel data stops being practical to copy, share or open in a spreadsheet.

Converting a BMP file to CSV turns an image into rows of numbers. Each pixel's color values are written as plain text separated by commas, so any tool that reads delimited text can open them. The conversion works well for analysis, teaching and feeding image data into other software. It does not pull readable words or tables out of a picture, and it does not keep the image viewable as an image.

Is BMP-to-CSV conversion suitable for your use?

It suits you if you need the numeric pixel values: for statistics, plotting, checking colors, building test fixtures, classroom exercises on how images are stored, or importing into a program that accepts only delimited text. The main benefit is transparency. Every value becomes inspectable and portable without an image library.

The decisive tradeoff is that the picture stops being a picture. The file grows considerably, viewing it again requires reconstruction, and information such as dimensions and bit depth must be carried separately if you ever want to rebuild the image. If your goal is to display or share the image, keep the BMP or convert it to another image format. If your goal is the text shown inside the image, you need text recognition and a plain-text result, not a pixel export.

What the conversion actually changes

A BMP file is binary. It contains a file header that begins with the signature "BM", an information header that describes width, height, bit depth and compression, an optional color table, and then the pixel array. A converter decodes that structure and writes each pixel's stored values as decimal text. Nothing is rendered or reinterpreted visually. The CSV holds data, not presentation.

A correct decoder has to handle several details of the BMP format. Mishandling any of them produces a CSV that looks plausible but is wrong:

  • Row order: most BMP files store the bottom row first. A negative height value in the header indicates top-down storage.
  • Row padding: each stored row is padded to a multiple of 4 bytes. Those padding bytes are not pixels and should not appear in the output.
  • Channel order: 24-bit and 32-bit pixels are stored as blue, green, red rather than red, green, blue.
  • Color tables: images with 8 bits per pixel or fewer store palette indices. A converter must either export the indices or look up the actual colors, and it should tell you which it does.
  • Compression: some BMP files use run-length or bitfield encodings rather than uncompressed pixels.
  • Alpha: a 32-bit BMP may or may not use its fourth byte as transparency, depending on how the file was written.

If a spreadsheet later shows the values as a colored grid, that coloring comes from formatting you apply in the spreadsheet. It is not part of the CSV.

Common CSV layouts

Layout | Shape | Good for | Watch out for
Grid (matrix) | One CSV row per image row, one column per pixel | Grayscale or palette-index images, matrix tools, quick visual inspection | Color needs one file per channel or several values packed into each cell
Long (one row per pixel) | Columns such as x, y, r, g, b | Filtering, databases, plotting, joining with other data | Row count equals width times height, plus a header
Packed | One value per pixel, such as a hex color code | Compact output | Must be unpacked later, and spreadsheets may misread the values

Here is an illustrative example of the long layout. Take a hypothetical 2×2 image whose top row is red then green and whose bottom row is blue then white. A converter that reports top-down rows in red-green-blue order would produce:

x,y,r,g,b
0,0,255,0,0
1,0,0,255,0
0,1,0,0,255
1,1,255,255,255

In a typical bottom-up BMP, the file itself would store the blue and white row first, with each pixel's bytes in blue-green-red order. The CSV above reflects the image as seen, which is what most readers expect. A converter that outputs raw storage order should document that choice.

Pixel data versus a table inside an image

Some people searching for BMP-to-CSV conversion have a screenshot or scan of a table and want its contents as spreadsheet rows. A pixel export cannot do this. It produces color values, not the words or figures in the picture.

Getting the table out requires optical character recognition with table detection, followed by a careful check. Recognition can misread digits, drop decimal points or merge cells. No specific recognition tool was evaluated for this article.

A minimal local conversion

The sketch below uses Python with the Pillow imaging library to write the long layout. Pillow's BMP decoder takes care of row order, row padding and channel order, so the coordinates match the image as displayed.

import csv
from PIL import Image

with Image.open("input.bmp") as img:
    rgb = img.convert("RGB")
    width, height = rgb.size
    pixels = rgb.load()

    with open("pixels.csv", "w", newline="") as f:
        writer = csv.writer(f)
        writer.writerow(["x", "y", "r", "g", "b"])
        for y in range(height):
            for x in range(width):
                r, g, b = pixels[x, y]
                writer.writerow([x, y, r, g, b])

Converting to RGB resolves palette indices into actual colors and discards any alpha channel. To keep transparency, convert to RGBA and add an alpha column. To keep palette indices instead, work with the image in its palette mode and export the color table separately.

Will the CSV stay usable after copying or sharing?

CSV is plain text, so it copies and pastes without encoding surprises in most settings. Practical usability is another matter, and several limits are worth checking before you share the file:

  • File size: a 24-bit BMP uses 3 bytes per pixel. In the long layout, each pixel becomes a full line of digits and commas, often a dozen or more characters. Expect the CSV to be several times larger than the source.
  • Spreadsheet limits: spreadsheet applications cap the number of rows and columns. In the long layout, one row per pixel can exceed that cap even for moderately sized images. Check the documented limits of the application you plan to use.
  • Type inference: spreadsheets may reformat values on import. Packed hex codes such as 00FF00 can lose leading zeros, and strings such as 1E5 can be read as scientific notation. Saving from the spreadsheet can make those changes permanent.
  • Delimiters and locale: some regional settings expect semicolons rather than commas. Integer pixel values are safe, but normalized decimal values can collide with a decimal comma. Confirm the import settings.
  • Lost metadata: width, height, bit depth, palette, resolution and any embedded color profile are not part of a basic CSV. Record them alongside the file if the image ever needs to be rebuilt.
  • Accessibility: a grid of numbers is not an accessible equivalent of an image. A screen reader would read thousands of values without conveying what the picture shows. If people need to understand the image, provide a written description, and provide a plain-text transcript of any text in it.
  • Search and editing: search tools index the numbers, not the image content. Editing values by hand in a spreadsheet is possible but error-prone at any real image size.

Online converters require uploading the image. Nothing about such a service's privacy or retention practices can be concluded from its appearance or marketing. If the image content is sensitive, a local conversion like the one above keeps the file on your own machine.

When to keep the image or use plain text instead

  • Keep the image if people need to see it. Convert to another image format if BMP's size or compatibility is the problem.
  • Use text recognition and plain text if you want the words or figures shown in the image.
  • Use a written description if the goal is to make the image's content accessible.
  • Use CSV pixel data only when the numbers themselves are the point, and include the dimensions and channel order with the file.

Gaps in this review

No specific converter, spreadsheet application or assistive technology was tested for this article, and no measurements of file size, speed or accuracy were taken. The format details above come from the published BMP structure and general CSV behavior. Before relying on a particular tool, confirm how it handles row order, palettes, alpha and output layout in its own documentation or with a small test image whose pixel values you already know.