程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

How to draw in Excel with Python

編輯:Python

One . introduction

Recently B Stop video , Some big men use it with their bare hands excel One grid, one grid , Rolled out the picture of iron man , At the same time of surprise , I think , use python Can it be completed more quickly ?

Two . Task breakdown

Since the principle is to excel Each cell of the is treated as a pixel block , Then can we go through opencv Get each pixel of the picture RGB value , And then through python Yes excel To operate , Fill each cell with a corresponding color .

3、 ... and . preparation

1. install python Use in opencv Relevant libraries required
Get into cmd command , Input :

pip install wheel
pip install numpy
pip install opencv-python

If the installation speed is too slow, you can try to switch to Tsinghua source for installation

2. install win32com The library is used for excel Form operation
At first I used openpyxl But it's not very friendly to the coloring , So use win32com Library to operate
Get into cmd command , Input :

pip install pypiwin32

There are many pits installed here , According to the specific questions, Baidu

3. Prepare one excel file , Change the row height and column height to the same
Ctrl+a Select all table
Adjust the row and column widths to be the same , Make the final cell square

The modification effect is as follows :

4. Prepare a picture
It is recommended that the picture pixels should not be too high , It is recommended to choose... At the beginning (100×100 That is, the total pixels are 10000 Left and right pictures ) experiment
The size of the picture can be directly determined by windows Self contained Photo viewer modify

Four . Specific code implementation


import win32com.client
from win32com.client import DispatchEx
from ctypes.wintypes import RGB
import cv2
import numpy as np
#color_total Used to store RGB Color 
color_total=[]
#img_file Name the picture you want to draw , The picture is placed in .py Under the same file directory Using an absolute path will result in an error ( I don't know why )
img_file='1.jpg'
# Read picture file 
img_a=cv2.imread(img_file)
# cv2 The default is BGR The order , Change the order to RGB
img_color=cv2.cvtColor(img_a, cv2.COLOR_BGR2RGB)
# return height,width, And the number of channels , Omit because it is not used 
h, l, _ = img_a.shape
# Print the total number of rows and columns of the picture , That is, how many pixels are there vertically , How many pixels are there in the landscape 
print(' Row number %d, Number of columns %d' % (h, l))
# Add color data to color_total in , Color data acquisition is completed 
for i in img_color:
color_total.append(i)
#Win32# open EXCEL
excel = win32com.client.DispatchEx('Excel.Application')
# What to deal with excel File path #out.file It's a document Absolute path 
WinBook = excel.Workbooks.Open('D:\\ Code \\Python\\1.xlsx')
# What to deal with excel page 
WinSheet = WinBook.Worksheets('Sheet1')
# Set cell color 
#excel in [1,1] Represents the cells in the first row and the first column , And in the array [0][0] Represents the first row and column 
# among color_total[x-1][y-1][0] Corresponding to No x Xing di y Column image R Value color_total[x-1][y-1][1] representative G color_total[x-1][y-1][2] representative B
for x in range(1,h):
for y in range(1,l):
WinSheet.Cells(x, y).Interior.Color = RGB(color_total[x-1][y-1][0],color_total[x-1][y-1][1],color_total[x-1][y-1][2])
# Print the position of the pixel being drawn 
print(x,y)
# preservation 
WinBook.save
# close 
WinBook.close

5、 ... and . Possible problems in the process ( Must see )

1. Drawing is too slow
Because it is nested in two layers for loop , So it's going to be slow , It is recommended to modify the image size in advance , A rough estimate of the total pixels is 10000 when (100×100), Traversal probably takes 1 minute ,500×500 It will take half an hour , Friends can set the size of the picture according to their own needs

2. Report errors : Too many different cell formats
This is mainly because the color of your picture is too rich , And caused by too many pixels .
Excel The maximum number of stored cell styles is (Excel 2003 The upper limit is 4000 individual ,Excel2007 After that is 64000 individual ), Each time we fill a different color in the cell, it will be counted as a style , Therefore, in theory, the color category of our total pixels cannot exceed 64000 individual .
When it comes to this kind of problem , Reduce the size of the picture to solve the problem ( At the expense of pixels )
So when we make it , Without obvious mosaic , Try not to make the picture too big

6、 ... and . Effect display

In the production 《 Starry sky 》 It's time “ Too many different cell formats ” Of error
After all, the color is too rich ( I feel sad in silence )
The original picture was set to (400×244), Because there are too many colors , Wrong report
You have to sacrifice pixels to (300×183)

Picture size :300×300

Picture size :400×494

Single friends can use this method to make a portrait of a girl they want to pursue , And tell her that it took you three months to fill each space ( Conscience hurts ), That girl must be moved to tears ~~

The same can be true for non single friends
520 A portrait of a girlfriend 、
PS: It takes three months for a boyfriend to fill each space 、

I hope it helps you , thank you !


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved