如果一张图片值1000个单词,那么在140个字符中你能容纳多少图片?

Note: That's it folks! Bounty deadline is here, and after some tough deliberation, I have decided that Boojum's entry just barely edged out Sam Hocevar's. I will post more detailed notes once I've had a chance to write them up. Of course, everyone should feel free to continue to submit solutions and improve solutions for people to vote on. Thank you to everyone who submitted and entry; I enjoyed all of them. This has been a lot of fun for me to run, and I hope it's been fun for both the entrants and the spectators.

我偶然看到了一篇有趣的文章,是关于如何将图片压缩到Twitter评论中,许多人在那个帖子(以及Reddit上的一个帖子)对不同的方法提出了建议。所以,我认为这将是一个很好的编码挑战;让人们把他们的钱放在他们的嘴巴上,并展示他们关于编码的想法如何在有限的空间内带来更多细节。

我向您提出一个通用系统,将图像编码为140个字符的Twitter消息,然后再将它们解码为图像。您可以使用Unicode字符,因此每个字符可以获得8位以上的字节。然而,即使允许使用Unicode字符,也需要将图像压缩到非常小的空间;这肯定是一种有损压缩,因此必须对每个结果看起来有多好进行主观判断。

以下是原作者Quasimondo从编码中得到的结果(图片基于创作共用署名-非商业许可协议):

你能做得更好吗?

规则

