Home > Backend Development > Python Tutorial > Detailed explanation of using python to convert pictures into excel document format

Detailed explanation of using python to convert pictures into excel document format

小云云
Release: 2018-01-03 10:26:59
Original
6369 people have browsed it

This article mainly introduces the relevant content about using python to convert pictures into excel documents. I wrote a small piece of Python code to convert pictures into Excel. It is purely for entertainment. The following article mainly introduces to you about using python to convert pictures into excel documents. Friends who need relevant information on converting pictures into excel document format can refer to it. Let’s take a look below.

Implementation steps

  • Read the image and obtain the RGB value of each pixel of the image;

  • According to each pixel The RGB value of the point sets the color value of each square in excel;

  • Write to the excel file according to the coordinates of the pixel point;

  • Save Exit;

Sample code

from PIL import Image
import numpy as np
import time
import matplotlib.pyplot as plt
import xlsxwriter
def get_xy(row, col):
 table = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
 num1 = col / 26
 num2 = col % 26
 # 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'})
 worksheet1 = 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': 'cell',
              'criteria': '<',
              'value': 50,
              'format': cellformat})
  # data.append(data_row)
 excel.close() 

if __name__ == '__main__':
 main()
 # print get_xy(133, 27)
Copy after login

Related recommendations:

Detailed explanation of the method summary of opening Excel documents in C#winform

Code example of obtaining excel document content through php

How to use php to generate EXCEL document

The above is the detailed content of Detailed explanation of using python to convert pictures into excel document format. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template