如何用代码码出一个樱桃小丸子?

论坛 期权论坛 期权     
匿名用户1024   2021-5-15 09:03   7214   5
听说代码是万能的,然后我就想到了我最爱的小丸子~
可不可以用代码码出一个小丸子呢?
不是可以码出一个大白吗?
我爱大白,更爱小丸子…

我要的小丸子呢,她可以是这样:
嘿哟哟,呼洽呼洽~
这样:
啦啦啦~唱着歌儿,上学去啦~
以及这样
biu!哇咔咔~

唉~~真的好喜欢小丸子呢!
求程序猿哥哥和程序媛姐姐作答。

以上,来自一个小丸子迷,嘻嘻嘻。
分享到 :
0 人收藏

5 个回复

倒序浏览
2#
有关回应  16级独孤 | 2021-5-15 09:03:42 发帖IP地址来自
谢邀~那我就正儿八经回答下吧╮(╯▽╰)╭
“如何用代码码出一个樱桃小丸子?” 可以从几个角度进行理解:
1.樱桃小丸子用代码字符的形式表现出来
2.绘制樱桃小丸子到屏幕上
下面分别说一下这两种都是如何去处理的。

---------------第一种----------------
简单的图形其实可以手工去码字处理,而遇到樱桃小丸子这类比较复杂的图像,由于工作量及显示效果问题,一般还是要靠算法来将图像信息转成字符形式,前面几个哥们儿已经回答的不错了。
算法的基本思路是先取得原图像素的灰度信息(可以参考:Grayscale 这里提到的算法),然后将不同的灰度范围对应不同的字符,通常这些字符在视觉上的明暗是有差异的,从而实现图片转字符的功能。
核心代码如下:
static NSString * characterMap = @"#@8%Oo\";,'. ";
for(int y = 0; y < height; y++) {
        for(int x = 0; x < width; x++) {
            uint8_t *rgbaPixel = (uint8_t *) &pixels[y * width + x];
            uint32_t gray = 0.3 * rgbaPixel[RED] + 0.59 * rgbaPixel[GREEN] + 0.11 * rgbaPixel[BLUE];
            NSUInteger position = lroundf((CGFloat)gray * 11.0f/255.0f);
            [ASCIIString appendFormat:@"%c", [characterMap characterAtIndex:position]];
        }
        [ASCIIString appendString:@"\n"];
}
最后的效果如下(比较惨烈):


还可以用类似十字绣的方式去处理,将原图分成大小相同的格子,然后分别计算每个格子的平均色值,最后用一个字符替代之,并染色处理,这样就可以是字符十字绣形式的小丸子啦,
//画格子的代码
    var getPixelsGrid  =function()
    {
//图片的原始数据
        var sourceData = getSourceData();
        var res = [];
        for (var i = 0; i < sourceData.length; i += 4) {
            var y = Math.floor(i / (getCanvasWidth() * 4));
            var x = (i - (y * getCanvasWidth() * 4)) / 4;
            if (typeof res[x] === "undefined") {
                res[x] = [];
            }
            res[x][y] = {
                r: sourceData[i+0],
                g: sourceData[i+1],
                b: sourceData[i+2],
                a: sourceData[i+3]
            }
        }
        return res;
    };
效果如下:


有个在线工具,你可以去玩一下:
ASCII-art converter

两种结合一下,效果也不错(用的其他图片)



---------------第二种----------------
拖了几天,继续来写第二种理解的了,绘制樱桃小丸子到屏幕上。
先说说屏幕的显示原理吧,如果我们把屏幕放大可以看到屏幕上其实有很多小点组成,如下:


屏幕上所有的图像都是由这几种颜色调和而成,于是呢,红色、绿色、蓝色又成称为光的三原色,也就是RGB,RGB各有256级亮度,所以可有的色彩组合就有256×256×256=16777216种颜色。
喵蛋,好像扯太远了..拉回来
好,然后呢,有个神器叫GPU,图片加载以后,GPU和CPU相互配合(这个展开讲会晕...),将图片信息解压,计算颜色混合等各种麻烦的事情,然后将图片信息渲染到界面上..大概过程就是这样。
OK...以上,其实程序员不需要特别深入了解,我们的在写程序的时候只需要告诉,我要从哪里加载图片就好。
iOS来说一句话就好了..[UIImage imageNamed:@"樱桃小丸子.jpg"];
后面的.jpg是什么呢,是图片的格式,换句话说图片以何种格式存储的,常见的位图存储格式有png、jpg、GIF等,那么什么是位图呢,就是它图片信息是以像素阵列的形式记录的,放大后会由于像素信息不全,模糊掉..
除了位图之外就是矢量图形,矢量图是通过数学计算记录一系列曲线信息及颜色、轮廓大小等形式记录下来的图片数据,一般矢量图要比位图大,但是好处就是,由于是数学记录的,所以放大后不会失真。
于是我把小丸子的图片转成了svg格式的矢量图,里面的记录格式成什么样子呢:


