简体   繁体   English

为什么图像对角线旋转?

[英]Why is image rotated diagonally?

Recently I tried to use ImageGrab and replicate the screen.But when I writed following code and runed it after drawing Image was rotated diagonally.最近我尝试使用 ImageGrab 并复制屏幕。但是当我编写以下代码并在绘制图像对角线旋转后运行它时。 Does anyone know why this happens?Is problem with ImageGrab, pygame or something else?有谁知道为什么会这样?是 ImageGrab、pygame 还是其他问题?

This is my code so far:到目前为止,这是我的代码:

from PIL import ImageGrab
import pyautogui
import pygame

colors=[]
WIDTH,HEIGHT=pyautogui.size()
image=ImageGrab.grab()
for x in range(0,WIDTH,5):
    for y in range(0,HEIGHT,5):
        color=image.getpixel((x,y))
        colors.append(color)
screen=pygame.display.set_mode((WIDTH,HEIGHT))
pixel_y=-5
pixel_x=0
for k,v in enumerate(colors):
    pixel_y+=5
    if pixel_y>HEIGHT:
        pixel_x+=5
        pixel_y=0
    pixel=pygame.Rect(pixel_x,pixel_y,5,5)
    pygame.draw.rect(screen,v,pixel)
    pygame.display.update(pixel)

You are drawing too many pixels in one column.您在一列中绘制了太多像素。 Change:改变:

if pixel_y>HEIGHT:

if pixel_y >= HEIGHT:

Simplify your code using the // (floor division) operator and the % (modulo) operator (the % operator computes the remainder of an integral division):使用// (底除法)运算符和% (取模)运算符简化您的代码( %运算符计算整数除法的余数):

h = HEIGHT // 5
for k, v in enumerate(colors):
    pixel = pygame.Rect(k // h * 5, k % h * 5, 5, 5)
    pygame.draw.rect(screen, v, pixel)
    pygame.display.update(pixel)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM