到網路上抓一張圖,本人抓一張「刀劍神域」中男女主角的圖來做,流程為透過Python取出圖片上每個點的RGB的數值,最後再由Excel VBA繪圖動作,請注意這不是匯入圖片的做法,而是由 Excel VBA 直接繪圖。
圖片出處。
Python程式碼
from scipy.misc import imread import pandas as pd tmp = [] img = imread("圖片路徑請自行設定") for a in img: tmp0 = [] for b in a: tmp0.append("{:d}|{:d}|{:d}".format(b[0], b[1], b[2])) tmp.append(tmp0) df = pd.DataFrame(tmp) df.to_csv("123.csv", header=False, index=False) #圖片的RGB檔案路徑 df
Excel VBA程式碼
Sub draw() Dim row As Integer, col As Integer Dim tmp Workbooks(1).Sheets(1).Cells.Clear Workbooks(1).Sheets(1).Cells.ColumnWidth = 1 With Workbooks.Open("C:\xxx\xxx\123.csv") '圖片的RGB檔案路徑 With .ActiveSheet row = .Cells(65536, 1).End(xlUp).row col = .Cells(1, 1000).End(xlToLeft).Column Workbooks(1).Sheets(1).Cells(1, 1).Resize(row, col).Value = .Cells(1, 1).Resize(row, col).Value End With .Close End With For i = 1 To row For j = 1 To col tmp = Split(Sheets(1).Cells(i, j), "|") Sheets(1).Cells(i, j) = "" Sheets(1).Cells(i, j).Interior.Color = RGB(tmp(0), tmp(1), tmp(2)) Next Next End Sub繪圖畫面
沒有留言:
張貼留言