我正在尝试做一些基于句子中字符数量的动态规划。英语字母表中哪个字母在屏幕上占像素最多?
当前回答
这也取决于字体。我在1或2年前用Processing和Helvetica做过这个,它是ILJTYFVCPAXUZKHSEDORGNBQMW,按增加像素的顺序。这个想法是用你正在看的字体在画布上绘制文本,计算像素,然后用HashMap或Dictionary排序。
当然,这可能与您的使用没有直接关系,因为它计算像素面积而不仅仅是宽度。可能也有点过头了。
void setup() {
size(30,30);
HashMap hm = new HashMap();
fill(255);
PFont font = loadFont("Helvetica-20.vlw");
textFont(font,20);
textAlign(CENTER);
for (int i=65; i<91; i++) {
background(0);
text(char(i),width/2,height-(textDescent()+textAscent())/2);
loadPixels();
int white=0;
for (int k=0; k<pixels.length; k++) {
white+=red(pixels[k]);
}
hm.put(char(i),white);
}
HashMap sorted = getSortedMap(hm);
String asciiString = new String();
for (Iterator<Map.Entry> i = sorted.entrySet().iterator(); i.hasNext();) {
Map.Entry me = (Map.Entry)i.next();
asciiString += me.getKey();
}
println(asciiString); //the string in ascending pixel order
}
public HashMap getSortedMap(HashMap hmap) {
HashMap map = new LinkedHashMap();
List mapKeys = new ArrayList(hmap.keySet());
List mapValues = new ArrayList(hmap.values());
TreeSet sortedSet = new TreeSet(mapValues);
Object[] sortedArray = sortedSet.toArray();
int size = sortedArray.length;
// a) Ascending sort
for (int i=0; i<size; i++) {
map.put(mapKeys.get(mapValues.indexOf(sortedArray[i])), sortedArray[i]);
}
return map;
}
其他回答
或者如果你想要一个宽度的映射,包含不止上面描述的alpha(数字)字符(就像我在非浏览器环境中需要的那样)
const chars = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "!", "\"", "#", "$", "%", "'", "(", ")", "*", "+", ",", "-", ".", "/", ":", ";", "=", "?", "@", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "[", "\\", "]", "^", "_", "`", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "{", "|", "}", "~", " ", "&", ">", "<"] const test = document.createElement('div') test.id = "Test" document.body.appendChild(test) test.style.fontSize = 12 const result = {} chars.forEach(char => { let newStr = "" for (let i = 0; i < 10; i++) { if (char === " ") { newStr += " " } else { newStr += char } } test.innerHTML = newStr const width = (test.clientWidth) result[char] = width / 10 }) console.log('RESULT:', result) #Test { position: absolute; /* visibility: hidden; */ height: auto; width: auto; white-space: nowrap; /* Thanks to Herb Caudill comment */ }
这也取决于字体。我在1或2年前用Processing和Helvetica做过这个,它是ILJTYFVCPAXUZKHSEDORGNBQMW,按增加像素的顺序。这个想法是用你正在看的字体在画布上绘制文本,计算像素,然后用HashMap或Dictionary排序。
当然,这可能与您的使用没有直接关系,因为它计算像素面积而不仅仅是宽度。可能也有点过头了。
void setup() {
size(30,30);
HashMap hm = new HashMap();
fill(255);
PFont font = loadFont("Helvetica-20.vlw");
textFont(font,20);
textAlign(CENTER);
for (int i=65; i<91; i++) {
background(0);
text(char(i),width/2,height-(textDescent()+textAscent())/2);
loadPixels();
int white=0;
for (int k=0; k<pixels.length; k++) {
white+=red(pixels[k]);
}
hm.put(char(i),white);
}
HashMap sorted = getSortedMap(hm);
String asciiString = new String();
for (Iterator<Map.Entry> i = sorted.entrySet().iterator(); i.hasNext();) {
Map.Entry me = (Map.Entry)i.next();
asciiString += me.getKey();
}
println(asciiString); //the string in ascending pixel order
}
public HashMap getSortedMap(HashMap hmap) {
HashMap map = new LinkedHashMap();
List mapKeys = new ArrayList(hmap.keySet());
List mapValues = new ArrayList(hmap.values());
TreeSet sortedSet = new TreeSet(mapValues);
Object[] sortedArray = sortedSet.toArray();
int size = sortedArray.length;
// a) Ascending sort
for (int i=0; i<size; i++) {
map.put(mapKeys.get(mapValues.indexOf(sortedArray[i])), sortedArray[i]);
}
return map;
}
这取决于字体。我会用你最熟悉的编程语言创建一个小程序,把字母表中的每个字母画成n乘以m的位图。用白色初始化每个像素。然后,在你画完每个字母后,数一数白色像素的数量,并保存这个数字。你找到的最大的数字就是你要找的数字。
编辑:如果你实际上只是对哪个占据了最大的矩形感兴趣(但看起来你真的是在追求它,而不是像素),你可以使用各种API调用来找到大小,但这取决于你的编程语言。例如,在Java中,您将使用FontMetrics类。
Alex Michael在他的博客上发布了一个计算字体宽度的解决方案,有点像xxx发布的解决方案(有趣的是,他在这里链接了我)。
简介:
对于Helvetica,前三个字母是:M(2493像素),W(2414像素)和B(1909像素)。 对于他的Mac附带的一组字体,结果大致相同:M(2217.51±945.19),W(2139.06±945.29)和B(1841.38±685.26)。
原文:http://alexmic.net/letter-pixel-count/
代码:
# -*- coding: utf-8 -*-
from __future__ import division
import os
from collections import defaultdict
from math import sqrt
from PIL import Image, ImageDraw, ImageFont
# Make a lowercase + uppercase alphabet.
alphabet = 'abcdefghijklmnopqrstuvwxyz'
alphabet += ''.join(map(str.upper, alphabet))
def draw_letter(letter, font, save=True):
img = Image.new('RGB', (100, 100), 'white')
draw = ImageDraw.Draw(img)
draw.text((0,0), letter, font=font, fill='#000000')
if save:
img.save("imgs/{}.png".format(letter), 'PNG')
return img
def count_black_pixels(img):
pixels = list(img.getdata())
return len(filter(lambda rgb: sum(rgb) == 0, pixels))
def available_fonts():
fontdir = '/Users/alex/Desktop/English'
for root, dirs, filenames in os.walk(fontdir):
for name in filenames:
path = os.path.join(root, name)
try:
yield ImageFont.truetype(path, 100)
except IOError:
pass
def letter_statistics(counts):
for letter, counts in sorted(counts.iteritems()):
n = len(counts)
mean = sum(counts) / n
sd = sqrt(sum((x - mean) ** 2 for x in counts) / n)
yield letter, mean, sd
def main():
counts = defaultdict(list)
for letter in alphabet:
for font in available_fonts():
img = draw_letter(letter, font, save=False)
count = count_black_pixels(img)
counts[letter].append(count)
for letter, mean, sd in letter_statistics(counts):
print u"{0}: {1:.2f} ± {2:.2f}".format(letter, mean, sd)
if __name__ == '__main__':
main()
根据您的平台,可能有一种方法从字符串或DrawText()函数中“getWidth”,以某种方式使用width属性。
我会做一个简单的算法时间,利用所需的字体,然后通过alfabet运行,并将其存储在一个小配置或只是计算它在初始化作为一个循环从a到Z并不难。