Use Python to translate images into Excel document format

Source: Internet
Author: User
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)
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.