Your program must have two modes: encoding and decoding. When encoding: Your program must take as input a graphic in any reasonable raster graphic format of your choice. We'll say that any raster format supported by ImageMagick counts as reasonable. Your program must output a message which can be represented in 140 or fewer Unicode code points; 140 code points in the range U+0000–U+10FFFF, excluding non-characters (U+FFFE, U+FFFF, U+nFFFE, U+nFFFF where n is 1–10 hexadecimal, and the range U+FDD0–U+FDEF) and surrogate code points (U+D800–U+DFFF). It may be output in any reasonable encoding of your choice; any encoding supported by GNU iconv will be considered reasonable, and your platform native encoding or locale encoding would likely be a good choice. See Unicode notes below for more details. When decoding: Your program should take as input the output of your encoding mode. Your program must output an image in any reasonable format of your choice, as defined above, though for output vector formats are OK as well. The image output should be an approximation of the input image; the closer you can get to the input image, the better. The decoding process may have no access to any other output of the encoding process other than the output specified above; that is, you can't upload the image somewhere and output the URL for the decoding process to download, or anything silly like that. For the sake of consistency in user interface, your program must behave as follows: Your program must be a script that can be set to executable on a platform with the appropriate interpreter, or a program that can be compiled into an executable. Your program must take as its first argument either encode or decode to set the mode. Your program must take input in one or more of the following ways (if you implement the one that takes file names, you may also read and write from stdin and stdout if file names are missing): Take input from standard in and produce output on standard out. my-program encode <input.png >output.txt my-program decode <output.txt >output.png Take input from a file named in the second argument, and produce output in the file named in the third. my-program encode input.png output.txt my-program decode output.txt output.png For your solution, please post: Your code, in full, and/or a link to it hosted elsewhere (if it's very long, or requires many files to compile, or something). An explanation of how it works, if it's not immediately obvious from the code or if the code is long and people will be interested in a summary. An example image, with the original image, the text it compresses down to, and the decoded image. If you are building on an idea that someone else had, please attribute them. It's OK to try to do a refinement of someone else's idea, but you must attribute them.

的指导方针

以下是一些可能被打破的规则、建议或评分标准:

Aesthetics are important. I'll be judging, and suggest that other people judge, based on: How good the output image looks, and how much it looks like the original. How nice the text looks. Completely random gobbledigook is OK if you have a really clever compression scheme, but I also want to see answers that turn images into mutli-lingual poems, or something clever like that. Note that the author of the original solution decided to use only Chinese characters, since it looked nicer that way. Interesting code and clever algorithms are always good. I like short, to the point, and clear code, but really clever complicated algorithms are OK too as long as they produce good results. Speed is also important, though not as important as how good a job compressing the image you do. I'd rather have a program that can convert an image in a tenth of a second than something that will be running genetic algorithms for days on end. I will prefer shorter solutions to longer ones, as long as they are reasonably comparable in quality; conciseness is a virtue. Your program should be implemented in a language that has a freely-available implementation on Mac OS X, Linux, or Windows. I'd like to be able to run the programs, but if you have a great solution that only runs under MATLAB or something, that's fine. Your program should be as general as possible; it should work for as many different images as possible, though some may produce better results than others. In particular: Having a few images built into the program that it matches and writes a reference to, and then produces the matching image upon decoding, is fairly lame and will only cover a few images. A program that can take images of simple, flat, geometric shapes and decompose them into some vector primitive is pretty nifty, but if it fails on images beyond a certain complexity it is probably insufficiently general. A program that can only take images of a particular fixed aspect ratio but does a good job with them would also be OK, but not ideal. You may find that a black and white image can get more information into a smaller space than a color image. On the other hand, that may limit the types of image it's applicable to; faces come out fine in black and white, but abstract designs may not fare so well. It is perfectly fine if the output image is smaller than the input, while being roughly the same proportion. It's OK if you have to scale the image up to compare it to the original; what's important is how it looks. Your program should produce output that could actually go through Twitter and come out unscathed. This is only a guideline rather than a rule, since I couldn't find any documentation on the precise set of characters supported, but you should probably avoid control characters, funky invisible combining characters, private use characters, and the like.

评分标准

作为我如何在选择我接受的解决方案时对解决方案进行排名的一般指南,让我们假设我可能会在25分的范围内评估解决方案(这是非常粗略的,我不会直接打分,只是将其作为一个基本指导方针):

15 points for how well the encoding scheme reproduces a wide range of input images. This is a subjective, aesthetic judgement 0 means that it doesn't work at all, it gives the same image back every time, or something 5 means that it can encode a few images, though the decoded version looks ugly and it may not work at all on more complicated images 10 means that it works on a wide range of images, and produces pleasant looking images which may occasionally be distinguishable 15 means that it produces perfect replicas of some images, and even for larger and more complex images, gives something that is recognizable. Or, perhaps it does not make images that are quite recognizable, but produces beautiful images that are clearly derived from the original. 3 points for clever use of the Unicode character set 0 points for simply using the entire set of allowed characters 1 point for using a limited set of characters that are safe for transfer over Twitter or in a wider variety of situations 2 points for using a thematic subset of characters, such as only Han ideographs or only right-to-left characters 3 points for doing something really neat, like generating readable text or using characters that look like the image in question 3 points for clever algorithmic approaches and code style 0 points for something that is 1000 lines of code only to scale the image down, treat it as 1 bit per pixel, and base64 encode that 1 point for something that uses a standard encoding technique and is well written and brief 2 points for something that introduces a relatively novel encoding technique, or that is surprisingly short and clean 3 points for a one liner that actually produces good results, or something that breaks new ground in graphics encoding (if this seems like a low number of points for breaking new ground, remember that a result this good will likely have a high score for aesthetics as well) 2 points for speed. All else being equal, faster is better, but the above criteria are all more important than speed 1 point for running on free (open source) software, because I prefer free software (note that C# will still be eligible for this point as long as it runs on Mono, likewise MATLAB code would be eligible if it runs on GNU Octave) 1 point for actually following all of the rules. These rules have gotten a bit big and complicated, so I'll probably accept otherwise good answers that get one small detail wrong, but I will give an extra point to any solution that does actually follow all of the rules

参考图片

有些人要求一些参考图片。这里有一些参考图片,你可以尝试一下;这里嵌入了较小的版本,如果你需要,它们都链接到较大版本的图像:

我提供500代表赏金(加上50 StackOverflow踢),我最喜欢的解决方案,基于上述标准。当然,我也鼓励其他人在这里投票选出他们最喜欢的解决方案。

截止日期说明

This contest will run until the bounty runs out, about 6 PM on Saturday, May 30. I can't say the precise time it will end; it may be anywhere from 5 to 7 PM. I will guarantee that I'll look at all entries submitted by 2 PM, and I will do my best to look at all entries submitted by 4 PM; if solutions are submitted after that, I may not have a chance to give them a fair look before I have to make my decision. Also, the earlier you submit, the more chance you will have for voting to be able to help me pick the best solution, so try and submit earlier rather than right at the deadline.

Unicode的笔记

There has also been some confusion on exactly what Unicode characters are allowed. The range of possible Unicode code points is U+0000 to U+10FFFF. There are some code points which are never valid to use as Unicode characters in any open interchange of data; these are the noncharacters and the surrogate code points. Noncharacters are defined in the Unidode Standard 5.1.0 section 16.7 as the values U+FFFE, U+FFFF, U+nFFFE, U+nFFFF where n is 1–10 hexadecimal, and the range U+FDD0–U+FDEF. These values are intended to be used for application-specific internal usage, and conforming applications may strip these characters out of text processed by them. Surrogate code points, defined in the Unicode Standard 5.1.0 section 3.8 as U+D800–U+DFFF, are used for encoding characters beyond the Basic Multilingual Plane in UTF-16; thus, it is impossible to represent these code points directly in the UTF-16 encoding, and it is invalid to encode them in any other encoding. Thus, for the purpose of this contest, I will allow any program which encodes images into a sequence of no more than 140 Unicode code points from the range U+0000–U+10FFFF, excluding all noncharacters and surrogate pairs as defined above.

I will prefer solutions that use only assigned characters, and even better ones that use clever subsets of assigned characters or do something interesting with the character set they use. For a list of assigned characters, see the Unicode Character Database; note that some characters are listed directly, while some are listed only as the start and end of a range. Also note that surrogate code points are listed in the database, but forbidden as mentioned above. If you would like to take advantage of certain properties of characters for making the text you output more interesting, there are a variety of databases of character information available, such as a list of named code blocks and various character properties.

Since Twitter does not specify the exact character set they support, I will be lenient about solutions which do not actually work with Twitter because certain characters count extra or certain characters are stripped. It is preferred but not required that all encoded outputs should be able to be transferred unharmed via Twitter or another microblogging service such as identi.ca. I have seen some documentation stating that Twitter entity-encodes <, >, and &, and thus counts those as 4, 4, and 5 characters respectively, but I have not tested that out myself, and their JavaScript character counter doesn't seem to count them that way.

提示和链接

The definition of valid Unicode characters in the rules is a bit complicated. Choosing a single block of characters, such as CJK Unified Ideographs (U+4E00–U+9FCF) may be easier. You may use existing image libraries, like ImageMagick or Python Imaging Library, for your image manipulation. If you need some help understanding the Unicode character set and its various encodings, see this quick guide or this detailed FAQ on UTF-8 in Linux and Unix. The earlier you get your solution in, the more time I (and other people voting) will have to look at it. You can edit your solution if you improve it; I'll base my bounty on the most recent version when I take my last look through the solutions. If you want an easy image format to parse and write (and don't want to just use an existing format), I'd suggest using the PPM format. It's a text based format that's very easy to work with, and you can use ImageMagick to convert to and from it.


