在imag上绘制对角线

2024-07-04 08:07:25 发布

您现在位置:Python中文网/ 问答频道 /正文

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

  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)

谢谢


Tags: 代码in图像for绘制rangewidthheight
3条回答

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

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),则只会绘制左上半行。。。

我想在讨论中加入一些数学上的考虑。。。

(很遗憾,JES的addLine函数只绘制了黑线,而且非常有限…)

注意:下面的代码使用了由MartinStettner(感谢他)指出的Bresenham的行算法。

The Bresenham's line algorithm is an algorithm which determines which order to form a close approximation to a straight line between two given points. Since a pixel is an atomic entity, a line can only be drawn on a computer screen by using some kind of approximation.

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

代码:

# 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的学生带来一些乐趣和想法。。。对其他人也一样。。。

你的picture对象来自哪里?它是什么?到目前为止什么不起作用?你想用什么库来访问图片?(我是说,你从哪里得到,或者打算从哪里得到“getWidth,getHeight,getPixel,setColor”?

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

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

画一条线需要同时改变x和y,更像是:

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

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

这是可行的,但除非图像是完全正方形,否则线条就不是完美的,否则它会跳过图像最宽方向上的像素。要解决这个问题,需要一个更精细的算法——但这是第二个能够真正访问图像上像素的方法——比如使用Python的图像库(PIL或Pillow)、pygame或其他库。

相关问题 更多 >

    热门问题