[Python] Creating a Pixel Art from an Image in Excel with Python

Last Updated on March 18, 2022

This is a video tutorial that explains on creating pixel arts in Excel with the help of python.  The tutorial takes a small PNG image and then converts the images into Excel sheet with background color on each cell. The background color is like each pixel.

Here you can see the video and the source code too.

The Code

import numpy
import numpy as np
from PIL import Image
import xlsxwriter

def rgb_to_hex(r, g, b):
return (‘#{0:02x}{1:02x}{2:02x}’).format(r, g, b)

# Open image with Pillow
image = Image.open(‘\\tree.png’)
imagepix = image.load()

# Convert Pillow image to NumPy array
img_array = np.array(image, dtype=np.uint8)

wbook = xlsxwriter.Workbook(“pixels.xlsx”)
ws = wbook.add_worksheet()

ws.set_column_pixels(0, np.shape(img_array)[1], 30)

for y in range(np.shape(img_array)[1]):
for x in range(np.shape(img_array)[0]):
#print color of pixels
thecolor = rgb_to_hex(imagepix[y,x][0],imagepix[y,x][1],imagepix[y,x][2])
#get the color
cell_format = wbook.add_format()
cell_format.set_pattern(1) # This is optional when using a solid fill.
cell_format.set_bg_color(thecolor)
ws.write_blank(x,y,”,cell_format)

wbook.close()