当前回答

图像文件和python源代码(版本1和2)

版本1 这是我的第一次尝试。我会随时更新。

我已经把SO标志降到300个字符几乎无损。我的技术使用转换到SVG矢量艺术,所以它在直线艺术上效果最好。它实际上是一个SVG压缩器,它仍然需要原始美术经过矢量化阶段。

在我的第一次尝试中,我使用了一个在线服务来跟踪PNG,但是有许多免费和非免费的工具可以处理这部分,包括potrace(开源)。

以下是结果

原创SO Logo http://www.warriorhut.org/graphics/svg_to_unicode/so-logo.png原创 解码SO Logo http://www.warriorhut.org/graphics/svg_to_unicode/so-logo-decoded.png编码解码后

人物:300

时间:不可测量,但实际上是即时的(不包括矢量化/栅格化步骤)

下一阶段将为每个unicode字符嵌入4个符号(SVG路径点和命令)。目前,我的python构建没有广泛的字符支持UCS4,这限制了我的每个字符的分辨率。我还将最大范围限制在unicode保留范围0xD800的低端,然而,一旦我构建了允许字符的列表和一个过滤器来避免它们,理论上我可以将上面的logo所需的字符数量降低到70-100。

目前这种方法的一个局限性是输出大小不固定。它取决于向量化后的向量节点/点的数量。自动化这一限制将需要对图像进行像素化(这将消除向量的主要好处),或者在简化阶段重复运行路径,直到达到所需的节点数(这是我目前在Inkscape中手动执行的)。

版本2

更新:v2现在有资格竞争。变化:

命令行控制输入/输出和调试 使用XML解析器(lxml)来处理SVG而不是正则表达式 每个unicode符号打包2个路径段 文档和清理 支持style="fill:color"和fill="color" 文档宽度/高度打包成单个字符 路径颜色包装成单个字符 色彩压缩是通过 每次丢弃4位的颜色数据 颜色,然后包装成一个字符通过十六进制转换。

人物:133

时间:几秒钟

