Quantcast
Channel: Scripting - McNeel Forum
Viewing all articles
Browse latest Browse all 5745

How to convert an RGB color value to grayscale in python inside GH?

$
0
0

@daizhuo wrote:

Hi!
Although I finally found that someone said "grayscale = RGB.R*0.3+RGB.G*0.59+RGB.B*0.11", but it seems not work. I had tomultiply 0.007 and got a result that seems grayscale was form 0 to 1. Here is my code:

import rhinoscriptsyntax as rs
import math
import System as sys
import Rhino.Geometry as rg
import scriptcontext as sc

path = path
bitmap = sys.Drawing.Bitmap.FromFile(path)
width = bitmap.Width
height = bitmap.Height

unum = 20
vnum = 20
uinc = 1.0/unum
vinc = 1.0/vnum

imageW=width/unum
imageH=height/vnum

panels=[]
temp=[]
for i in range(unum):
for j in range(vnum):
valRGB = sys.Drawing.Bitmap.GetPixel(bitmap,i*imageW,j*imageH)
val = (valRGB.R*0.3+valRGB.G*0.59+valRGB.B*0.11)*0.007

    pt1 = srf.PointAt(i*uinc,j*vinc)
    pt2 = srf.PointAt((i+1)*uinc,j*vinc)
    pt3 = srf.PointAt(i*uinc,(j+1-val)*vinc)
    pt4 = srf.PointAt((i+1)*uinc,(j+1-val)*vinc)

    panel = rg.NurbsSurface.CreateFromCorners(pt1,pt2,pt4,pt3)
    panel2= rg.NurbsSurface.CreateFromCorners(pt1,pt2,pt4,pt3)
    center = srf.PointAt((i+0.5)*uinc, (j+0.5)*vinc)
    normal = srf.NormalAt((i+0.5)*uinc, (j+0.5)*vinc)

    panel = rs.RotateObject(sc.doc.Objects.AddSurface(panel),center,val*math.pi*4,normal)

    panels.append(panel)

a = panels

Posts: 3

Participants: 2

Read full topic


Viewing all articles
Browse latest Browse all 5745

Trending Articles