【问题标题】:Python - converting bitmap image to list of colors per pixelPython - 将位图图像转换为每像素颜色列表
【发布时间】:2017-03-24 23:52:33
【问题描述】:

我有一个最大为 22 x 22 像素的位图的功能代码,但是除了奇怪的事情发生之外,有人知道为什么吗? 该程序将用于在我的世界/可能其他“基于像素”的游戏中自动构建大型结构。当前状态最多为 22×22 像素,但仅此而已,我想知道为什么/如何修复它。

当前错误信息(超过 22 像素的图像): KeyError(搜索调色板时,原因:颜色不再是 rgb 的元组,而是未知原因的单个数字

所需的大小最大为 128 x 128,结果是(对于 2 x 1 位图)-简化,因为它可能必须是正方形: [(0,0, '0-0-0', 黑色), (0,1, '255, 255, 255', 白色)]

当前节目:

# palette.py
# Palette with rgb-values depicting different blocks in-game:
predef = {}         #initialize a list variable, the fill it:
predef['255-255-255'] = 'air'
predef['255---0---0'] = 'redstone_block'
predef['--0--38-255'] = 'lapis_block'
predef['0-0-0'] = 'coal_block'

# main.py
from PIL import Image
import numpy as np
import json
import palette

def convert(filein, pal = palette.predef, zz = 0):
    '''
    due to a combined internal/external palette system ignore the warnings about incoherent types of str and dict
    as the dict is the internal, and the str is the filename of a json containing a dict.

    :param filein: Filename of bitmap
    :param pal: blank to use internal, or specify a separate palette.json
    :return:
    '''
    customset = []                  # customset list for blocks and positions

    if pal is not palette.predef:
        with open(pal) as js:
            pal = json.load(js)
        #print(pal) #debug to check palette

    im = Image.open(str(filein))
    p = np.array(im) #.reshape(-1, 3)
    print('width/height:', len(p), 'by', len(p))
    for y in range(0, len(p)):
        for x in range(0, len(p)):
            pix = p[y][x]
            pix = str(pix).replace(' ', '-').replace('[', '').replace(']', '')
            print(pix)
            pixcolor = (y, x, pix, pal[pix])     # Switch comment statu on these to see the error
            #pixcolor = pix                      # instead of the error message

            customset.append(pixcolor)

    return customset

print(convert('./1.bmp', './palettetest.json') # works with a bitmap up to 22 by 22

【问题讨论】:

  • convert()函数中的zz还没有实现,应该是z=的起点?用于建筑。也只实现了几种颜色,它们在palette.py文件中作为rgb值

标签: python python-3.x bitmap


【解决方案1】:

找到了解决办法:

从 PIL 导入图像 将 numpy 导入为 np 导入json 进口时间 import palette # 为什么在我们上面有 --0---0---0 时需要这个? -未知

def convert(filein, pal = palette.predef, zz = 0):
    '''
    due to a combined internal/external palette system ignore the warnings about incoherent types of str and dict
    as the dict is the internal, and the str is the filename of a json containing a dict.

    :param filein: Filename of bitmap
    :param pal: blank to use internal, or specify a separate palette.json
    :return:
    '''
    customset = []                  # customset list for blocks and positions
    if pal is not palette.predef:
        with open(pal) as js:
            pal = json.load(js)
        #print(pal)

    photo = Image.open(filein)  # opens the image
    photo = photo.convert('RGB')

    width = photo.size[0]
    height = photo.size[1]
    print('Size of current image is: {} by {}\n'.format(width, height))
    for y in range(0, height):
        row = ''

        for x in range(0, width):
            RGB = photo.getpixel((x,y))     # Gets the color value of pixel in color rgb
            R, G, B = RGB                   # Splits the rgb values
            rgb= str(RGB).replace(', ','-').replace('(', '').replace(')', '')
            info = 'x: {}, y: {} - pixelcolor: {}, block: {}'.format(x, y, rgb, pal[rgb])
            time.sleep(0.01) # to releave the cpu a bit
            customset.append(info)
            #print(info)

    return customset

print(convert('./1.bmp'))

【讨论】:

  • 如果有更好的问题解决方案,和/或如果您知道为什么第一个代码被超过 22x22 像素的图像损坏,请添加答案
猜你喜欢
  • 2012-07-08
  • 2012-03-11
  • 2013-01-09
  • 2021-02-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-07
  • 2015-05-02
相关资源
最近更新 更多