import numpy as np import imageio as io from PIL import Image class Color(object): def…
- User
import numpy as np import imageio as io from PIL import Image
class Color(object):
def __init__(self, color_table=None): if color_table is None: self.color_table = [1,0] else: self.color_table = color_table def RGB(self): if self.color_table[1] == 0: #Red if self.color_table[0] == 0: #Light return [255,192,192] elif self.color_table[0] == 1: #Normal return [255,0,0] elif self.color_table[0] == 2: #Dark return [192,0,0] elif self.color_table[1] == 1: #Yellow if self.color_table[0] == 0: #Light return [255,255,192] elif self.color_table[0] == 1: #Normal return [255,255,0] elif self.color_table[0] == 2: #Dark return [192,192,0] elif self.color_table[1] == 2: #Green if self.color_table[0] == 0: #Light return [192,255,192] elif self.color_table[0] == 1: #Normal return [0,255,0] elif self.color_table[0] == 2: #Dark return [0,192,0] elif self.color_table[1] == 3: #Cyan if self.color_table[0] == 0: #Light return [192,255,255] elif self.color_table[0] == 1: #Normal return [0,255,255] elif self.color_table[0] == 2: #Dark return [0,192,192] elif self.color_table[1] == 4: #Blue if self.color_table[0] == 0: #Light return [192,192,255] elif self.color_table[0] == 1: #Normal return [0,0,255] elif self.color_table[0] == 2: #Dark return [0,0,192] elif self.color_table[1] == 5: #Magenta if self.color_table[0] == 0: #Light return [255,192,255] elif self.color_table[0] == 1: #Normal return [255,0,255] elif self.color_table[0] == 2: #Dark return [192,0,192] def push_color(self): self.color_table[0] = (self.color_table[0] + 1) % 3 return self.RGB() def write_color(self): self.color_table[0] = (self.color_table[0] + 2) % 3 self.color_table[1] = (self.color_table[1] + 5) % 6 return self.RGB()current_color = Color() piet_painting = []
def draw_block(size,num): block = np.zeros( (12,12,3), dtype=np.uint8 )
if num != 0: old_push_color = current_color.push_color() current_color.write_color() block[:,:] = current_color.RGB() block[0,0] = old_push_color size = size +1 else: block[:,:] = current_color.RGB() pix_lft = 144-size div = pix_lft // 12 rem = pix_lft % 12 if div !=0: block[12-div:,]=0 block[11-div:,:rem]=0 pos_y = 12*num pos_x = 0 piet_painting[pos_x:pos_x+12,pos_y:pos_y+12] = blockdef draw_end(num): block = np.zeros( (12,5,3), dtype=np.uint8 )
old_push_color = current_color.push_color() block[:,:] = 255 block[0,0] = old_push_color block[0,1] = current_color.write_color() block[0:2,3] = 0 block[1,1] = 0 block[2,0] = 0 block[2,4] = 0 block[3,1:4] = 0 block[2,1:4]=current_color.write_color() pos_y = 12*num pos_x = 0 piet_painting[pos_x:pos_x+12,pos_y:pos_y+5] = blockmessage = 'Siyi Liu' painting_len = len(message)*12 + 5 piet_painting = np.zeros((12,painting_len,3), dtype=np.uint8)
i = 0 for char in message: draw_block(ord(char),i) i += 1 draw_end(i)
if painting_len < 390:
plato_painting = np.zeros((12,painting_len,3), dtype=np.uint8)
plato_painting[0:12,0:painting_len] = piet_painting
plato_img = Image.fromarray(plato_painting)
plato_img.save('plato_code.png')
img = Image.fromarray(piet_painting) img.save('piet_code_file.png') 请把以上代码使用ruby写出来,使用chunky_png gem,给我完整的代码
- Assistant
require 'chunky_png'
class Color def initialize(color_table = nil) if color_table.nil? @color_table = [1,0] else @color_table = color_table end end
def rgb if @color_table[1] == 0 #Red if @color_table[0] == 0 #Light return [255,192,192] elsif @color_table[0] == 1 #Normal return [255,0,0] elsif @color_table[0] == 2 #Dark return [192,0,0] end elsif @color_table[1] == 1 #Yellow if @color_table[0] == 0 #Light return [255,255,192] elsif @color_table[0] == 1 #Normal return [255,255,0] elsif @color_table[0] == 2 #Dark return [192,192,0] end elsif @color_table[1] == 2 #Green if @color_table[0] == 0 #Light return [192,255,192] elsif @color_table[0] == 1 #Normal return [0,255,0] elsif @color_table[0] == 2 #Dark return [0,192,0] end elsif @color_table[1] == 3 #Cyan if @color_table[0] == 0 #Light return [192,255,255] elsif @color_table[0] == 1 #Normal return [0,255,255] elsif @color_table[0] == 2 #Dark return [0,192,192] end elsif @color_table[1] == 4 #Blue if @color_table[0] == 0 #Light return [192,192,255] elsif @color_table[0] == 1 #Normal return [0,0,255] elsif @color_table[0] == 2 #Dark return [0,0,192] end elsif @color_table[1] == 5 #Magenta if @color_table[0] == 0 #Light return [255,192,255] elsif @color_table[0] == 1 #Normal return [255,0,255] elsif @color_table[0] == 2 #Dark return [192,0,192] end end end
def push_color @color_table[0] = (@color_table[0] + 1) % 3 rgb end
def write_color @color_table[0] = (@color_table[0] + 2) % 3 @color_table[1] = (@color_table[1] + 5) % 6 rgb end end
current_color = Color.new piet_painting = []
def draw_block(size,num) block = ChunkyPNG::Image.new(12,12,ChunkyPNG::Color.rgb(*current_color.rgb))
if num != 0 old_push_color = current_color.push_color current_color.write_color block[0,0] = ChunkyPNG::Color.rgb(*old_push_color) size += 1 end
pix_lft = 144-size div = pix_lft / 12 rem = pix_lft % 12
if div !=0 block.crop!(0,12-div,12,div) end
block.crop!(0,11-div,rem,1) if rem.nonzero?
pos_y = 12*num pos_x = 0 piet_painting[pos_x..pos_x+11,pos_y..pos_y+11] = block.pixels.map {|p| ChunkyPNG::Color.to_truecolor_bytes(p)} end
def draw_end(num) block = ChunkyPNG::Image.new(5,12) old_push_color = current_color.push_color block.rect(0,0,0,2,ChunkyPNG::Color.rgb(*old_push_color)) current_color.write_color block.rect(1,0,1,1,ChunkyPNG::Color.rgb(*current_color.rgb)) block.rect(0,2,4,2,ChunkyPNG::Color.rgb(255,255,255)) block.rect(2,2,3,2,ChunkyPNG::Color.rgb(current_color.write_color)) block.rect(1,3,3,3,ChunkyPNG::Color.rgb(0,0,0)) pos_y = 12num pos_x = 0 piet_painting[pos_x..pos_x+11,pos_y..pos_y+4] = block.pixels.map {|p| ChunkyPNG::Color.to_truecolor_bytes(p)} end
message = 'Siyi Liu' painting_len = message.length*12 + 5 piet_painting = ChunkyPNG::Image.new(painting_len,12, ChunkyPNG::Color.rgb(255,255,255))
i = 0 message.each_byte do |char| draw_block(char,i) i += 1 end
draw_end(i)
if painting_len < 390
plato_painting = ChunkyPNG::Image.new(painting_len,12, ChunkyPNG::Color.rgb(255,255,255))
plato_painting.compose!(piet_painting)
plato_img = plato_painting.to_image
plato_img.save('plato_code.png')
end
piet_img = piet_painting.to_image piet_img.save('piet_code_file.png')