• Python复现颜色图绘制大赛的作品


    Python复现颜色图绘制大赛的作品

    受slandarer大佬启发,自己用Python复现了一下七年前的一个颜色图绘图大赛的一些作品。把复现过程记录如下:
    在这里插入图片描述
    这场比赛居然已经是七年前的事情了,大佬A Frayed Knot在StackExchange
    上发起了挑战,举办了名为“Tweetable数学艺术”的比赛(实际上活动整整进行了一年半)

    比赛规则如下:

    使用C++代码或格式与C++类似的代码,使用不多于140个字符,通过输入x,y坐标输出R,G,B颜色值的形式构造函数,并生成一张1024x1024大小的图片。

    挑战发起者给出了的一个基础示例:

    /* RED */
        return i&&j?(i%j)&(j%i):0;
    /* GREEN */
        return i&&j?(i%j)+(j%i):0;
    /* BLUE */
        return i&&j?(i%j)|(j%i):0;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    在这里插入图片描述
    但这个我拿c++和Python跑了一遍发现效果与之有差异,估计是有写错的地方或者C++各种版本不同格式转换的特性,期待大家的指出。我将其改写为python版本:

    导入工具包

    import cv2
    import numpy as np
    #导入python绘图matplotlib
    import matplotlib.pyplot as plt
    #使用ipython的魔法方法,将绘制出的图像直接嵌入在notebook单元格中
    %matplotlib inline
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    #定义可视化图像函数
    def look_img(img):
        '''opencv读入图像格式为BGR,matplotlib可视化格式为RGB,因此需将BGR转RGB'''
        try:  #显示彩图
            img_RGB = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
            plt.imshow(img_RGB)
            plt.show()
        except: #显示灰度图或黑白图
            plt.imshow(img,cmap="gray")
            #plt.axis('off')
            plt.show()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    颜色图1

    h = 1024
    w = 1024
    img = 255 * np.ones((h ,w , 3), dtype=np.uint8)  #生成一张1024*1024像素的白图
     
    for i in range(h):  # 循环遍历所有像素点,替换像素点
        for j in range(w):        
            img[i,j,2] = ((i%j)&(j%i))&255 if (i&j) else 0
            img[i,j,1] = ((i%j)+(j%i))&255 if (i&j) else 0
            img[i,j,0] = ((i%j)|(j%i))&255 if (i&j) else 0
     
    look_img(img)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    在这里插入图片描述

    同时自己也做了一些尝试,发现若是将代码微调,其结果也非常有意思:以下是改写版:

    颜色图2

    h = 1024
    w = 1024
    img = 255 * np.ones((h ,w , 3), dtype=np.uint8)  #生成一张1024*1024像素的白图
     
    for i in range(h):  # 循环遍历所有像素点,替换像素点
        for j in range(w):        
            img[i,j,2] = ((i%j)&(j%i))&200 if (i&j) else 0
            img[i,j,1] = ((i%j)+(j%i))&200 if (i&j) else 0
            img[i,j,0] = ((i%j)|(j%i))&200 if (i&j) else 0
     
    look_img(img)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    在这里插入图片描述

    颜色图3

    h = 1024
    w = 1024
    img = 255 * np.ones((h ,w , 3), dtype=np.uint8)  #生成一张1024*1024像素的白图
     
    for i in range(h):  # 循环遍历所有像素点,替换像素点
        for j in range(w):        
            img[i,j,2] = ((i%j)&(j%i))&255 if (i&j) else 0
            img[i,j,1] = ((i%j)^(j%i))&255 if (i&j) else 0
            img[i,j,0] = ((i%j)|(j%i))&255 if (i&j) else 0
     
    look_img(img)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    在这里插入图片描述

    颜色图4

    trichoplax大佬——方格布

    正常方格布:

    h = 1024
    w = 1024
    img = 255 * np.ones((h ,w , 3), dtype=np.uint8)  #生成一张1024*1024像素的白图
    DIM=1024
     
    for i in range(h):  # 循环遍历所有像素点,替换像素点
        for j in range(w):
            s=3./(j+99)        
            img[i,j,2] = (round((i+DIM)*s+j*s)%2+round((DIM*2-i)*s+j*s)%2)*127
            img[i,j,1] = (round((i+DIM)*s+j*s)%2+round((DIM*2-i)*s+j*s)%2)*127
            img[i,j,0] = (round((i+DIM)*s+j*s)%2+round((DIM*2-i)*s+j*s)%2)*127
     
    look_img(img)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    在这里插入图片描述

    颜色图5

    扭曲并添加颜色:

    h = 1024
    w = 1024
    img = 255 * np.ones((h ,w , 3), dtype=np.uint8)  #生成一张1024*1024像素的白图
    DIM=1024
     
    for i in range(h):  # 循环遍历所有像素点,替换像素点
        for j in range(w):
            s=3./(j+99)
            y=(j+np.sin((i*i+(j-700)**2*5)/100./DIM)*35)*s        
            img[i,j,2] = (round((i+DIM)*s+y)%2+round((DIM*2-i)*s+y)%2)*127
            img[i,j,1] = (round(5*((i+DIM)*s+y))%2+round(5*((DIM*2-i)*s+y))%2)*127
            img[i,j,0] = (round(29*((i+DIM)*s+y))%2+round(29*((DIM*2-i)*s+y))%2)*127
     
    look_img(img)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    在这里插入图片描述

    颜色图6

    大佬cjfaure——尖锐漩涡

    h = 1024
    w = 1024
    img = 255 * np.ones((h ,w , 3), dtype=np.uint8)  #生成一张1024*1024像素的白图
     
    for i in range(h):  # 循环遍历所有像素点,替换像素点
        for j in range(w):        
            img[i,j,2] = int(int(np.sqrt((73-i)**2+(609-j)**2)+1)/(np.sqrt(abs(np.sin(int(np.sqrt((860-i)**2+(162-j)**2))/115)))+1)/200)*90
            img[i,j,1] = int(int(np.sqrt((160-i)**2+(60-j)**2)+1)/(np.sqrt(abs(np.sin(int(np.sqrt((86-i)**2+(860-j)**2))/115)))+1)/200)*90
            img[i,j,0] = int(int(np.sqrt((844-i)**2+(200-j)**2)+1)/(np.sqrt(abs(np.sin(int(np.sqrt((250-i)**2+(20-j)**2))/115)))+1)/200)*90
     
    look_img(img)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    在这里插入图片描述

    颜色图7

    对参数进行微调:

    h = 1024
    w = 1024
    img = 255 * np.ones((h ,w , 3), dtype=np.uint8)  #生成一张1024*1024像素的白图
     
    for i in range(h):  # 循环遍历所有像素点,替换像素点
        for j in range(w):        
            img[i,j,2] = int(int(np.sqrt((148-i)**2+(1000-j)**2)+1)/(np.sqrt(abs(np.sin(int(np.sqrt((500-i)**2+(400-j)**2))/115)))+1)/200)*90
            img[i,j,1] = int(int(np.sqrt((610-i)**2+(60-j)**2)+1)/(np.sqrt(abs(np.sin(int(np.sqrt((864-i)**2+(860-j)**2))/115)))+1)/200)*90
            img[i,j,0] = int(int(np.sqrt((180-i)**2+(100-j)**2)+1)/(np.sqrt(abs(np.sin(int(np.sqrt((5-3-i)**2+(103-j)**2))/115)))+1)/200)*90
     
    look_img(img)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    在这里插入图片描述

    颜色图8

    cjfaure大佬——音乐谱

    h = 1024
    w = 1024
    img = 255 * np.ones((h ,w , 3), dtype=np.uint8)  #生成一张1024*1024像素的白图
     
    for i in range(h):  # 循环遍历所有像素点,替换像素点
        for j in range(w):        
            img[i,j,2] = (((int(100*np.sin(int((i+400)*(j+100)/11115)))&i)*1029)%256)*5
            img[i,j,1] = (((int(100*np.sin(int((i+400)*(j+100)/11115)))&i)*1029)%256)*5
            img[i,j,0] = (((int(100*np.sin(int((i+400)*(j+100)/11115)))&i)*1029)%256)*5
     
    look_img(img)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    在这里插入图片描述

    颜色图9

    色带代码及图片:

    h = 1024
    w = 1024
    img = 255 * np.ones((h ,w , 3), dtype=np.uint8)  #生成一张1024*1024像素的白图
     
    for i in range(h):  # 循环遍历所有像素点,替换像素点
        for j in range(w):        
            img[i,j,2] = np.cos(np.arctan2(j-512,i-512)/2)**2*255
            img[i,j,1] = (np.cos(np.arctan2(j-512,i-512)/2-2*np.arccos(-1)/3))**2*255
            img[i,j,0] = (np.cos(np.arctan2(j-512,i-512)/2+2*np.arccos(-1)/3))**2*255
     
    look_img(img)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    在这里插入图片描述

    颜色图10

    Snake大佬——三色谢宾斯基三角形

    h = 1024
    w = 1024
    img = 255 * np.ones((h ,w , 3), dtype=np.uint8)  #生成一张1024*1024像素的白图
     
    for i in range(h):  # 循环遍历所有像素点,替换像素点
        for j in range(w):        
            img[i,j,2] = int(np.cos(i&j))*255
            img[i,j,1] = int(np.cos((1024-i)&(1024-j)))*255
            img[i,j,0] = int(np.tan((i|j)))*125
     
    look_img(img)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    在这里插入图片描述

    (The End)

  • 相关阅读:
    高级深入--day39
    K8S部署Java项目 pod的logs报错为:Error: Unable to access jarfile app.jar
    centos7安装mysql8.x的注意事项,与5.x版本有许多不同
    redis如何清空当前缓存和所有缓存
    T31开发笔记: 移动侦测
    分类预测 | Matlab实现WOA-BiLSTM鲸鱼算法优化双向长短期记忆神经网络的数据多输入分类预测
    Golang面试笔试基础篇:从基础语法考察入手(一)
    桥接模式(Bridge)
    美团 2022-8-13笔试
    Git远程仓库配置SSH(以github为例)
  • 原文地址:https://blog.csdn.net/baidu/article/details/126533883