使用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)
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)
任何帮助表示赞赏。问候。
没有找到相关结果
已邀请:
2 个回复
雇砰
第2部分(与以上内容串联):
惭法搽
使用提供的主要功能samplebias。 谢谢堆栈溢出!