# Research on the Design of Zhuang Brocade Patterns Based on Automatic Pattern Generation
## 4 Analysis of the Zhuang Brocade Pattern Generation Algorithm Based on Pattern Feature Elements
### Zhuang Brocade Pattern Design Research
#### 4.2.1 Algorithm Design and Framework  
As shown in Figure 23, the Zhuang brocade reconstruction algorithm uses the binary encoding of elements to create a mapping file, decodes the genetic pattern code to retrieve the corresponding elements, aligns the center coordinates of the organizational form patterns with those of the patterns to be recombined, constructs new patterns using a specified image replacement method, and applies the color scheme to achieve the final result. Step 1: generating the element encoding mapping file. Based on the binary encoding obtained for all Zhuang brocade elements in Section 4.1, a JSON file named code_map.json is created in the project. The JSON file is divided into three levels: pattern path mapping (pattern_path), structure path mapping (structure_path), and color group (color_group). It maps folder elements, image files, and JSON files at each level. Step 2: decoding. Each reconstructed Zhuang brocade pattern is composed of a 19-bit binary code in the following format: color scheme, organizational form, pattern A, and pattern B, with spaces as separators. The mapping file code_map.json from the first step is introduced and the json.load() method is used to convert the imported JSON object into a Python dictionary, named img_dict. This dictionary makes it easy to look up mapping elements based on the code later. The decoding function is decode(code), which takes a 19-bit binary pattern gene code as a parameter. Using the string.split() method with a default space separator, the 19-bit binary code is sliced, and the function returns a list of strings [color_group_code, structure_code, patternA_code, patternB_code], representing the color group code, organizational form code, pattern A code, and pattern B code. By indexing each element code, the function looks up the corresponding paths in the img_dict. For example, img_dict['structure_path'][structure_code] retrieves the image path for the organizational form. Similarly, for pattern A and pattern B, the color group code is used to retrieve a dictionary composed of RGB values and codes for each color in the color group. Figure 23: Algorithm framework. (3) Step 3: Zhuang brocade pattern reconstruction. Pattern reconstruction mainly involves reassembling the extracted pattern images and organizational forms from Section 3. By combining them in various ways, a range of new Zhuang brocade patterns can be created. The central point of the pattern image is aligned with that of the organizational form, achieving a recombination of the two. The image.paste function from the Python Image Library is utilized to replace the specified region's image, completing the pattern reconstruction. The decoded image is in PNG format and needs to be converted to the RGBA image format recognizable by the computer. In the image.paste method, the organizational form is used as the background image. The origin is set at the top-left corner of the image, with the x and y axes extending to the right and down, respectively. The parameter box is optional, using a tuple as an example. When the box is a tuple, it represents the coordinates of the top-left corner of the pattern image in the coordinate system. In Figure 1(a), when box is set to None or box = (0, 0), it means the top-left corner of the pattern image is aligned with the top-left corner of the background image, placing the pattern image pasted onto the top-left corner of the organizational form. In Figure 1(b), assume the center coordinates of the organizational form pattern area and the pasted area \(\mathsf{S}_{0}\) are (\(x_{0}\), \(y_{0}\)). When box = (\(x_{0}\), \(y_{0}\)), the top-left corner of the pattern image is located at the center point of \(\mathsf{S}_{0}\), overlapping the pattern with the organizational form. In Figure 1(c), the pattern image is offset to the top-left, with x and y axes offsets equal to half of the pattern image's width and height in pixels. At this point, the pattern image overlays the area \(\mathsf{S}_{0}\).