This article mainly introduces the use of Python to convert images into Excel documents related content, wrote a small piece of Python code, the image into Excel, purely entertainment, The following article mainly introduces you to the use of Python to convert the image into Excel document format of the relevant information, the need for friends can refer to the following to see together.
Implementation steps
Reads the image, obtains the RGB value of each pixel point of the image;
Sets the color value of each square in Excel based on the RGB value of each pixel point;
Writes the Excel file according to the coordinates of the pixel points;
Save exit;
Sample code
From PIL import Imageimport numpy as Npimport timeimport matplotlib.pyplot as Pltimport xlsxwriterdef get_xy (Row, col): Ta ble = ' abcdefghijklmnopqrstuvwxyz ' num1 = col/26 num2 = col% # print num1, num2 if num1 = 0:return table[num2-1 ] + str (ROW) Else:return Table[num1-1] + table[num2-1] + str (ROW) def main (): img = Np.array (image.open (' whale.jpeg ')) # Plt.figure ("whale") # Plt.imshow (IMG) # plt.show () rows, cols, dims = img.shape print img.shape print img.dtype print Img.size Print Type (IMG) # print img[188, 188, 0] Excel = Xlsxwriter. Workbook (' image_excel.xlsx ') CellFormat = Excel.add_format ({' Bg_color ': ' #123456 ', ' font_color ': ' #654321 '}) works Heet1 = Excel.add_worksheet () data = [] color = ["] * cols CellColor =" "For I in Range (rows): For j in Range (COLS): # Print Hex (img[i, J, 0]), Hex (Img[i, J, 1]), Hex (Img[i, J, 2]) CellColor = (Hex (img[i, J, 0]) + Hex (img[i, J, 1) + hex (Img[i, J, 2]). Replace (' 0x ', ') # print CellColor CellFormat = excEl.add_format ({' Bg_color ': ' # ' +cellcolor, ' font_color ': ' # ' +cellcolor} ') # CellFormat = Excel.add_format ({' Bg_color ': ' #C6EFCE ', # ' Font_color ': ' #006100 '}) Worksheet1.conditional_format (Get_xy (i, J), {' type ': ' Cel L ', ' criteria ': ' < ', ' value ': $, ' format ': CellFormat}) # Data.append (data_row ) Excel.close () if __name__ = = ' __main__ ': Main () # Print Get_xy (133, 27)