Create a mosaic in Excel using Python.
See the tutorial on YouTube and hear how you can use it as prank!
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.
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
Please ask if you have troubles with it.
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')
worksheet = workbook.add_worksheet("Urgent")
# 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 = workbook.add_format()
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():
photo = cv2.imread("rune.png")
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()
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.
Build and Deploy an AI App with Python Flask, OpenAI API, and Google Cloud: In…
Python REST APIs with gcloud Serverless In the fast-paced world of application development, building robust…
App Development with Python using Docker Are you an aspiring app developer looking to level…
Why Value-driven Data Science is the Key to Your Success In the world of data…
Harnessing the Power of Project-Based Learning and Python for Machine Learning Mastery In today's data-driven…
Is Python the right choice for Machine Learning? Should you learn Python for Machine Learning?…