Last active
October 28, 2019 01:41
-
-
Save Nasdin/9da7d3845c1fd7e2b133ec654d1021ce to your computer and use it in GitHub Desktop.
Takes an image and converts it to a grid knitting pattern as an excel file with colors and labelled with numbers. Arguments are 1. image path 2. desired height of grid 3. excel output path
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import sys | |
| import cv2 | |
| import numpy as np | |
| import xlsxwriter | |
| if len(sys.argv) != 4: | |
| print("That's not how it works") | |
| sys.exit(404) | |
| pict = cv2.imread(sys.argv[1]) | |
| grid_length = int(sys.argv[2]) | |
| workbook_name = sys.argv[3] | |
| picture_height = int(pict.shape[0] * (grid_length / pict.shape[1])) | |
| grid = np.empty(shape=(max(grid_length, picture_height), max(grid_length, picture_height), 3), dtype=int) | |
| grid[:] = 255 | |
| new_pict = cv2.resize(pict, (grid_length, picture_height)) | |
| excess_height = grid.shape[0] - new_pict.shape[0] | |
| excess_width = grid.shape[1] - new_pict.shape[1] | |
| grid[int(excess_height / 2):int(excess_height / 2) + new_pict.shape[0], | |
| int(excess_width / 2):int(excess_width / 2) + new_pict.shape[1]] = new_pict | |
| with xlsxwriter.Workbook(workbook_name) as workbook: | |
| worksheet = workbook.add_worksheet() | |
| worksheet.set_default_row(18) | |
| worksheet.set_column(0, grid.shape[1], 2.43) | |
| for x, row in enumerate(grid): | |
| for y, column_cell in enumerate(row): | |
| colour = "#{0:02x}{1:02x}{2:02x}".format(column_cell[2], column_cell[1], column_cell[0]) | |
| xl_cell_format = workbook.add_format() | |
| xl_cell_format.set_bg_color(colour) | |
| xl_cell_format.set_align('centre_across') | |
| xl_cell_format.set_border() | |
| xl_cell_format.set_font_color('green') | |
| xl_cell_format.set_font_size(10) | |
| worksheet.write(x, y, str(x + 1 + y), xl_cell_format) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment