algorithmic modeling for Rhino
Hi all,
do you know whether there is a way in the python console in rhino to import a module to read images?
What I am in need of would be to be able to read an image:
size of the image
color of a given pixel
(png, jpg, bmp, doesn't matter)
import png doesn't seem to work and I couldn't manage to install PIL.
Has anyone ever tried to do it? What would be the easiest way to do it?
Thanks in advance
best
Vincenzo
Tags:
Can't you use the System.Drawing.Image class? It has width and Height properties and a GetPixel(x,y) method.
Thanks David,
this is a good suggestion, still I am not managing to make it work:
import System
path = "C:\temp\image.jpg"
image = System.Drawing.Image.FromFile(path)
height = System.Drawing.Image.Height(image)
width = System.Drawing.Image.Width(image)
color = System.Drawing.Image.GetPixelFormatSize(height/2,width/2)
a) the path doesn't work, I get an error when I am using "from file" (wrong characters in path)
b) couldn't find getpixel but only getpixelformatsize which looks the wrong command
how should I fix it?
Thanks again
V.
Hi Vincenzo
When using paths in python, use the double backslash characters to separate folders,files. This is because a single backslash is an escape character in python.
Other than that, use the equal character to set or get the property of an object (Width and Height in this case):
import System
path = "D:\\temp\\image.jpg"
bitmap = System.Drawing.Bitmap.FromFile(path)
width = bitmap.Width
height = bitmap.Height
color = System.Drawing.Bitmap.GetPixel(bitmap, width/2, height/2)
Thanks djordje, my mistake, I think you can also use simple "/" and it would work
import System
import rhinoscriptsyntax as rs
path = "C:/temp/image.bmp"
bitmap = System.Drawing.Bitmap.FromFile(path)
width = bitmap.Width
height = bitmap.Height
color = System.Drawing.Bitmap.GetPixel(bitmap, width/2, height/2)
print color
rvalue = color[1]
but when in this case I get the color value which kind of variable is that? I
t is not considered a list in fact the last line is not working, how can I get the values as single numbers from that?
Yes, looks like it supports single forward slashes too. But in the upper example you used a single backslash, and python does not support that. Only double backslashes (that was the point of "wrong characters in path" error message).
As for the last issue: what you got is a Color object. Check for its properties:
avalue = color.A
rvalue = color.R
gvalue = color.G
bvalue = color.B
Thanks a lot again for the support, it definitely works!
Here a piece of code with it implemented and test image:
import System
import rhinoscriptsyntax as rs
def remap(uval,u,h):
start = u[0]
end = u[1]
startt= 0
endt= h
hval = (uval-start)/(end-start) *(endt)
return hval
path = "C:/temp/image2.bmp"
srf = "3e8b01e5-8da8-451e-a86f-060df82a94c9"
bitmap = System.Drawing.Bitmap.FromFile(path)
width = bitmap.Width
height = bitmap.Height
rs.EnableRedraw(False)
listUdomain = rs.SurfaceDomain(srf,0)
listVdomain = rs.SurfaceDomain(srf,1)
floatUstep = (listUdomain[1]-listUdomain[0])/40
floatVstep = (listVdomain[1]-listVdomain[0])/40
for i in rs.frange(listUdomain[0],listUdomain[1],floatUstep):
for j in rs.frange(listVdomain[0],listVdomain[1],floatVstep):
temppt = rs.EvaluateSurface(srf,i,j)
targetwidth = remap(i,listUdomain,width)
targetheight = remap(j,listVdomain,height)
col = System.Drawing.Bitmap.GetPixel(bitmap, abs(targetwidth-1), abs(targetheight-1))
pt = rs.AddSphere(temppt,.3)
rs.ObjectColor(pt,[col.R,col.B,col.G])
rs.EnableRedraw(True)
Hi Vincenzo,
Look great!
I would like to do the same as you did. Could you explain me a bit about these following lines?
def remap(uval,u,h):
start = u[0]
end = u[1]
startt= 0
endt= h
hval = (uval-start)/(end-start) *(endt)
return hval
Welcome to
Grasshopper
Added by Parametric House 0 Comments 0 Likes
Added by Parametric House 0 Comments 0 Likes
Added by Parametric House 0 Comments 0 Likes
Added by Parametric House 0 Comments 0 Likes
© 2024 Created by Scott Davidson. Powered by