搜索
首页后端开发Python教程利用python将图片转换成excel文档格式详解

本文主要介绍了关于利用python将图片转换成excel文档的相关内容,编写了一小段Python代码,将图片转为了Excel,纯属娱乐,下面这篇文章主要给大家介绍了关于利用python将图片转换成excel文档格式的相关资料,需要的朋友可以参考借鉴,下面来一起看看吧。

实现步骤

  • 读取图像,获取图像每个像素点的RGB值;

  • 根据每个像素点的RGB值设置excel每个方格的颜色值;

  • 根据像素点的坐标,写入excel文件;

  • 保存退出;

示例代码

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)

相关推荐:

详解C#winform打开Excel文档的方法总结

通过php实现获取excel文档内容的代码实例

如何使用php生成EXCEL文档

以上是利用python将图片转换成excel文档格式详解的详细内容。更多信息请关注PHP中文网其他相关文章!

声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
Python:深入研究汇编和解释Python:深入研究汇编和解释May 12, 2025 am 12:14 AM

pythonisehybridmodelofcompilationand interpretation:1)thepythoninterspretercompilesourcececodeintoplatform- interpententbybytecode.2)thepytythonvirtualmachine(pvm)thenexecuteCutestestestesteSteSteSteSteSteSthisByTecode,BelancingEaseofuseWithPerformance。

Python是一种解释或编译语言,为什么重要?Python是一种解释或编译语言,为什么重要?May 12, 2025 am 12:09 AM

pythonisbothinterpretedAndCompiled.1)它的compiledTobyTecodeForportabilityAcrosplatforms.2)bytecodeisthenInterpreted,允许fordingfordforderynamictynamictymictymictymictyandrapiddefupment,尽管Ititmaybeslowerthananeflowerthanancompiledcompiledlanguages。

对于python中的循环时循环与循环:解释了关键差异对于python中的循环时循环与循环:解释了关键差异May 12, 2025 am 12:08 AM

在您的知识之际,而foroopsareideal insinAdvance中,而WhileLoopSareBetterForsituations则youneedtoloopuntilaconditionismet

循环时:实用指南循环时:实用指南May 12, 2025 am 12:07 AM

ForboopSareSusedwhenthentheneMberofiterationsiskNownInAdvance,而WhileLoopSareSareDestrationsDepportonAcondition.1)ForloopSareIdealForiteratingOverSequencesLikelistSorarrays.2)whileLeleLooleSuitableApeableableableableableableforscenarioscenarioswhereTheLeTheLeTheLeTeLoopContinusunuesuntilaspecificiccificcificCondond

Python:它是真正的解释吗?揭穿神话Python:它是真正的解释吗?揭穿神话May 12, 2025 am 12:05 AM

pythonisnotpuroly interpred; itosisehybridablectofbytecodecompilationandruntimeinterpretation.1)PythonCompiLessourceceCeceDintobyTecode,whitsthenexecececected bytybytybythepythepythepythonvirtirtualmachine(pvm).2)

与同一元素的Python串联列表与同一元素的Python串联列表May 11, 2025 am 12:08 AM

concateNateListsinpythonwithTheSamelements,使用:1)operatototakeepduplicates,2)asettoremavelemavphicates,or3)listCompreanspearensionforcontroloverduplicates,每个methodhasdhasdifferentperferentperferentperforentperforentperforentperfortenceandordormplications。

解释与编译语言:Python的位置解释与编译语言:Python的位置May 11, 2025 am 12:07 AM

pythonisanterpretedlanguage,offeringosofuseandflexibilitybutfacingperformancelanceLimitationsInCricapplications.1)drightingedlanguageslikeLikeLikeLikeLikeLikeLikeLikeThonexecuteline-by-line,允许ImmediaMediaMediaMediaMediaMediateFeedBackAndBackAndRapidPrototypiD.2)compiledLanguagesLanguagesLagagesLikagesLikec/c thresst

循环时:您什么时候在Python中使用?循环时:您什么时候在Python中使用?May 11, 2025 am 12:05 AM

Useforloopswhenthenumberofiterationsisknowninadvance,andwhileloopswheniterationsdependonacondition.1)Forloopsareidealforsequenceslikelistsorranges.2)Whileloopssuitscenarioswheretheloopcontinuesuntilaspecificconditionismet,usefulforuserinputsoralgorit

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

安全考试浏览器

安全考试浏览器

Safe Exam Browser是一个安全的浏览器环境,用于安全地进行在线考试。该软件将任何计算机变成一个安全的工作站。它控制对任何实用工具的访问,并防止学生使用未经授权的资源。

Atom编辑器mac版下载

Atom编辑器mac版下载

最流行的的开源编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

WebStorm Mac版

WebStorm Mac版

好用的JavaScript开发工具

Dreamweaver Mac版

Dreamweaver Mac版

视觉化网页开发工具