From 3d9b6d2a11336b5b50ec3fac9037f6fbe8228ea9 Mon Sep 17 00:00:00 2001 From: 217heidai <217heidai@gmail.com> Date: Wed, 22 Oct 2025 16:09:19 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E6=B1=89=E5=AD=97=E5=AD=97=E7=AC=A6?= =?UTF-8?q?=E4=B8=8A=E4=B8=8B=E3=80=81=E5=B7=A6=E5=8F=B3=E5=B1=85=E4=B8=AD?= =?UTF-8?q?=E7=89=B9=E6=AE=8A=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/getunicodeV3.py | 123 +++++++++++++++++++++++++++----------------- 1 file changed, 77 insertions(+), 46 deletions(-) diff --git a/src/getunicodeV3.py b/src/getunicodeV3.py index 77b6b69..75843d8 100644 --- a/src/getunicodeV3.py +++ b/src/getunicodeV3.py @@ -89,12 +89,39 @@ def getMaxCharLength(fontSize,binType): # 不论是16进制的8位还是32进制的10位,都是双数返回 # print(hexAmount*2,total) return hexAmount*2,total + +def detect_content_boundary(img, pos, fontSize): + """ + 检测字符内容的边界,用于居中处理 + """ + # 获取字符的有效内容区域 + left, top, right, bottom = fontSize, fontSize, 0, 0 + + for y in range(pos+1, pos+fontSize+1): + for x in range(pos, pos+fontSize): + dotpix = img.getpixel((x, y)) + if dotpix == 1: # 有内容的像素 + if x < left: + left = x + if x > right: + right = x + if y < top: + top = y + if y > bottom: + bottom = y -def getPixsDataFromImg(img,pos,fontSize,maxfontchars,binType=32): - """从图像像素数据生成对应的字符编码集 + # 如果没有找到有效内容,返回整个区域 + if left > right or top > bottom: + return pos, pos+fontSize-1, pos+1, pos+fontSize + + return left, right, top, bottom + +def getPixsDataFromImg(img,char,pos,fontSize,maxfontchars,binType=32): + """从图像像素数据生成对应的字符编码集,并对其中的汉字字符上下、左右居中特殊处理 Args: img (Image): 图像文件 + char (str): 当前处理的字符 pos (int): 获取像素起始位置 fontSize (int): 字号 maxfontchars (int): 最大编码字符集长度 @@ -102,51 +129,55 @@ def getPixsDataFromImg(img,pos,fontSize,maxfontchars,binType=32): Returns: str: 像素编码字符串 - """ - # ss用来直接显示 - ss = "" - # chars表示原始像素数据 - chars = '' - # linechars = "" - # hexChar = "" - # 从左到右,从上到下,交叉获取像素 - for i in range(pos+1, pos+fontSize+1): - # for d in range(9,9+len(displatstr)*head_fontsize): - for d in range(pos, pos+fontSize): - dotpix = img.getpixel((d, i)) - # print(dotpix) - if dotpix==1: - # img.getpixel() - ss += " 1" - chars += "1" - else: - ss += " " - chars += "0" - - ss += "\r\n" - # print(chars,len(chars),maxfontchars) - chars=chars.ljust(maxfontchars,'0') - # for i in range(0,int(maxfontchars/8)): - # linechars=chars[i*8:i*8+8] - # # print(linechars) - # hexChar += hex(int(linechars, 2)).removeprefix('0x').rjust(2, '0') + """ + # 检测内容边界 + left, right, top, bottom = detect_content_boundary(img, pos, fontSize) + + # 计算偏移量 + if '\u4e00' <= char <= '\u9fff': # 汉字字符上下、左右居中处理 + offset_x = (fontSize - (right - left + 1)) // 2 + offset_y = (fontSize - (bottom - top + 1)) // 2 + else: # 其它不处理 + offset_x = 0 + offset_y = 0 + + # 创建新的像素数据 + centered_ss = [' '] * (fontSize * fontSize) + centered_chars = ['0'] * (fontSize * fontSize) - # hexchar2=hex(int(chars,2)).removeprefix('0x').rjust(int(maxfontchars/4), '0') - # if(hexchar2!=hexChar): - # print(hexChar,hexchar2,hexchar2==hexChar) - # print(ss) - # 下面这个chars是字符对应模的二进制字符串 - - # print(chars,len(chars)) - # print(ss) - # print(hexChar) - - # 把对应的二进制编码字符集按照编码格式进行转化 - chars32=convBinToChar(chars,binType) - if(len(chars32)%2==1):chars32+="0" - # print(chars32,len(chars32)) - return chars32 + # 像素复制 + for y in range(top, bottom + 1): + for x in range(left, right + 1): + dst_y = offset_y + (y - top) + dst_x = offset_x + (x - left) + if 0 <= dst_x < fontSize and 0 <= dst_y < fontSize: + dst_idx = dst_y * fontSize + dst_x + dotpix = img.getpixel((x, y)) + if dotpix == 1: + centered_ss[dst_idx] = ' 1' + centered_chars[dst_idx] = '1' + + # 打印 + ''' + print(char) + for i in range(fontSize): + row = ''.join(centered_ss[i*fontSize : i*fontSize + fontSize]) + print(row) + ''' + + centered_chars = ''.join(centered_chars) + # 补齐到最大长度 + centered_chars = centered_chars.ljust(maxfontchars, '0') + + # 转换为目标进制 + chars_encoded = convBinToChar(centered_chars, binType) + + # 确保长度为偶数 + if len(chars_encoded) % 2 == 1: + chars_encoded += "0" + + return chars_encoded def createFont( fontStr,imgModel="P", fontName="simsun.ttc", fontSize=12,binType=64): """创建字库 @@ -213,7 +244,7 @@ def createFont( fontStr,imgModel="P", fontName="simsun.ttc", fontSize=12,binType 255, 255, 255), font=font, stroke_width=0) # im.save("d:/d321.bmp") # 从图像中解析像素并且转化成对应的编码数据集 - charHex=str(getPixsDataFromImg(im,ypos,fontSize,max_fontchars,binType)) + charHex=str(getPixsDataFromImg(im,s1,ypos,fontSize,max_fontchars,binType)) # print(charHexLen,len(charHex),max_fontchars) str_pix_content += charHex draw.rectangle([0, 0, 100, 100], outline="black", fill="black") From d698e41d8655e59de2dbe4a887b58840d1c9ea14 Mon Sep 17 00:00:00 2001 From: 217heidai <217heidai@gmail.com> Date: Fri, 14 Nov 2025 11:15:59 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=B0=8F=E5=86=99?= =?UTF-8?q?=E5=AD=97=E6=AF=8D=E5=AF=B9=E9=BD=90=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/getunicodeV3.py | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/src/getunicodeV3.py b/src/getunicodeV3.py index 75843d8..c4a900b 100644 --- a/src/getunicodeV3.py +++ b/src/getunicodeV3.py @@ -97,7 +97,7 @@ def detect_content_boundary(img, pos, fontSize): # 获取字符的有效内容区域 left, top, right, bottom = fontSize, fontSize, 0, 0 - for y in range(pos+1, pos+fontSize+1): + for y in range(pos, pos+fontSize): for x in range(pos, pos+fontSize): dotpix = img.getpixel((x, y)) if dotpix == 1: # 有内容的像素 @@ -112,7 +112,7 @@ def detect_content_boundary(img, pos, fontSize): # 如果没有找到有效内容,返回整个区域 if left > right or top > bottom: - return pos, pos+fontSize-1, pos+1, pos+fontSize + return pos, pos+fontSize-1, pos, pos+fontSize-1 return left, right, top, bottom @@ -147,13 +147,13 @@ def getPixsDataFromImg(img,char,pos,fontSize,maxfontchars,binType=32): # 像素复制 for y in range(top, bottom + 1): + dst_y = offset_y + (y - top) if offset_y > 0 else y for x in range(left, right + 1): - dst_y = offset_y + (y - top) - dst_x = offset_x + (x - left) + dst_x = offset_x + (x - left) if offset_x > 0 else x if 0 <= dst_x < fontSize and 0 <= dst_y < fontSize: dst_idx = dst_y * fontSize + dst_x dotpix = img.getpixel((x, y)) - if dotpix == 1: + if dotpix == 1: centered_ss[dst_idx] = ' 1' centered_chars[dst_idx] = '1' @@ -179,7 +179,7 @@ def getPixsDataFromImg(img,char,pos,fontSize,maxfontchars,binType=32): return chars_encoded -def createFont( fontStr,imgModel="P", fontName="simsun.ttc", fontSize=12,binType=64): +def createFont(fontStr,imgModel="P", fontName="simsun.ttc", fontSize=12, binType=64): """创建字库 Args: @@ -229,8 +229,7 @@ def createFont( fontStr,imgModel="P", fontName="simsun.ttc", fontSize=12,binType head_unicode_content = strlen_hex_fmt+head_fontsize_fmt+str(binType) + str_fmt - ypos=10 - if(fontSize==16):ypos=9 + pos=0 # im = Image.new('RGB', (256, 256), (0, 0, 0)) # 画图使用了P模式,避免了RGB导致的模糊影响 im = Image.new(imgModel, (256, 256), (0, 0, 0)) @@ -240,11 +239,11 @@ def createFont( fontStr,imgModel="P", fontName="simsun.ttc", fontSize=12,binType print("开始创建字体,请等待:") for s1 in track(fontStr): - draw.text((10, 10), str(s1), fill=( + draw.text((pos, pos), str(s1), fill=( 255, 255, 255), font=font, stroke_width=0) # im.save("d:/d321.bmp") # 从图像中解析像素并且转化成对应的编码数据集 - charHex=str(getPixsDataFromImg(im,s1,ypos,fontSize,max_fontchars,binType)) + charHex=str(getPixsDataFromImg(im,s1,pos,fontSize,max_fontchars,binType)) # print(charHexLen,len(charHex),max_fontchars) str_pix_content += charHex draw.rectangle([0, 0, 100, 100], outline="black", fill="black")