如果一张图片值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.


当前回答

这里的压缩效果很好。

http://www.intuac.com/userport/john/apt/

http://img86.imageshack.us/img86/4169/imagey.jpg http://img86.imageshack.us/img86/4169/imagey.jpg

我使用以下批处理文件:

capt mona-lisa-large.pnm out.cc 20
dapt out.cc image.pnm
Pause

结果文件大小为559字节。

其他回答

存储一堆参考图像的想法很有趣。存储25Mb的样本图像,并让编码器尝试使用这些位来组成图像,这是错误的吗?对于这么小的管道,两端的机器必然要比通过的数据量大得多,那么25Mb的代码和1Mb的代码和24Mb的图像数据之间有什么区别呢?

(请注意,最初的指导方针排除了将输入限制为库中已经存在的图像-我不是这样建议的)。

我的解决方案概述如下:

I start with calculating the maximum amount of raw data that you can fit into 140 utf8 characters. (I am assuming utf8, which is what the original website claimed twitter stored it's messages in. This differs from the problem statement above, which asks for utf16.) Using this utf8 faq, I calculate that the maximum number of bits you can encode in a single utf8 character is 31 bits. In order to do this, I would use all characters that are in the U-04000000 – U-7FFFFFFF range. (1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx, there are 31 x's, therefore I could encode up to 31 bits). 31 bits times 140 characters equals 4340 bits. Divide that by 8 to get 524.5, and round that down to 542 bytes. (If we restrict ourselves to utf16, then we could only store 2 bytes per character, which would equal 280 bytes). Compress the image down using standard jpg compression. Resize the image to be approximately 50x50px, then attempt to compress it at various compression levels until you have an image that is as close to 542 bytes as possible without going over. This is an example of the mona lisa compressed down to 536 bytes. Encode the raw bits of the compressed image into utf-8 characters. Replace each x in the following bytes: 1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx, with the bits from the image. This part would probably be the part where the majority of the code would need to be written, because there isn't anything that currently exists that does this.

我知道你们要的是代码,但我不想花时间写代码。我想一个有效的设计至少可以激励其他人编写代码。

我认为我提出的解决方案的主要好处是,它重用了尽可能多的现有技术。尝试编写一个好的压缩算法可能很有趣,但肯定会有更好的算法,很可能是由拥有高等数学学位的人编写的。

另一个重要的注意事项是,如果决定utf16是首选编码,那么这个解决方案就会失败。当压缩到280字节时,jpeg就不能正常工作了。不过,对于这个特定的问题语句,可能有比jpg更好的压缩算法。

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

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

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

以下是我解决这个问题的方法,我必须承认这是一个非常有趣的项目,它绝对超出了我的正常工作范围,给了我一些新的东西去学习。

我的基本想法如下:

向下采样图像灰度,这样总共有16个不同的阴影 在映像上执行RLE 将结果打包为UTF-16字符 对打包的结果执行RLE以删除任何重复字符

事实证明,这是有效的,但从下面的示例图像中可以看到,这只是在有限的范围内。就输出而言,下面是一个示例推文,特别是示例中显示的Lena图像。

乤 乤 arcandor 唂 伂 倂 倁 qi nong 2 companies 倁 3 companies 倁 2 companies 伂 8 companies 伂 3 companies 伂 5 companies 倂 倃 伂 倁 3 qi tuan qi 2 伂 倃 5 companies 倁 3 companies 倃 4 companies 倂 enterprise 倁 enterprise 伂 2 companies 伂 5 companies 倁 enterprise 伂 쥹 皗 鞹 Bei 륶 䦽 阹 럆 䧜 tsubaki 籫 릹 靭 욶 옷 뎷 step 㰷 qian 䴗 Cuan 㞳 鞷 㬼 mongoose 鏙 돗 鍴 祳 㭾 뤶 plunge 焻 � 乹 Ꮛ Dai 䍼

正如你所看到的,我确实试着限制了字符集;然而,在存储图像颜色数据时,我遇到了这样做的问题。此外,这种编码方案也会浪费大量可以用于其他图像信息的数据位。

就运行时间而言,对于较小的图像,代码的运行速度非常快,对于所提供的示例图像大约为55ms,但对于较大的图像,时间确实会增加。对于512x512 Lena参考映像,运行时间为1182ms。我应该指出的是,代码本身并没有很好地优化性能(例如,所有东西都作为位图工作),所以在一些重构之后,时间可能会减少一些。

请随时为我提供任何建议,我可以做得更好或什么可能是错误的代码。运行时和示例输出的完整列表可以在以下位置找到:http://code-zen.info/twitterimage/

更新一个

我已经更新了压缩推文字符串时使用的RLE代码,以做一个基本的回顾,如果是这样,那么使用输出。这只适用于数字值对,但它确实保存了数据的几个字符。运行时间和图像质量大致相同,但推文往往更小一些。测试完成后我会在网站上更新图表。以下是一个推文字符串的例子,同样是莉娜的小版本:

乤乤万乐唂伂倂倁企侬2企倁3企倁禹伂8企伂丁伂5企倂倃伂倁具儁企2伂倃加倁分队倃4企倂企倁企伂配制伂近视眼倁企伂쥹皗鞹鐾륶䦽阹럆䧜椿籫릹第욶옷뎷步㰷歉䴗镩㞳鞷㬼獴鏙돗鍴祳㭾뤶殒焻�乹Ꮛ靆䍼

更新两个

