在图像上绘制对角线

时间:2013-03-23 09:41:47

标签: python drawing jython jes

嗨我试图在图像的右上角到左下角绘制对角线,这是我的代码到目前为止。

  width = getWidth(picture)
  height = getHeight(picture)
  for x in range(0, width):
    for y in range(0, height):
      pixel = getPixel(picture, x, y)
      setColor(pixel, black)

由于

3 个答案:

答案 0 :(得分:4)

大多数图形库都有一些直接绘制线的方法。

JES中有addLine功能,因此您可以执行

addLine(picture, 0, 0, width, height)

如果你坚持设置单个像素,你应该看看Bresenham Line Algorithm,这是绘制线条最有效的算法之一。

代码注释:您使用两个嵌套循环执行的操作如下

for each column in the picture
  for each row in the current column
     set the pixel in the current column and current row to black

所以基本上你用黑色像素填充整个图像。

修改

要在整个图像上绘制多条对角线(在它们之间留一个空格),您可以使用以下循环

width = getWidth(picture)
height = getHeight(picture)
space = 10
for x in range(0, 2*width, space):
  addLine(picture, x, 0, x-width, height)

这会给你一个像(例子是手绘......)

的图像

diagonal lines

这使用了大多数图形库提供的剪切功能,即简单地忽略了不在图像中的部分线条。请注意,如果没有2*width(即如果x仅达到with),则只会绘制左上半部分...

答案 1 :(得分:1)

您的picture对象来自哪里?它是什么?到目前为止还没有什么工作?您尝试使用什么图像访问库? (我的意思是,你从哪里得到,或者打算从哪里得到“getWidth,getHeight,getPixel,setColor)?

我认为没有可以在setColor调用中使用“像素”作为整个对象的库,如果存在,它将是世界上最慢的东西 - 可能在银河系中。

另一方面,如果这些方法确实存在并且您的图片,上面的代码将覆盖所有黑色图像 - 您将获得所有可能的x值内的所有可能的“y”值(从0到高度)(来自图像的0到宽度,并为每个黑色着色。

绘制一条线需要您同时更改x和y,更像是:

(使用另一个“虚构的库”,但更合理一点:

for x, y in zip(range(0, width), range(0, height)):
   picture.setPixel((x,y), Black) )

这样做很有意思,但除非图像完全是正方形,否则线条不会很完美 - 否则它会跳过图像最宽方向的像素。要解决这个问题需要更精确的算法 - 但这是第二个让你有一个真正的方法来访问图像上的像素 - 比如使用Python的成像库(PIL或Pillow),pygame或其他一些库。

答案 2 :(得分:1)

我想在讨论中添加一些数学考虑因素......

(只是因为JES的addLine函数仅 黑线 而且非常有限而感到遗憾......)

注意: 以下代码使用MartinStettner指出的 Bresenham的线算法(所以感谢他)。

  

Bresenham的线算法是一种算法,它确定哪个顺序与两个给定点之间的直线形成近似。由于像素是原子实体,因此只能通过某种近似在计算机屏幕上绘制线条。

注意:要了解以下代码,您需要记住一些基本的学校数学课程(线方程和三角学)。

代码:

# The following is fast implementation and contains side effects...

import random

# Draw point, with check if the point is in the image area
def drawPoint(pic, col, x, y):
   if (x >= 0) and (x < getWidth(pic)) and (y >= 0) and (y < getHeight(pic)):
     px = getPixel(pic, x, y)
     setColor(px, col)


# Draw line segment, given two points
# From Bresenham's line algorithm
# http://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm
def drawLine(pic, col, x0, y0, x1, y1):

   dx = abs(x1-x0)
   dy = abs(y1-y0) 
   sx = sy = 0

   #sx = 1 if x0 < x1 else -1
   #sy = 1 if y0 < y1 else -1

   if (x0 < x1): 
     sx = 1 
   else: 
     sx = -1
   if (y0 < y1):
     sy = 1 
   else: 
     sy = -1

   err = dx - dy

   while (True):

     drawPoint(pic, col, x0, y0)

     if (x0 == x1) and (y0 == y1): 
       break

     e2 = 2 * err
     if (e2 > -dy):
       err = err - dy
       x0 = x0 + sx

     if (x0 == x1) and (y0 == y1):
       drawPoint(pic, col, x0, y0)
       break

     if (e2 <  dx):
       err = err + dx
       y0 = y0 + sy 


# Draw infinite line from segment
def drawInfiniteLine(pic, col, x0, y0, x1, y1):
   # y = m * x + b
   m = (y0-y1) / (x0-x1)
   # y0 = m * x0 + b   =>   b = y0 - m * x0
   b = y0 - m * x0

   x0 = 0
   y0 = int(m*x0 + b)
   # get a 2nd point far away from the 1st one
   x1 = getWidth(pic) 
   y1 = int(m*x1 + b)

   drawLine(pic, col, x0, y0, x1, y1)


# Draw infinite line from origin point and angle
# Angle 'theta' expressed in degres
def drawInfiniteLineA(pic, col, x, y, theta):

   # y = m * x + b
   dx = y * tan(theta * pi / 180.0)  # (need radians)
   dy = y

   if (dx == 0):
     dx += 0.000000001 # Avoid to divide by zero 

   m = dy / dx

   # y = m * x + b   =>   b = y - m * x
   b = y - m * x

   # get a 2nd point far away from the 1st one
   x1 = 2 * getWidth(pic)
   y1 = m*x1 + b

   drawInfiniteLine(pic, col, x, y, x1, y1)


# Draw multiple parallele lines, given offset and angle
def multiLines(pic, col, offset, theta, randOffset = 0):
   # Range is [-2*width, 2*width] to cover the whole surface
   for i in xrange(-2*getWidth(pic), 2*getWidth(pic), offset):
      drawInfiniteLineA(pic, col, i + random.randint(0, randOffset), 1, theta)

# Draw multiple lines, given offset, angle and angle offset
def multiLinesA(pic, col, offsetX, offsetY, theta, offsetA):
   j = 0
   # Range is [-2*width, 2*width] to cover the whole surface
   for i in xrange(-2*getWidth(pic), 2*getWidth(pic), offsetX):
      drawInfiniteLineA(pic, col, i, j, theta)
      j += offsetY
      theta += offsetA



file = pickAFile()
picture = makePicture(file)
color = makeColor(0, 65, 65) #pickAColor()
#drawline(picture, color, 10, 10, 100, 100)
#drawInfiniteLine(picture, color, 10, 10, 100, 100)
#drawInfiniteLineA(picture, color, 50, 50, 135.0)
#multiLines(picture, color, 20, 56.0)
#multiLines(picture, color, 10, 56.0, 15)
multiLinesA(picture, color, 10, 2, 1.0, 1.7) 

show(picture)


输出(由 Pierre Soulages 绘画):


enter image description here

enter image description here

enter image description here


希望这给JES学生带来一些乐趣和想法......还有其他人......