在矩形背景上写矩形 [英] Writing rectangles over a rectangular background
问题描述
大家好。我正在尝试创建一个具有背景的函数,并使用png模块在背景中使用x,y coords从列表中编写矩形。
< b>我尝试了什么:
我一直想把它写成这样的
def foto(self,lista,randomname):
width = self.w
length = self.h
def create(iw,ih,c =( 0 , 0 , 0 )):
img = []
for _ in 范围(ih):
row = []
for _ in 范围(iw):
row.append(c)
img.append(row)
返回 img
def inside(img,i,j):
return 0 < = i< width 和 0< = j< lenght
def draw_quad_simple(img,x,y,w,h,c =( 0 , 0 , 0 )):
for j 在范围内(y,y + h):
for i < span class =code-keyword>在范围内(x,x + w):
如果在里面(img,i,j) :
img [j] [i] = c
background = create(self.w,self.h,(self.r,self.g,self.b))
p = 0
p 在范围内(len(lista)):
draw_quad_simple(background ,self.w,self.h,(lista [p] .x - self.x),(lista [p] .y-self.y),lista [p] .w,lista [p] .h,( lista [p] .r,lista [p] .g,lista [p] .b))
p = p + 1
def save(filename,img):
pyimg = png.from_array(img,' RGB')
pyimg.save(filename)
save(fname_out,background)
但代码中有一些我不明白的错误。我应该得到这个图片,但我得到这个,我不知道错误在哪里。输入是:
BackgroundRectangle =起点p1(50,70)颜色c1(255,0,0)矩形(p1 ,150,100,c1)
Rect2 =起点p2(100,40)颜色c2(0,255,0)矩形(p2,40,70,c2)
Rect3 =起点p3( 120,90)颜色c3(0,0,255)矩形(p3,60,40,c3)
Rect4 =起点p4(30,140)颜色c4(255,255,0)矩形(p4,50,20,c4)
Rect5 =起点p5(130,20)颜色c5(0,255,255)矩形(p5,50,20,c5)
lista = [Rect2,Rect3,Rect4,Rect5]
randomname = 'randomname'
编辑:修正了代码,现在它正在工作
< blockquote>我不太了解Python,但不应该 img.append(row)
调用缩进以在 iw $外执行c $ c>循环?
def create(iw,ih,c =( 0 , 0 , 0 )):
img = []
_ 范围(ih):
row = []
_ 范围(iw):
row.append( c)
img.append(row)
return img
循环中还有一个 p
变量的增量声明,以便只处理每一个项目。如果这不是故意的,请删除它:
# p = 0
p 范围内(len(lista)) :
draw_quad_simple(background,lista [p] .x,lista [p] .y,lista [p] .w,lista [p] .h,(lista [p] .r,lista [p]。 g,lista [p] .b))
# p = p + 1
[/ EDIT]
Hello people. I'm trying to make a function where you have a background and you write rectangles from a list with x,y coords over the background with the png module.
What I have tried:
I've been trying to write it down like this
def foto(self,lista,randomname):
width = self.w
length = self.h
def create(iw, ih, c=(0,0,0)):
img = []
for _ in range(ih):
row = []
for _ in range(iw):
row.append(c)
img.append(row)
return img
def inside(img, i, j):
return 0 <= i < width and 0<= j < lenght
def draw_quad_simple(img, x, y, w, h, c=(0,0,0)):
for j in range(y, y+h):
for i in range(x,x+w):
if inside(img,i,j):
img[j][i] = c
background= create(self.w,self.h,(self.r,self.g,self.b))
p=0
for p in range(len(lista)) :
draw_quad_simple(background,self.w,self.h,(lista[p].x - self.x),(lista[p].y-self.y),lista[p].w,lista[p].h,(lista[p].r,lista[p].g,lista[p].b))
p=p+1
def save(filename, img):
pyimg =png.from_array(img, 'RGB')
pyimg.save(filename)
save(fname_out,background)
But there is something wrong in the code that I don't understand. I should get this IMAGE but I'm getting THIS , and I have no clue where the error is. The input is :
BackgroundRectangle = starting point p1(50,70) color c1(255,0,0) Rectangle(p1,150,100,c1)
Rect2= starting point p2(100,40) color c2(0,255,0) Rectangle(p2,40,70,c2)
Rect3= starting point p3(120,90) color c3(0,0,255) Rectangle(p3,60,40,c3)
Rect4= starting point p4(30,140) color c4(255,255,0) Rectangle(p4,50,20,c4)
Rect5= starting point p5(130,20) color c5(0,255,255) Rectangle(p5,50,20,c5)
lista= [Rect2,Rect3,Rect4,Rect5]
randomname='randomname'
EDIT : Fixed the code , now it's working
I don't know Python well but shouldn't theimg.append(row)
call be outdented to be executed outsideiw
loop?
def create(iw, ih, c=(0,0,0)): img = [] for _ in range(ih): row = [] for _ in range(iw): row.append(c) img.append(row) return img
[EDIT]
There is also an increment statement for thep
variable inside a loop so that only every second item is processed. If this is not by intention, remove it:
#p=0 for p in range(len(lista)) : draw_quad_simple(background,lista[p].x,lista[p].y,lista[p].w,lista[p].h,(lista[p].r,lista[p].g,lista[p].b)) #p=p+1
[/EDIT]
这篇关于在矩形背景上写矩形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!