Another small update, but I modified the code to pack the color shades into groups of three as opposed to four, this uses some more space, but unless I'm missing something it should mean that "odd" characters no longer appear where the color data is. Also, I updated the compression a bit more so it can now act upon the entire string as opposed to just the color count block. I'm still testing the run times, but they appear to be nominally improved; however, the image quality is still the same. What follows is the newest version of the Lena tweet:

2 乤 arcandor 唂 伂 倂 倁 qi nong 2 companies 倁 3 companies 倁 ウ 伂 8 companies 伂 エ 伂 5 companies 倂 倃 伂 倁 グ jung enterprise 2 伂 倃 ガ 倁 ジ 倃 4 companies 倂 enterprise 倁 enterprise 伂 ツ 伂 ス 倁 enterprise 伂 坹 live keeps smashing strand 刾 啩 RongLi blow 婩 媷 advised 圿 Guo live 妛 putting casket choke 婣 cold nowadays, camp with polish serve Ji collapse are compared, the female 媗 definitely xing zong Yao 夽 xing 唹 until cold 圶 埫 奫 ð ª after he drank kratos appears

StackOverflow标志http://code-zen.info/twitterimage/images/stackoverflow-logo.bmp康奈尔盒子http://code-zen.info/twitterimage/images/cornell-box.bmp莉娜http://code-zen.info/twitterimage/images/lena.bmp蒙娜丽莎http://code-zen.info/twitterimage/images/mona-lisa.bmp

好吧,这是我的:nanocrunch.cpp和CMakeLists.txt文件,使用CMake构建它。它依赖magick++ ImageMagick API进行大部分图像处理。它还需要用于字符串编码的大字节算法的GMP库。

我基于分形图像压缩的解决方案,有一些独特的扭曲。基本思路是将图像缩小到50%,然后在各个方向上寻找与原始图像中不重叠的块相似的部分。它对这个搜索采取了非常强力的方法,但这只是让我更容易介绍我的修改。

第一个修改是,我的程序不仅考虑90度的旋转和翻转,还考虑45度的方向。每块多一个比特,但它极大地提高了图像质量。

另一件事是,为每个块的每个颜色组件存储对比度/亮度调整太昂贵了。相反,我存储了一个重度量化的颜色(调色板只有4 * 4 * 4 = 64种颜色),它只是以某种比例混合在一起。从数学上讲,这相当于对每种颜色进行可变亮度和恒定对比度的调整。不幸的是,这也意味着没有负对比来翻转颜色。

一旦计算出每个块的位置、方向和颜色,它就会将其编码为UTF-8字符串。首先,它生成一个非常大的bigum来表示块表中的数据和图像大小。这个方法类似于Sam Hocevar的解——一个基数随位置变化的大数。

Then it converts that into a base of whatever the size of the character set available is. By default, it makes full use of the assigned unicode character set, minus the less than, greater than, ampersand, control, combining, and surrogate and private characters. It's not pretty but it works. You can also comment out the default table and select printable 7-bit ASCII (again excluding <, >, and & characters) or CJK Unified Ideographs instead. The table of which character codes are available is stored a run-length encoded with alternating runs of invalid and valid characters.

Anyway, here are some images and times (as measured on my old 3.0GHz P4), and compressed to 140 characters in the full assigned unicode set described above. Overall, I'm fairly pleased with how they all turned out. If I had more time to work on this, I'd probably try to reduce the blockiness of the decompressed images. Still, I think the results are pretty good for the extreme compression ratio. The decompressed images are bit impressionistic, but I find it relatively easy to see how bits correspond to the original.

Stack Overflow Logo (8.6s编码,7.9s解码,485字节): http://i44.tinypic.com/2w7lok1.png

Lena(32.8秒编码,13.0秒解码,477字节): http://i42.tinypic.com/2rr49wg.png http://i40.tinypic.com/2rhxxyu.png

《蒙娜丽莎》(43.2秒编码,14.5秒解码,490字节) http://i41.tinypic.com/ekgwp3.png http://i43.tinypic.com/ngsxep.png

编辑:CJK统一字符

Sam在评论中问关于在CJK中使用这个。以下是从CJK统一字符集压缩到139个字符的《蒙娜丽莎》版本:

http://i43.tinypic.com/2yxgdfk.png Xu Lin 驞 wailing amidine ð ª ‰  according to 蛥 㶉 kink Qu 朖 辿 Korea storage epilepsy squid slanting Yi 㻅 ð ¦ ˆ  urea 蕜 held last releasing frequency smartweed debt 鑡 Zi Duo 靊 obscure ð ª ¸ 嚵 Yue poly Tui 慛 絖 makes 馿 Xie 櫰 Jue Yun Wen we Ying 粂 敽 tooth 稉 qing 蔍 螎 Xiang Bu 覧 light ð «   refrigerator vigilant 冧 笻 Ji mixed Yun core 譶 Hui dropping out Gui Yi 偞 媄 child Yu 梀 ð « – ’ scythe 猳 閺 Sheng and shan beak 伆 杇 婣 sucking 鐤 諽 鷍 owl ð « ˜ ¡ rob 毤 two-string fiddle 誖 terpene wish 珵 仾 xenon 熜 Huo 繴 Hui jin mustache 杍 嚖 Tong schoenberg 縿 bread 珝 dad 擸 萿

我使用的程序顶部的调优参数是:19,19,4,4,3,10,11,1000,1000。我还注释掉了number_assigned和codes的第一个定义,并取消注释掉了它们的最后一个定义,以选择CJK统一字符集。