我是Python编程的新手,正在编写一个程序,在其中将裁剪输入的图像,然后将其保存在某个位置。现在,我能够使用PIL和pygame的组合来完成这项工作。但问题在于,当我从打开的pygame窗口中选择图像时,选择区域完全不透明,我无法透过所选区域看到下面的内容。这给我的老板带来了问题,他希望在选择时能够看到下面的内容。为了让大家更好地理解问题,我在这里写出了我的代码:
import pygame, sys
from PIL import Image
pygame.init()
def displayImage( screen, px, topleft):
screen.blit(px, px.get_rect())
if topleft:
pygame.draw.rect( screen, (128,128,128), pygame.Rect(topleft[0], topleft[1], pygame.mouse.get_pos()[0] - topleft[0], pygame.mouse.get_pos()[1] - topleft[1]))
pygame.display.flip()
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 = None
bottomright = 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
displayImage(screen, px, topleft)
return ( topleft + bottomright )
if __name__ == "__main__":
input_loc="C:\pic1.PNG"
output_loc="C:\pic2.PNG"
screen, px = setup(input_loc)
left, upper, right, lower = mainLoop(screen, px)
im = Image.open(input_loc)
im = im.crop(( left, upper, right, lower))
pygame.display.quit()
im.save(output_loc)
非常感谢您的帮助。顺祝商祺。
fill()
之后你会使用pygame.draw.rect()
- 我已经相应地更新了答案。 - samplebiasscreen = pygame.display.set_mode((int(px.get_rect()[2]*1.2),int(px.get_rect()[3]*1.2)))
screen.blit(px,(px.get_rect()[0],px.get_rect()[1],int(px.get_rect()[2]*1.2),int(px.get_rect()[3]*1.2)))
但是它仍然没有给我一个缩放后的图像。 - Arnab Ghosal