Pygame无法打开某些图像

时间:2019-04-13 10:39:25

标签: python tkinter pygame python-imaging-library

我正在pygame的帮助下裁剪图像,即使两个图像都在同一位置,它也会打开一些图像,而其他图像却给出错误

我是Python的新手,并且从stackoverflow获得了大部分代码 俗话说,要添加更多详细信息,以便我输入随机的东西,到目前为止似乎还不是那么随机


    def displayImage(screen, px, topleft, prior):
        # ensure that the rect always has positive width, height
        x, y = topleft
        width =  pygame.mouse.get_pos()[0] - topleft[0]
        height = pygame.mouse.get_pos()[1] - topleft[1]
        if width < 0:
            x += width
            width = abs(width)
        if height < 0:
            y += height
            height = abs(height)

        # eliminate redundant drawing cycles (when mouse isn't moving)
        current = x, y, width, height
        if not (width and height):
            return current
        if current == prior:
            return current

        # draw transparent box and blit it onto canvas
        screen.blit(px, px.get_rect())
        im = pygame.Surface((width, height))
        im.fill((128, 128, 128))
        pygame.draw.rect(im, (32, 32, 32), im.get_rect(), 1)
        im.set_alpha(128)
        screen.blit(im, (x, y))
        pygame.display.flip()

        # return current box extents
        return (x, y, width, height)

    def setup(path):
        px = pygame.image.load(path)
        screen = pygame.display.set_mode( px.get_rect()[2:] )
        screen.blit(px, px.get_rect())
        pygame.display.flip()
        return screen, px

    def mainLoop(screen, px):
        topleft = bottomright = prior = None
        n=0
        while n!=1:
            for event in pygame.event.get():
                if event.type == pygame.MOUSEBUTTONUP:
                    if not topleft:
                        topleft = event.pos
                    else:
                        bottomright = event.pos
                        n=1
            if topleft:
                prior = displayImage(screen, px, topleft, prior)
        return ( topleft + bottomright )

    if __name__ == "__main__":
        root=Tk()
        file = filedialog.askopenfilename(parent=root, initialdir="F:/",title='Choose an image.')
        img = Image.open(file)
        input_loc = os.path.basename(img.filename)
        output_loc = 'out.png'
        screen, px = setup(input_loc)
        left, upper, right, lower = mainLoop(screen, px)

        # ensure output rect always has positive width, height
        if right < left:
            left, right = right, left
        if lower < upper:
            lower, upper = upper, lower
        im = Image.open(input_loc)
        im = im.crop(( left, upper, right, lower))
        pygame.display.quit()
        im.save(output_loc)

        root.mainloop()

我收到此错误:

    Traceback (most recent call last):
      File "C:/Users/Admin/PycharmProjects/ashish/crop image.py", line 66, in <module>
        screen, px = setup(input_loc)
      File "C:/Users/Admin/PycharmProjects/ashish/crop image.py", line 39, in setup
        px = pygame.image.load(path)
    pygame.error: Couldn't open Screenshot(6).png

    Process finished with exit code 1

0 个答案:

没有答案