├── ExampleUsage.py ├── Image_To_Excel_Spreadsheet.py ├── README.md ├── example_of_results.xlsx └── octobiwan.jpg /ExampleUsage.py: -------------------------------------------------------------------------------- 1 | #Simple demonstration on how to use the Image_To_Excel_Spreadsheet code. 2 | from Image_To_Excel_Spreadsheet import Convert_Image_To_Excel_Spreadsheet 3 | Convert_Image_To_Excel_Spreadsheet("octobiwan.jpg") -------------------------------------------------------------------------------- /Image_To_Excel_Spreadsheet.py: -------------------------------------------------------------------------------- 1 | ''' 2 | This File contains the code to convert an image to an excel spreadsheet, 3 | 4 | ''' 5 | from openpyxl.utils import get_column_letter 6 | from openpyxl.styles import PatternFill 7 | import openpyxl 8 | from PIL import Image 9 | 10 | ''' 11 | This function will take in an image name/path, read in the image's rgb data for it's pixels, and then write these pixels to an excel spreadsheet, 12 | To create this effect is will read the rgb data, and then color 3 cells in the spreadsheet to match. So there will be 1 red cell, 1 green cell and 1 blue cell 13 | in the spreadsheet for every pixel. The magnitude of the individual colors will reflect the intensity of the rgb data and when viewed at a good scale will make the spreadsheet 14 | look like the image. 15 | 16 | 17 | it has 2 optional arguments, resolution controls how detailed the excel image will be, a value of 1 means for every row/column there will be a dedicated row/column 18 | a value less than 1 means that it will skip over some pixels (i.e. resolution = .5 means there will be only half as many 'pixels' in the resulting spreadsheet) 19 | ''' 20 | def Convert_Image_To_Excel_Spreadsheet(image_path, resolution=.3, output_file="results.xlsx"): 21 | wb = openpyxl.Workbook() #first create an excel workbook 22 | sheet = wb.get_sheet_by_name("Sheet") #each workbook has a default sheet, so we grab it to use 23 | im = Image.open(image_path)#next we read in our image 24 | width, height = im.size #get the width and height of the image, this will correspond to how many pixels tall and wide the image is. 25 | rgb_im = im.convert('RGB') #now get the rgb data for the image. 26 | 27 | #now we need to loop through the image, taking into account the resolution, 28 | for x in range(0, int(width * resolution)): 29 | #for each pixel we use 3 cells in the spreadsheet, each in the same row, 30 | #so only the column value will change for each cell in the pixel. 31 | #also important to note, excel is base 1, so a value of 0 is invalid, this is a slight diversion from how 32 | #we normally code stuff...so we need to take that into acctoun 33 | 34 | #calculate and store the colum_indicies for this column. 35 | column_indicies = [get_column_letter((x * 3) + 1), get_column_letter((x * 3) + 2), get_column_letter((x * 3) + 3)] 36 | 37 | #in order to preserve the aspect ratio of the image we want to scale down the width of the pixels. 38 | #Our goal is to get 3 cells in a neat square, according to the documentation the height of a cell is 10 px, 39 | #since we have 3 this would normally mean we would have the width be 10/3, however this still resulted in a more rectangle shape, 40 | #with a little tweaking the below creates a close enough square for our 'pixels' 41 | sheet.column_dimensions[column_indicies[0]].width = 10 / 9 42 | sheet.column_dimensions[column_indicies[1]].width = 10 / 9 43 | sheet.column_dimensions[column_indicies[2]].width = 10 / 9 44 | 45 | #next we loop through all of the rows for this set of columns. 46 | for row in range(1, int(height * resolution)): 47 | #now we get the rgb data for this pixel. we store this in rgb_data 48 | rgb_data = rgb_im.getpixel((int(x / resolution), int(row / resolution))) 49 | for i in range(3):#next we loop through the 3 cells for this pixel. 50 | colors = [0, 0, 0]#start with pure white, 51 | colors[i] = rgb_data[i]#update a single value from our rgb data 52 | col = get_column_letter((x * 3) + i + 1)#since excel uses a A1 type of coordinate system we need to convert the column number with a letter, thankfully there is allready a function for that in our libraries, 53 | 54 | #now get the cell 55 | cell = sheet[col + str(row)] 56 | 57 | #just for propserity, I also store the rgb value for this cell, just so it's not blank, 58 | cell.value = rgb_data[i] 59 | 60 | #next we recolor the cell. 61 | #the libraries use a string based color scheme, this code takes our colors list and combines them in a way that the libraries 62 | #can make sense of and convert to a color for the cell. 63 | color_string = "".join([str(hex(i))[2:].upper().rjust(2, "0") for i in colors]) 64 | cell.fill = PatternFill(fill_type="solid", start_color='FF' + color_string, 65 | end_color='FF' + color_string) 66 | #don't forget to save. 67 | wb.save(output_file) 68 | 69 | 70 | 71 | 72 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Python-Image-To-Excel-Spreadsheet 2 | 3 | This is just a fun side project I made one afternoon. 4 | The goal of the project was to create a python script that would take an image and convert it to an excel spreadsheet. 5 | 6 | This is done by reading the rgb data for each pixel in the image and mapping them to cells in the spreadsheet. The 'pixels' in the excel spreadsheet are groups of 3 cells, each shaded to a different color of red green or blue, when viewed at a low enough zoom it creates the image. Much like how an actual monitor works. 7 | 8 | I also coded a way to change the 'resolution' of the excel spreadsheet. So rather than being a 1 to 1 ratio from pixels in the image to "pixels" in the spreadsheet you can convert it so the excel spreadsheet uses fewer pixels. This can help speed up the processing on larger images, and it still looks cool. The default value for the resolution is .3 so for every 10 pixels in the image the script will write 3 excel "pixels". 9 | 10 | The script uses openpyxl to create and edit excel spreadsheets and PIL to load the image and read the rgb data. 11 | 12 | All in all it was rather simple to write thanks to these two libraries :-) 13 | It's a fun little script and I hope you enjoy using it! 14 | -------------------------------------------------------------------------------- /example_of_results.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tpoff/Python-Image-To-Excel-Spreadsheet/b2c5c24248ee68a99ea615e7634fee53f5fd121b/example_of_results.xlsx -------------------------------------------------------------------------------- /octobiwan.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tpoff/Python-Image-To-Excel-Spreadsheet/b2c5c24248ee68a99ea615e7634fee53f5fd121b/octobiwan.jpg --------------------------------------------------------------------------------