0

当我尝试运行脚本时(我在 Mac 上;我按下命令 B,对吗?),而不是生成 txt 文件,没有任何反应。(我认为,唯一的 Traceback 信息总是会出现,而且通常代码无论如何都会运行——

Traceback (most recent call last):
File "/Users/[my name]/Downloads/ImportImageTextFile.py", line 2, in <module>
import rhinoscriptsyntax as rs
ImportError: No module named rhinoscriptsyntax

这是到目前为止的代码(抱歉,其中一些不相关——我粘贴的代码 1. 将 jpg 转换为文本文件,2. 将文本文件放入 rhino)。

# Import points from a text file
import rhinoscriptsyntax as rs
from System.Drawing import Color


recreateImage = False


def main(): 
    # import the converted pixel image and store the RGB values in "pixels":
    # note: the resultant list will be organized as follows:

    # the first 2 [] indicates the y and x coordinates
    # the last [] indicates the color (red = 0 , green = 1 , blue = 2)

    #   pixels[y][x] = [R,G,B]  to get the blue value at pixel 11 in the first row:  pixels[11][0][2]
    #   you can get the width of the pixel image by using:  w = len(pixels[0])   (this returns the length of the list of the first row or X-dimension)
    #   you can get the height of the pixel image by using:  h = len(pixels)   (this returns the length/number of "columns" or Y-dimension)


    pixels = importPoints(recreateImage)





    # the 4 is the "step value"

    for x in range (0, len(pixels[0]) , 2):
        for y in range (0, len(pixels) , 2):
            r = pixels[y][x][0]
            g = pixels[y][x][1]
            b = pixels[y][x][2]

            # we save the "AddSphere" into the phrase spId
            # we subtract the y to not let the image mirror/ make the pixelated image higher than the original image, otherwise it would be directly above the original image
            # the "3 - (r+g+b)/765" represents the sphere radius size

            # spId = rs.AddSphere([x,len(pixels)-y,(r+g+b)/765*30], 3 - (r+g+b)/765 )
            # rs.ObjectColor(spId, [r,g,b])

            # lines have a start point and an end point
            # [] / brackets indicate a new array/list

            lineId = rs.AddLine ( [x, -y , r+g+b] , [x, -y , 0] )
            rs.ObjectColor (lineId, [r,g,b])





def importPoints(makeImage):
    #prompt the user for a file to import
    filter = "Text file (*.txt)|*.txt|All Files (*.*)|*.*||"
    filename = rs.OpenFileName("Open Pixel File", filter)
    if not filename: return

    #read each line from the file
    file = open(filename, "r")
    contents = file.readlines()

    if (makeImage):
        previousLayer = rs.CurrentLayer()
        rs.AddLayer("Image")
        rs.CurrentLayer("Image")

    counterX = -1
    counterY = -1

    width = 0
    height = 0

    imagePixels = []
    pixelRow = []

    #browse through each line in the text file:
    for text in contents:
        items = text.strip("()\r\n").split(";")

        #print (str(items))        



        if (counterX == -1 and counterY == -1):
            counterY +=1

            width = int(items[1].split(",")[1])
            height = int(items[2].split(",")[1])
            print ("Image size: "+str(width)+" x "+str(height))

        else:
            r = int(items[0])
            g = int(items[1])
            b = int(items[2])
            counterX +=1

            if (counterX == width):
                counterY +=1
                counterX = 0
                imagePixels.append(pixelRow)
                pixelRow = []

            if (makeImage):
                ptId = rs.AddPoint ([counterX, -counterY, 0])
                rs.ObjectName (ptId, str(counterX)+" - "+str(counterY)+" : "+str(r)+","+str(g)+","+str(b))
                rs.ObjectColor (ptId, [r,g,b])

            pixelRow.append([r,g,b])


    file.close()

    if (makeImage):
        rs.CurrentLayer(previousLayer)

    return imagePixels



main()

太感谢了!

4

0 回答 0