[英]PyGame only blits last of loop
我正在開發一個在人們臉上畫一個正方形的應用程序(非常基本但不是最簡單的)。 它只打印 1 個正方形而不是 2+。
例如,這是 output:
output 希望(Photoshop obv):
代碼(剪斷):
while x <= faces:
print(x)
image_height = int(response[x]["faceRectangle"]["height"]) * image_modifier_height
image_top = int(response[x]["faceRectangle"]["top"]) * image_modifier_height # - image_height/2
image_left = int(response[x]["faceRectangle"]["left"]) * image_modifier_width
image_width = int(response[x]["faceRectangle"]["width"]) * image_modifier_width
upper_line = (image_left, image_top)
bottom_line = (image_left, image_top + image_height)
right_line = (image_left + image_width, image_top)
# print("top: {}".format(image_top))
# print("left: {}".format(image_left))
# print("width: {}".format(image_width))
# print("height: {}".format(image_height))
# print("upper_line: {}".format(upper_line))
# print("bottom_line: {}".format(bottom_line))
# print("right_line: {}".format(right_line))
horizontal_line = pygame.image.load(r'C:\Users\Wit\PycharmProjects\LauEnv\Python learning\img\horizontalline.png')
horizontal_line = pygame.transform.scale(horizontal_line, (int(image_width), 5))
vertical_line = pygame.image.load(r'C:\Users\Wit\PycharmProjects\LauEnv\Python learning\img\verticalline.png')
vertical_line = pygame.transform.scale(vertical_line, (5, int(image_height)))
image_ = pygame.transform.scale(image_, (512, 512))
size = (512, 512)
screen = pygame.display.set_mode(size)
emotion = max(response[x]["faceAttributes"]["emotion"].items(), key=operator.itemgetter(1))[0]
emotion = pygame.font.Font('freesansbold.ttf', int(image_height / 10)).render(emotion, True, [0, 0, 0], [255, 255, 255])
age = str(int(response[x]["faceAttributes"]["age"]))
age = pygame.font.Font('freesansbold.ttf', int(image_height / 10)).render(age, True, [0, 0, 0], [255, 255, 255])
gender = response[x]["faceAttributes"]["gender"]
gender = pygame.font.Font('freesansbold.ttf', int(image_height / 10)).render(gender, True, [0, 0, 0], [255, 255, 255])
screen.blit(image_, (0, 0))
screen.blit(horizontal_line, upper_line)
screen.blit(horizontal_line, bottom_line)
screen.blit(vertical_line, upper_line)
screen.blit(vertical_line, right_line)
screen.blit(emotion, (upper_line[0], upper_line[1] - int(image_height / 8)))
screen.blit(age, (upper_line[0], upper_line[1] - int(image_height / 8) * 2))
screen.blit(gender, (upper_line[0], upper_line[1] - int(image_height / 8) * 3))
人臉列表/dic:
[{'faceRectangle': {'top': 157, 'left': 48, 'width': 349, 'height': 349}, 'faceAttributes': {'gender': 'female', 'age': 18.0,
'emotion': {'anger': 0.0, 'contempt': 0.0, 'disgust': 0.0, 'fear': 0.0,
'happiness': 0.0, 'neutral': 0.999, 'sadness': 0.0,
'surprise': 0.0}}},
{'faceRectangle': {'top': 144, 'left': 594, 'width': 341, 'height': 341}, 'faceAttributes': {'gender': 'male', 'age': 62.0,
'emotion': {'anger': 0.023, 'contempt': 0.054, 'disgust': 0.0, 'fear': 0.0,
'happiness': 0.0, 'neutral': 0.922, 'sadness': 0.001,
'surprise': 0.0}}}]
解決它。 我將“screen.blit(image_, (0, 0))”放在覆蓋每個正方形的循環中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.