v2解码http://www.warriorhut.org/graphics/svg_to_unicode/so-logo-decoded-v2.png编码解码后(版本2)

正如您所看到的,这次有一些人工制品。这不是方法的限制,而是我转换中的某个错误。当点超出0.0 - 127.0范围时,就会发生工件,我试图约束它们的尝试有好有坏。解决方案是简单地缩放图像,但我有问题缩放实际的点,而不是画板或组矩阵,我现在太累了。简而言之,如果你的点在支持的范围内,它通常是可行的。

我相信中间的扭结是由于一个手柄移动到另一边的手柄连接。基本上,这些点一开始就靠得太近了。在压缩源图像之前运行一个简化过滤器可以修复这个问题,并去除一些不必要的字符。

更新: 这种方法适用于简单的对象,所以我需要一种方法来简化复杂的路径并减少噪音。我使用Inkscape来完成这个任务。我曾经用Inkscape剔除了一些不必要的路径,但没有时间尝试自动化。我使用Inkscape的“简化”功能来减少路径的数量,制作了一些样本svgs。

简化工作还可以,但它可能会很慢,因为有这么多路径。

自动跟踪示例http://www.warriorhut.org/graphics/svg_to_unicode/autotrace_16_color_manual_reduction.png康奈尔盒子http://www.warriorhut.com/graphics/svg_to_unicode/cornell_box_simplified.png莉娜http://www.warriorhut.com/graphics/svg_to_unicode/lena_std_washed_autotrace.png

缩略图追踪http://www.warriorhut.org/graphics/svg_to_unicode/competition_thumbnails_autotrace.png

这里有一些超低分辨率的照片。这些将更接近140个字符的限制,尽管一些聪明的路径压缩可能也需要。

培养http://www.warriorhut.org/graphics/svg_to_unicode/competition_thumbnails_groomed.png 简化和轻视。

trianglulated http://www.warriorhut.org/graphics/svg_to_unicode/competition_thumbnails_triangulated.png 简化,去斑点和三角化。

autotrace --output-format svg --output-file cornell_box.svg --despeckle-level 20 --color-count 64 cornell_box.png

上图:使用自动跟踪的简化路径。

不幸的是,我的解析器不处理自动跟踪输出,所以我不知道有多少点在使用或简化到什么程度,遗憾的是,在截止日期之前没有时间写它。它比inkscape输出更容易解析。

其他回答

在最初的挑战中,大小限制的定义是,如果你将文本粘贴到Twitter的文本框中,并按下“更新”键,Twitter仍然允许你发送的内容。正如一些人正确地注意到的那样,这与你用手机发送的短信不同。

What is not explictily mentioned (but what my personal rule was) is that you should be able to select the tweeted message in your browser, copy it to the clipboard and paste it into a text input field of your decoder so it can display it. Of course you are also free to save the message as a text file and read it back in or write a tool which accesses the Twitter API and filters out any message that looks like an image code (special markers anyone? wink wink). But the rule is that the message has to have gone through Twitter before you are allowed to decode it.

祝你好运,这350个字节——我怀疑你能不能利用它们。

愚蠢的想法,但是sha1(my_image)将导致任何图像的“完美”表示(忽略冲突)。显而易见的问题是解码过程需要大量的暴力。

1位单色会更容易一些。每个像素变成1或0,所以对于100*100像素的图像,您将有1000位数据。由于SHA1哈希值是41个字符,我们可以在一条消息中放入三个字符,只需要强力使用两组3333位和一组3334位(尽管这可能仍然是不合理的)。

这不太实际。即使是固定长度的1位100*100px图像。,假设我没有算错,49995000个组合,或者16661667个组合。

def fact(maxu):
        ttl=1
        for i in range(1,maxu+1):
                ttl=ttl*i
        return ttl

def combi(setsize, length):
    return fact(length) / (fact(setsize)*fact(length-setsize))

print (combi(2, 3333)*2) + combi(2, 3334)
# 16661667L
print combi(2, 10000)
# 49995000L

Roger Alsing写的这个遗传算法有一个很好的压缩比,代价是很长的压缩时间。得到的顶点向量可以使用有损或无损算法进一步压缩。

http://rogeralsing.com/2008/12/07/genetic-programming-evolution-of-mona-lisa/

这将是一个很有趣的程序,但我还是算了。

想法:你可以用字体作为调色板吗?尝试将图像分解为一系列向量,尝试用向量集的组合来描述它们(每个字符本质上是一组向量)。这是使用字体作为字典。我可以用l表示垂直线用-表示水平线?只是一个想法。

