英文字母中的哪个字母占据了大部分像素?

我试图根据一个句子中的字符数来做一些dynamic的编程。 英文字母中的哪一个字母占据了屏幕上最多的像素?

嗯,让我们看看:

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb

cccccccccccccccccccccccccccccccccccccccc

dddddddddddddddddddddddddddddddddddddddd

eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee

ffffffffffffffffffffffffffffffffffffffff

gggggggggggggggggggggggggggggggggggggggg

hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh

iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii

jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj

kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk

llllllllllllllllllllllllllllllllllllllll

mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm

nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn

oooooooooooooooooooooooooooooooooooooooo

pppppppppppppppppppppppppppppppppppppppp

qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq

rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr

ssssssssssssssssssssssssssssssssssssssss

tttttttttttttttttttttttttttttttttttttttt

uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu

vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv

wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy

zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz

AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA

BBBBBB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。

当然,这是一个愚蠢的经验实验。 哪个字母最宽没有单一的答案。 这取决于字体。 所以你必须做一个类似的经验实验来找出你的环境的答案。 但事实是,大多数字体遵循相同的惯例,大写字母W将是最宽的。

除了Ned Batchelder的非常实际的答案之外,因为我来这里想知道数字:

0000000000000000000000000000000000000000

1111111111111111111111111111111111111111

2222222222222222222222222222222222222222

3333333333333333333333333333333333333333

4444444444444444444444444444444444444444

5555555555555555555555555555555555555555

6666666666666666666666666666666666666666

7777777777777777777777777777777777777777

8888888888888888888888888888888888888888

9999999999999999999999999999999999999999

我相信字母W是最宽的。

资本“M”通常是最广泛的。

根据你的平台,可能有一种方法来从string或DrawText()函数“getWidth”以某种方式与宽度属性。

我会做一个简单的algortime,利用所需的字体,然后跑过alfabet并将其存储在一个小configuration中,或者只是在初始化时计算出来,因为从A到Z的循环不是那么难。

Chrome中的Arial 30px – W胜

这也取决于字体。 我在1年或2年前用Processing和Helvetica做了这个,按像素递增的顺序是ILJTYFVCPAXUZKHSEDORGNBQMW。 这个想法是用你正在看的字体在canvas上绘制文本,计算像素,然后用HashMap或Dictionary进行sorting。

当然,这可能不是直接关系到你的使用,因为这是计算像素面积而不是宽度。 也可能有点矫枉过正。

 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; } 

程序化解决scheme如何?

 var capsIndex = 65; var smallIndex = 97 var div = document.createElement('div'); div.style.float = 'left'; document.body.appendChild(div); var highestWidth = 0; var elem; for(var i = capsIndex; i < capsIndex + 26; i++) { div.innerText = String.fromCharCode(i); var computedWidth = window.getComputedStyle(div, null).getPropertyValue("width"); if(highestWidth < parseFloat(computedWidth)) { highestWidth = parseFloat(computedWidth); elem = String.fromCharCode(i); } } for(var i = smallIndex; i < smallIndex + 26; i++) { div.innerText = String.fromCharCode(i); var computedWidth = window.getComputedStyle(div, null).getPropertyValue("width"); if(highestWidth < parseFloat(computedWidth)) { highestWidth = parseFloat(computedWidth); elem = String.fromCharCode(i); } } div.innerHTML = '<b>' + elem + '</b>' + ' won'; 

Alex Michael在他的博客上发布了一个计算字体宽度的解决scheme,有点像xxx发布的解决scheme。

概要:

  • 对于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() 

这取决于字体。 例如零交叉占用了比常规的多得多。

但是如果能猜出来,我会用X或B去。

这将取决于字体。 我会用一种你最习惯的编程语言来创build一个小程序,在那里你将字母表中的每个字母都绘制成一个m大小的位图。 用白色初始化每个像素。 然后,在绘制每个字母后,计算白色像素的数量并保存该数字。 你find的最高的数字是你正在寻找的。

编辑:如果你其实只是感兴趣的是哪一个占据最大的矩形(但看起来像是你真的在那之后,而不是像素),你可以使用各种AP​​I调用find大小,但这取决于你的编程语言。 例如,在Java中,您将使用FontMetrics类。

我知道这里接受的答案是W,W是WIN。

但是,在这种情况下,W也是宽度。 使用的案例研究采用简单的宽度testing来检查像素,但只是宽度,而不是总像素数。 作为一个简单的反例,接受的答案假定O和Q占用相同数量的像素,但它们只占用相同数量的空间。

因此,W占据了最多的空间 。 但是,这是所有的像素被破解是?

我们来获取一些经验数据。 我从下面的B,M和W创build了imgur图像。然后我分析了它们的像素数(见下面),结果如下:

B:114像素

M:150像素

W:157像素

以下是我如何将它们送入canvas并分析来自图像的原始像素数据。

 var imgs = { B : "//i.imgur.com/YOuEPOn.png", M : "//i.imgur.com/Aev3ZKQ.png", W : "//i.imgur.com/xSUwE7w.png" }; window.onload = function(){ for(var key in imgs){(function(img,key){ var Out = document.querySelector("#"+key+"Out"); img.crossOrigin = "Anonymous"; img.src=imgs[key]; img.onload = function() { var canvas = document.querySelector('#'+key); (canvas.width = img.width,canvas.height = img.height); var context = canvas.getContext('2d'); context.drawImage(img, 0, 0); var data = context.getImageData(0, 0, img.width, img.height).data; Out.innerHTML = "Total Pixels: " + data.length/4 + "<br>"; var pixelObject = {}; for(var i = 0; i < data.length; i += 4){ var rgba = "rgba("+data[i]+","+data[i+1]+","+data[i+2]+","+data[i+3]+")"; pixelObject[rgba] = pixelObject[rgba] ? pixelObject[rgba]+1 : 1; } Out.innerHTML += "Total Whitespace: " + pixelObject["rgba(255,255,255,255)"] + "<br>"; Out.innerHTML += "Total Pixels In "+ key +": " + ((data.length/4)-pixelObject["rgba(255,255,255,255)"]) + "<br>"; }; })(new Image(),key)} }; 
 <table> <tr> <td> <canvas id="B" width="100%" height="100%"></canvas> </td> <td id="BOut"> </td> </tr> <tr> <td> <canvas id="M" width="100%" height="100%"></canvas> </td> <td id="MOut"> </td> </tr> <tr> <td> <canvas id="W" width="100%" height="100%"></canvas> </td> <td id="WOut"> </td> </tr> </table>