1729 @ ウィキ

BMP


※上記の広告は60日以上更新のないWIKIに表示されています。更新することで広告が下部へ移動します。

とりあえず ここ のサンプルをそのまま動かすpythonコード

if __name__ == '__main__':
    bmp = ''.join(map(chr,[
                           0x42,0x4d,                         # 'BM'
                           0x48,0,0,0,                        # file size 0x48
                           0,0,                               # 0
                           0,0,                               # 0
                           0x20,0,0,0,                        # pixel data start at 0x20
                           0x0c,0,0,0,                        # size of struct, 0x0c
                           0x0a,0,                            # image height: 10
                           0x0a,0,                            # image width: 10
                           1,0,                               # 1
                           1,0,                               # 1
                           0x00,0x00,0x80,                    # color: dark red (#800000)
                           0x00,0x00,0x00,                    # color: black
                           0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,   # * pixel data
                           0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,   # *
                           0,0,0,0,0,0,0,0                    # *
                           ]))
    f = open('test.bmp', 'wb') # 'wb' (write + binary)
    f.write(bmp)
    f.close()

出力:


HSL空間から、RGB空間への座標変換など
def hsl2rgb(h, s, l):
    h %= 360
    assert any(isinstance(x, float) for x in (h, s, l))
    assert 0 <= h < 360
    assert 0 <= s <= 1
    assert 0 <= l <= 1
    if l <= 0.5:
        sat_diff = s*l
    else:
        sat_diff = s*(1-l)
    max = l + sat_diff
    min = l - sat_diff
    hue_diff = 2*sat_diff*(h%60)/60
    if h < 60:
        r = max
        g = min + hue_diff
        b = min
    elif 60 <= h < 120:
        r = max - hue_diff
        g = max
        b = min
    elif 120 <= h < 180:
        r = min
        g = max
        b = min + hue_diff
    elif 180 <= h < 240:
        r = min
        g = max - hue_diff
        b = max
    elif 240 <= h < 300:
        r = min + hue_diff
        g = min
        b = max
    elif 300 <= h :
        r = max
        g = min
        b = max - hue_diff
    return r, g, b

def unit2byte(x):
    return chr(int(x*255))

画像の出力など
import math
import random
import imlib

class Pixel(object):
    def __init__(self, rgb=None, hsl=None):
        if rgb:
            self.rgb = rgb
        elif hsl:
            self.rgb = imlib.hsl2rgb(*hsl)
        else:
            self.rgb = 0, 0, 0
    def __str__(self):
        return ''.join(map(imlib.unit2byte, self.rgb))

class PPM(object):
    def __init__(self, xy):
        self.x, self.y = xy
        self.table = [[Pixel(hsl=[(1-abs(self.grad(i)*self.grad(j)*2-1))*36000/1.67, 1.0 , 0.5]) for i in range(self.x)] for j in range(self.y)]
    def grad(self, c, r=0):
        if not r:
            return float(c)/self.x
        else:
            return float(self.x - c)/self.x
    def grad_y(self, c):
        return c*255/self.y
    def to_ppm(self):
        data = 'P6\n#\n%s %s\n255\n' % (self.x, self.y)
        data += ''.join(''.join(str(pixel) for pixel in row) for row in self.table)
        return data
    def save_as(self, file):
        f = open(file, 'wb')
        f.write(self.to_ppm())
        f.close()

def test():
    PPM((120,)*2).save_as('img.ppm')

if __name__ == '__main__':
    test()




45x45:

48x48:





. . . . .

120x120: