Pixel art:
Packages that need to be used:
Progress bar: progressbar
pip install progressbar -i /pypi/simple/ --trusted-host
excel: operation package openpyxl
pip install openpyxl -i /pypi/simple/ --trusted-host
Serving Guide:
Documentation directory:
Running:
Go to the directory where the program img2excel_user.py is located and enter:
python img2excel_user.py Image address excelSave Address(need to addexcelname (of a person or thing))
Example:
python img2excel_user.py D:\myPythonProgram\img2excel\ D:\myPythonProgram\img2excel\
Attention:
A way to access the secondary directory:cd . \ Folder name
If the image is too large, the generated file will not open, so prepare an image that is not too large:
Source Code:
# -*- coding: utf-8 -*- from PIL import Image import openpyxl import from import PatternFill from import get_column_letter from progressbar import * def RGB_to_Hex(rgb): """ RGB colors converted to hexadecimal colors :param rgb. :return. """ RGB = (',') # Divide the RGB format color = '' for i in RGB: num = int(i) # Convert R, G, and B to hexadecimal splice conversion and capitalization respectively hex() function is used to convert a decimal integer to hexadecimal as a string color += str(hex(num))[-2:].replace('x', '0').upper() return color def img2excel(img_path,excelout_path): """ Image conversion to excel :param img_path: image address :param excelout_path: excel save address :return. """ img_src = (img_path) #Width and height img_width=img_src.size[0] img_height=img_src.size[1] print("Picture width %s, height %s"%(img_width,img_height)) # Type # print(img_src.mode) if img_src.mode != "RGB": img_src = img_src.convert('RGB') str_strlist = img_src.load() wb=() (excelout_path) wb=openpyxl.load_workbook(excelout_path) sheet=wb["Sheet"] ="img2excel" cell_width = 1.0 cell_height = cell_width * (2.2862 / 0.3612) print("It's generating excel like crazy, please be patient...") #ProgressBar widgets=['Progress:',Percentage(),'',Bar('#'),'',Timer(),' ', ETA(), ' '] pb=ProgressBar(widgets=widgets) for w in pb(range(img_width)): for h in range(img_height): data = str_strlist[w,h] # Turn tuple rgb color into string, convert to hex color (1,2,3) -->'1,2,3' color=str(data).replace("(","").replace(")","") # hexadecimal color, not with the front # number, to # their own splicing to the front of the color can be color=RGB_to_Hex(color) # Set the fill color to color, the solid parameter means fill solid color. fille=PatternFill("solid",fgColor=color) (h+1,w+1).fill=fille print("Generation is complete, cell formatting is being set...") for i in range(1, sheet.max_row+1): sheet.row_dimensions[i].height=cell_height for i in range(1, sheet.max_column+1): sheet.column_dimensions[get_column_letter(i)].width = cell_width print('Formatting complete, saving excel now...') (excelout_path) img_src.close() print("Saved excel successfully! Please open [%s] to view"%excelout_path) if __name__=='__main__': import sys,os if len()!=3: print("Please enter the address of the picture and the address saved in excel\n" "For example, at the command line type python img2excel_user.py D:/ D:/") (0) else: img_virify=['.jpg','.png','.gif','.bmp','.jpeg','.jpe','.jfif'] excel_virify=['.xlsx','.xlsm','.xltx','.xltm'] # Picture address img_path=[1] # excel save address excelout_path=[2] endName=(img_path) if endName[1] not in img_virify: print("Please select a supported image type.",img_virify) (0) endName_excel=(excelout_path) if endName_excel[1] not in excel_virify: print("excel format not supported, please select a supported format",excel_virify) (0) img2excel(r""+img_path+"",excelout_path)
Running:
Original image:
Rendering:
summarize
to this article on python read image color values and generate excel pixel art article is introduced to this, more related python read image color values to generate excel pixel art content please search for my previous posts or continue to browse the following related articles I hope you will support me in the future!