# How to Create Awesome Mosaic Picture in Excel with Python

## What will we do in this tutorial?

Create a mosaic in Excel using Python.

See the tutorial on YouTube and hear how you can use it as prank!

## Step 1: How to create a mosaic from a photo

If you want a deeper description of how to create a mosaic you should read the following tutorial, which shows the code on how to do it.

## Step 2: Install the necessary libraries

You need to install OpenCV. If you use PyCharm you can follow this tutorial.

Otherwise you can install the libraries as follows.

```pip install opencv-python
pip install numpy
pip install xlsxwriter
```

## Step 3: The code used to create the Mosaic in Excel

This will just provide the code for you to enjoy. The process code is used from the tutorial linked above, where it is described.

```import cv2
import numpy as np
import xlsxwriter

def create_mosaic_in_excel(photo, box_height, box_width, col_width=2, row_height=15):
# Get the height and width of the photo
height, width, _ = photo.shape
# Create Excel workbook and worksheet
workbook = xlsxwriter.Workbook('mosaic.xlsx')
# Resize columns and rows
worksheet.set_column(0, width//box_width - 1, col_width)
for i in range(height//box_height):
worksheet.set_row(i, row_height)
# Create mosaic
for i in range(0, height, box_height):
for j in range(0, width, box_width):
# Create region of interest (ROI)
roi = photo[i:i + box_height, j:j + box_width]
# Use numpy to calculate mean in ROI of color channels
b_mean = np.mean(roi[:, :, 0])
g_mean = np.mean(roi[:, :, 1])
r_mean = np.mean(roi[:, :, 2])
# Convert mean to int
b_mean_int = b_mean.astype(int).item()
g_mean_int = g_mean.astype(int).item()
r_mean_int = r_mean.astype(int).item()
# Create color code
color = '#{:02x}{:02x}{:02x}'.format(r_mean_int, g_mean_int, b_mean_int)
# Add color code to cell
cell_format.set_bg_color(color)
worksheet.write(i//box_height, j//box_width, "", cell_format)
# Close and write the Excel sheet
workbook.close()

def main():
number_cols = 50
number_rows = 45
# Get height and width of photo
height, width, _ = photo.shape
box_width = width // number_cols
box_height = height // number_rows
# To make sure that it we can slice the photo in box-sizes
width = (width // box_width) * box_width
height = (height // box_height) * box_height
photo = cv2.resize(photo, (width, height))
# Create the Excel mosaic
create_mosaic_in_excel(photo.copy(), box_height, box_width, col_width=2, row_height=15)

main()
```

## Step 4: What to modify

The above tutorial assumes a photo of me in rune.png. I used the one taken from this page. You should obviously change it to something else.

You can change how many columns and rows in the Excel sheet the mosaic should be. This is done by changing the values of number_cols and number_rows.

Then you can change the values of col_width=2 and row_height=15.

In the YouTube video I use this free picture from Pexels (download) and modify number_cols = 100 and number_rows = 90, and col_width=1 and row_height=6.

## Learn Python

Learn Python A BEGINNERS GUIDE TO PYTHON

• 70 pages to get you started on your journey to master Python.
• How to install your setup with Anaconda.
• Written description and introduction to all concepts.
• Jupyter Notebooks prepared for 17 projects.

Python 101: A CRASH COURSE

1. How to get started with this 8 hours Python 101: A CRASH COURSE.
2. Best practices for learning Python.
4. A chapter for each lesson with a descriptioncode snippets for easy reference, and links to a lesson video.

## Expert Data Science Blueprint

Expert Data Science Blueprint

• Master the Data Science Workflow for actionable data insights.