小编Arn*_*sal的帖子

使用Python进行图像裁剪

我是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) …
Run Code Online (Sandbox Code Playgroud)

python crop image-processing python-imaging-library

6
推荐指数
1
解决办法
1万
查看次数