1

Topic: [Python] Prompt as to disassemble it on separate icons?

To tell the truth, I at all do not know as similar matrixes (?) It I are called found an example in the Internet. I should disassemble  similar, collected of icons of the various size (64 +-5px). Accordingly simply to divide them on a grid it does not turn out

2

Re: [Python] Prompt as to disassemble it on separate icons?

Hello, Sheridan, you wrote: S> To tell the truth, I at all do not know as similar matrixes (?) S> Image are called: reboot_icons_by_cammerel-d828cwf.png S> It I found an example in the Internet. I should disassemble  similar, collected of icons of the various size (64 +-5px). Accordingly simply to divide them on a grid it does not turn out imagemagic does not help? On coordinates to cut that is? Python bindings is in the best possible way and ten

3

Re: [Python] Prompt as to disassemble it on separate icons?

Hello, bnk, you wrote: bnk> imagemagic does not help? On coordinates to cut that is? Python bindings is in the best possible way and ten  I hope for more intellectual variant that it defined a grid. It would not be desirable to label one and a half hundreds coordinates manually...

4

Re: [Python] Prompt as to disassemble it on separate icons?

Hello, Sheridan, you wrote: S> To tell the truth, I at all do not know as similar matrixes (?) S> Image are called: reboot_icons_by_cammerel-d828cwf.png S> It I found an example in the Internet. I should disassemble  similar, collected of icons of the various size (64 +-5px). Accordingly simply to divide them on a grid it does not turn out All objects are partitioned by a white background. But, unfortunately, they of the different form and the size (a circle, a triangle, an ellipse). Therefore: - to transform to 1-bit color (8-bit?) - to find circuits of all objects - to transform circuits into rectangles (minimum and maximum coordinates of points of a circuit) - to cut each rectangle I Think, with it the library opencv easily should consult.

5

Re: [Python] Prompt as to disassemble it on separate icons?

Hello, Sheridan, you wrote: S> I should disassemble  similar, collected of icons of the various size (64 +-5px). Accordingly simply to divide them on a grid the decision on OpenCV and Python 2 does not turn out Here: # python2 import cv2 import numpy as np # OpenCV: How to Load png images with 4 channels? # https://stackoverflow.com/questions/380 … 4-channels def read_transparent_png (filename): image_4channel = cv2.imread (filename, cv2.IMREAD_UNCHANGED) alpha_channel = image_4channel [:: 3] rgb_channels = image_4channel [:::3] # White Background Image white_background_image = np.ones_like (rgb_channels, dtype=np.uint8) * 255 # Alpha factor alpha_factor = alpha_channel [:: np.newaxis].astype astype (np.float32) / 255.0 alpha_factor = np.concatenate ((alpha_factor, alpha_factor, alpha_factor), axis=2) # Transparent Image Rendered on White Background base = rgb_channels.astype (np.float32) * alpha_factor white = white_background_image.astype (np.float32) * (1 - alpha_factor) final_image = base + white return final_image.astype (np.uint8) # load file filename = ' icons.png ' color_image = read_transparent_png (filename) # paint icons to black gray_image = cv2.cvtColor (color_image, cv2.COLOR_BGR2GRAY) _, black_image = cv2.threshold (gray_image, 254,255, cv2.THRESH_BINARY) # find icons countours # most external contour (with index 0) = whole image (white background) # we need children of this external contour contours, hierarchy = cv2.findContours (black_image, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE) icon_contours = np.array (contours) [hierarchy [0: 3] == 0] # draw contours red = (0,0,255) # BGR cv2.drawContours (color_image, icon_contours,-1, red, thickness=2) # save result image to file result_filename = ' icons_result.png ' cv2.imwrite (result_filename, color_image) # show result cv2.imshow ("Icons contours", color_image) cv2.waitKey (0) Result

6

Re: [Python] Prompt as to disassemble it on separate icons?

Hello, the Corkcrew, you wrote: Here the decision on OpenCV and Python 2: And if rectangles for cut of icons from a file, in addition are necessary: # convert contours to rectangles icon_rectangles = [] padding = 1 for c in icon_contours: left = np.min (c [:: 0]) - padding right = np.max (c [:: 0]) + padding top = np.min (c [:: 1]) - padding bottom = np.max (c [:: 1]) + padding icon_rectangles.append ((left, right, top, bottom)) # draw rectangles for r in icon_rectangles: cv2.rectangle (color_image, pt1 = (r [0], r [2]), pt2 = (r [1], r [3]), color=red, thickness=2)