图像文件和python源代码(版本1和2)

版本1 这是我的第一次尝试。我会随时更新。

我已经把SO标志降到300个字符几乎无损。我的技术使用转换到SVG矢量艺术,所以它在直线艺术上效果最好。它实际上是一个SVG压缩器,它仍然需要原始美术经过矢量化阶段。

在我的第一次尝试中,我使用了一个在线服务来跟踪PNG,但是有许多免费和非免费的工具可以处理这部分,包括potrace(开源)。

以下是结果

原创SO Logo http://www.warriorhut.org/graphics/svg_to_unicode/so-logo.png原创 解码SO Logo http://www.warriorhut.org/graphics/svg_to_unicode/so-logo-decoded.png编码解码后

人物:300

时间:不可测量,但实际上是即时的(不包括矢量化/栅格化步骤)

下一阶段将为每个unicode字符嵌入4个符号(SVG路径点和命令)。目前,我的python构建没有广泛的字符支持UCS4,这限制了我的每个字符的分辨率。我还将最大范围限制在unicode保留范围0xD800的低端,然而,一旦我构建了允许字符的列表和一个过滤器来避免它们,理论上我可以将上面的logo所需的字符数量降低到70-100。

目前这种方法的一个局限性是输出大小不固定。它取决于向量化后的向量节点/点的数量。自动化这一限制将需要对图像进行像素化(这将消除向量的主要好处),或者在简化阶段重复运行路径,直到达到所需的节点数(这是我目前在Inkscape中手动执行的)。

版本2

更新:v2现在有资格竞争。变化:

命令行控制输入/输出和调试 使用XML解析器(lxml)来处理SVG而不是正则表达式 每个unicode符号打包2个路径段 文档和清理 支持style="fill:color"和fill="color" 文档宽度/高度打包成单个字符 路径颜色包装成单个字符 色彩压缩是通过 每次丢弃4位的颜色数据 颜色,然后包装成一个字符通过十六进制转换。

人物:133

时间:几秒钟

v2解码http://www.warriorhut.org/graphics/svg_to_unicode/so-logo-decoded-v2.png编码解码后(版本2)

正如您所看到的,这次有一些人工制品。这不是方法的限制,而是我转换中的某个错误。当点超出0.0 - 127.0范围时,就会发生工件,我试图约束它们的尝试有好有坏。解决方案是简单地缩放图像,但我有问题缩放实际的点,而不是画板或组矩阵,我现在太累了。简而言之,如果你的点在支持的范围内,它通常是可行的。

我相信中间的扭结是由于一个手柄移动到另一边的手柄连接。基本上,这些点一开始就靠得太近了。在压缩源图像之前运行一个简化过滤器可以修复这个问题,并去除一些不必要的字符。

更新: 这种方法适用于简单的对象,所以我需要一种方法来简化复杂的路径并减少噪音。我使用Inkscape来完成这个任务。我曾经用Inkscape剔除了一些不必要的路径,但没有时间尝试自动化。我使用Inkscape的“简化”功能来减少路径的数量,制作了一些样本svgs。

简化工作还可以,但它可能会很慢,因为有这么多路径。

自动跟踪示例http://www.warriorhut.org/graphics/svg_to_unicode/autotrace_16_color_manual_reduction.png康奈尔盒子http://www.warriorhut.com/graphics/svg_to_unicode/cornell_box_simplified.png莉娜http://www.warriorhut.com/graphics/svg_to_unicode/lena_std_washed_autotrace.png

缩略图追踪http://www.warriorhut.org/graphics/svg_to_unicode/competition_thumbnails_autotrace.png

这里有一些超低分辨率的照片。这些将更接近140个字符的限制,尽管一些聪明的路径压缩可能也需要。

培养http://www.warriorhut.org/graphics/svg_to_unicode/competition_thumbnails_groomed.png 简化和轻视。

trianglulated http://www.warriorhut.org/graphics/svg_to_unicode/competition_thumbnails_triangulated.png 简化,去斑点和三角化。

autotrace --output-format svg --output-file cornell_box.svg --despeckle-level 20 --color-count 64 cornell_box.png

上图:使用自动跟踪的简化路径。

不幸的是,我的解析器不处理自动跟踪输出,所以我不知道有多少点在使用或简化到什么程度,遗憾的是,在截止日期之前没有时间写它。它比inkscape输出更容易解析。