里面的path就是一条条曲线路径及其系列属性,然后...我用绘制代码的形式将其绘制出来...


大概就酱紫的代码 重复重复重复(也是蛮拼的体力活),当然像这种复杂图形,人工写会累死,一般都是借助辅助工具,比如我用的"PaintCode",画出来大概这样:


然后这个小丸子总共写了多少行代码呢?答:17000多行...多来几个10万行代码经验就有了(不过无意义)....
3#
有关回应  16级独孤 | 2021-5-15 09:03:43 发帖IP地址来自
  1. # -*- coding:utf-8 -*-class Image2Binary(object):    def __init__(self):        pass    def read_file(self, infile, mode=&#39;rb&#39;):        data = None        with open(infile, mode) as f:            data = f.read()        return data    def write_file(self, data, outfile, mode=&#39;wb&#39;):        with open(outfile, mode) as f:            f.write(data)    def image2binary(self, data):        return &#39;&#39;.join([&#39;{0:08b}&#39;.format(ord(i)) for i in data]) if data else None    def binary2image(self, data):        return &#39;&#39;.join([chr(int(data[i:i + 8], 2)) for i in range(0, len(data), 8)]) if data else Noneif __name__ == &#39;__main__&#39;:    infile = &#39;test.jpg&#39;    outfile = &#39;test_out.jpg&#39;    image = Image2Binary()    image_data = image.read_file(infile)    binary_data = image.image2binary(image_data)    rollback_data = image.binary2image(binary_data)    image.write_file(rollback_data, outfile)
复制代码

无聊,贴段Python代码娱乐.
功能1: 将图片数据,转换成二进制数据
功能2: 将二进制数据,反转回图片数据.

可以把一个图片,转成二进制数串,再重新写出原图.
哈哈,我承认是很无聊.

这段代码有啥用呢.
我觉得吧,可以把某些高清无码的大图,转成01串,然后悄悄的...

哈哈哈哈~



################   萌萌哒   ###################
################   萌萌哒   ###################
################   萌萌哒   ###################
################   萌萌哒   ###################
################   萌萌哒   ###################

update:  实现提问要求.

  1. # -*- coding:utf8 -*-################################################################ note:# 1. 实现把 image 图片,转化成ASCII 码的图片效果.# 2. 依赖的 Python 包: PIL# 3. 64位的Python,无法安装PIL,需安装pillow替代PIL(导包语句不变)###############################################################from PIL import Imageclass Image2Ascii(object):    def __init__(self, filename):        self.def_chars = "   ...&#39;,;:clodxkO0KXNWMMM"        self.def_height = 50        self.image = Image.open(filename)    def adjust_size(self, image, height):        if image:            width = int((height * image.size[0]) / image.size[1] * 2.3)            return (width, height)        return None    def image2ascii(self):        result = &#39;&#39;        new_size = self.adjust_size(self.image, self.def_height)        self.image = self.image.resize(new_size)        self.image= self.image.convert(&#39;L&#39;)        pixs = self.image.load()        for y in range(new_size[1]):            for x in range(new_size[0]):                result = &#39;%s%s&#39; % (result, self.def_chars[(pixs[x,y]/10)-1])            result = &#39;%s\n&#39; % result        print &#39;ACSII IMAGE:\n&#39;, resultif __name__ == &#39;__main__&#39;:    image = Image2Ascii(&#39;test.jpg&#39;)    image.image2ascii()
复制代码


原图:


画出来的 ASCII 码图:



题图小丸子:




算法不是原创,稍作修改,发来娱乐一下.
4#
有关回应  16级独孤 | 2021-5-15 09:03:44 发帖IP地址来自
吃胖版的小丸子。。。。的头。。。。



