使用 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)
感谢任何帮助。问候。
I am new to Python coding and I am writing a program in which I will be cropping an entered image and then saving it in a location. Now, I am being able to do this using a combination of PIL and pygame. But the problem is that, when I am selecting the image from the open pygame window, the selection area is totally opaque and I am not able to see through the region I am selecting. This is causing problems for my boss who wants to be able to see through it as he selects. For you guys to better understand the problem, I am writing my code here:
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)
Any help is appreciated. Regards.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我快速浏览了一下并解决了其他一些问题。基本上我的更改是这样做的:
这是运行固定代码的屏幕截图:
我将代码分成两部分以避免滚动条:
第 2 部分(连接到上面):
I took a quick look and fixed a few other problems along the way. Essentially my changes do this:
Here is a screenshot of running the fixed code:
I split the code into two parts to avoid the scrollbars:
And part 2 (concatenate to the above):
Samplebias 的答案对我来说非常好。我通过添加缩放和平移功能将其扩展到我正在处理的大图像。
使用提供的main函数samplebias。
感谢堆栈溢出!
The answer by samplebias was great for me. I extended it for the large images I am working with by adding zoom and pan capabilities.
Use the main function samplebias provided.
Thanks Stack Overflow!