附上代码吧。代码复制了保存在记事本中。然后记事本后缀改成html。chorme运行正常。不知道其他的怎么样。不会写浏览器兼容。。_(:з」∠)_
  1. 小丸子吃胖了*{ margin: 0px; padding: 0px;}body{ background: #fff;}#Chi-biMaruko{ margin: 50px auto; width: 400px; }.head{ width: 230px; height: 100px; background:#000; border-radius:100px 100px 0px 0px; position: relative; transform:rotate(5deg); -webkit-transform:rotate(5deg); -moz-transform:rotate(5deg);}#hair_1{     width: 0;     height: 0;     transform: rotate(-20deg);  -webkit-transform: rotate(-20deg);  -moz-transform: rotate(-20deg);     border-top: 58px solid black;    border-right: 50px solid rgba(251,220,199,1);     position: absolute;      left: 80px;     top: 53px; } #hair_2{     width: 0;     height: 0;    transform:rotate(-15deg); -webkit-transform:rotate(-15deg); -moz-transform:rotate(-15deg);     border-top: 40px solid black;    border-right: 40px solid rgba(251,220,199,1);     position: absolute;      left: 40px;     top: 66px; } #hair_3{     width: 0;     height: 0;     border-top: 33px solid black;    border-right: 20px solid rgba(251,220,199,1);     position: absolute;      left: 23px;     top: 70px; } #hair_4{     width: 0;     height: 0;    transform:rotate(3deg); -webkit-transform:rotate(3deg); -moz-transform:rotate(3deg);     border-top: 30px solid black;    border-right: 23px solid rgba(251,220,199,1);     position: absolute;      left: 0px;     top: 82px; }  #hair_5{     width: 0;     height: 0;    transform:rotate(-20deg); -webkit-transform:rotate(-20deg); -moz-transform:rotate(-20deg);     border-top: 40px solid black;    border-right: 30px solid rgba(251,220,199,1);     position: absolute;      left: 135px;     top: 67px; } #hair_6{     width: 0;     height: 0;    transform:rotate(-15deg); -webkit-transform:rotate(-15deg); -moz-transform:rotate(-15deg);     border-top: 30px solid black;    border-right: 23px solid rgba(251,220,199,1);     position: absolute;      left: 170px;     top: 82px; } #hair_7{     width: 0;     height: 0;    transform:rotate(-15deg); -webkit-transform:rotate(-15deg); -moz-transform:rotate(-15deg);     border-top: 25px solid black;    border-right: 18px solid rgba(251,220,199,1);     position: absolute;      left: 195px;     top: 90px; } .head_2{  width: 230px;  height: 180px;  background: rgba(251,220,199,1);  position: absolute;  top: 100px;  left: 0px;  z-index: -1;  transform: rotate(-1deg);  -webkit-transform:rotate(-1deg);    -moz-transform:rotate(-1deg);    border-radius: 0px 0px 115px 115px; } .brow_left{  border: 4px solid #000;    width: 80px;    height: 80px;    position: absolute;    top: 80px;    left: 15px;    border-radius: 50px 30px 40px 30px;    transform: rotate(120deg);    -webkit-transform:rotate(120deg);    -moz-transform:rotate(120deg); } .brow_left:after{    content: "";    width: 73px;    height: 100px;    position: absolute;    bottom: 3px;    left: 26px;    background: rgba(251,220,199,1);    transform: rotate(-22deg);    -webkit-transform:rotate(-22deg);    -moz-transform:rotate(-22deg); } #keepOut_1{   background: #FBDCC7;   width: 10px;   height: 10px;   position: absolute;   top: 77px;   left: 36px;   z-index: 1; } #keepOut_2{   background: #FBDCC7;   width: 20px;   height: 20px;   position: absolute;   bottom: 63px;}#keepOut_3{   background: #FBDCC7;   width: 40px;     height: 10px;   position: absolute;   top: 27px;   right: 50px;   transform: rotate(-50deg);   -webkit-transform: rotate(-50deg);}.brow_right{  border: 4px solid #000;    width: 80px;    height: 80px;    position: absolute;    top: 90px;    left: 90px;    border-radius: 50px 30px 40px 30px;    transform: rotate(140deg);    -webkit-transform:rotate(140deg);    -moz-transform:rotate(140deg); } .brow_right:after{    content: "";    width: 73px;    height: 100px;    position: absolute;    bottom: 0px;    left: 17px;    background: rgba(251,220,199,1);    transform: rotate(-62deg);    -webkit-transform:rotate(-62deg);    -moz-transform:rotate(-62deg); } #keepOut_4{   background: #FBDCC7;   width: 90px;   height: 25px;   position: absolute;   top: 55px;   left: 15px;   transform: rotate(-50deg);   -webkit-transform: rotate(-28deg);}#eyesLeft{ border: 4px solid #000;    width: 80px;    height: 80px;    position: absolute;    top: 130px;    left: 15px;    border-radius: 50px 30px 90px 30px;    transform: rotate(150deg);    -webkit-transform:rotate(150deg);    -moz-transform:rotate(150deg);}#eyesLeft:after{  content: "";  width: 86px;  height: 90px;  position: absolute;  bottom: 11px;  left: 0px;  background: rgba(251,220,199,1);  transform: rotate(27deg);  -webkit-transform: rotate(27deg);  -moz-transform: rotate(27deg);}#keepOut_5{     background: #FBDCC7;   width: 20px;   height: 20px;   position: absolute;   top: 40;   right: 76;}#keepOut_6{   background: #FBDCC7;   width: 20px;   height: 20px;   position: absolute;   top: 64px;   right: 29;}#eyesRight{ border: 4px solid #000;    width: 40px;    height: 40px;    position: absolute;    top: 125px;    left: 120px;    border-radius: 111px 120px 130px 110px;    transform: rotate(150deg);    -webkit-transform:rotate(150deg);    -moz-transform:rotate(150deg);}#keepOut_7{    width: 50px;    height: 60px;    position: absolute;    bottom: 1px;    left: 11px;    background: rgba(251,220,199,1);    transform: rotate(-30deg);    -webkit-transform:rotate(-30deg);    -moz-transform:rotate(-30deg);}#keepOut_8{   background: #FBDCC7;   width: 20px;   height: 20px;   position: absolute;   bottom: 20px;   right: 30;}#mouth{    background: rgb(251, 220, 199);  width: 20px;  height: 20px;  position: absolute;  border-radius: 100%;  top: 230px;  left: 110px;  border: 2px solid;}#keepOut_9{    background: rgb(251, 220, 199);  width: 30px;  height: 30px;  position: absolute;  top: 217px;  left: 106px;  transform: ratote(-20deg);  -webkit-transform: rotate(-20deg);}#blusherLeft{  background: rgb(247, 157, 167);  width: 20px;  height: 20px;  position: absolute;  border-radius: 100%;  top: 200px;  left: 30px;}#blusherRight{  background: rgb(247, 157, 167);  width: 20px;  height: 20px;  position: absolute;  border-radius: 100%;  top: 200px;  left: 190px;}                                                                                                                                                                                          
复制代码



妈妈我再也不装逼了。QAQ
5#
有关回应  16级独孤 | 2021-5-15 09:03:45 发帖IP地址来自
ImagetoAsciiArt  软件可以转化的


修改尺寸效果更加精细

6#
有关回应  16级独孤 | 2021-5-15 09:03:46 发帖IP地址来自
度娘到的java方法:
[code]public class PrintImg{  public static void main(String[] args) throws Exception {        char[][] result=imageToASCII(new File("D:/1.jpg"));        FileWriter fw=new FileWriter(new File("D:/1.txt"),true);        for (int i=0;i> 8) & 0xFF;                int b = (argb >> 0) & 0xFF;                int grayPixel = (int)(0.299*r+0.587*g+0.114*b);                if (grayPixel>=230) gray[x][y]=&#39; &#39;;                else if (grayPixel>=200) gray[x][y]=&#39;.&#39;;                else if (grayPixel>=180) gray[x][y]=&#39;*&#39;;                else if (grayPixel>=160) gray[x][y]=&#39;:&#39;;                else if (grayPixel>=130) gray[x][y]=&#39;o&#39;;                else if (grayPixel>=100) gray[x][y]=&#39;&&#39;;                else if (grayPixel>=70) gray[x][y]=&#39;8&#39;;                else if (grayPixel>=50) gray[x][y]=&#39;#&#39;;                else gray[x][y]=&#39;@&#39;;                System.out.print(gray[x][y]);                img2.setRGB(y, x, (grayPixel
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:136515
帖子:27303
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP