简体   繁体   English

Python中的数字图像裁剪

[英]Digital Image cropping in Python

Got this question from a professor, a physicist. 从一位教授,一位物理学家那里得到了这个问题。

I am a beginner in Python programming. 我是Python编程的初学者。 I am not a computer professional I am a physicist. 我不是计算机专业人士我是物理学家。 I was trying to write a code in python for my own research which involves a little image processing. 我试图在python中编写一个代码用于我自己的研究,这涉及一些图像处理。

All I need to do is to display an image and then select a region of interest using my mouse and finally crop out the selected region. 我需要做的就是显示图像,然后使用鼠标选择感兴趣的区域,最后裁剪出所选区域。 I can do this in Matlab using the ginput() function. 我可以使用ginput()函数在Matlab中完成此操作。

I tried using PIL. 我尝试过使用PIL。 But I find that after I issue the command Image.show(), the image is displayed but then the program halts there unless I exit from the image window. 但是我发现在发出命令Image.show()之后,显示图像但是程序暂停,除非我从图像窗口退出。 Is there any way to implement what I was planning. 有没有办法实现我的计划。 Do I need to download any other module? 我需要下载任何其他模块吗? Please advise. 请指教。

While I agree with David that you should probably just use GIMP or some other image manipulation program, here is a script (as I took it to be an exercise to the reader) using pygame that does what you want. 虽然我同意大卫你应该只使用GIMP或其他一些图像处理程序,但这是一个脚本(因为我把它作为练习给读者)使用pygame来做你想要的。 You will need to install pygame as well as the PIL, usage would be: 您将需要安装pygame以及PIL,用法如下:

scriptname.py <input_path> <output_path>

Actual script: 实际脚本:

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
    runProgram = True
    while runProgram:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                runProgram = False
            elif event.type == pygame.MOUSEBUTTONUP:
                if not topleft:
                    topleft = event.pos
                else:
                    bottomright = event.pos
                    runProgram = False
        displayImage(screen, px, topleft)
    return ( topleft + bottomright )


if __name__ == "__main__":
    screen, px = setup(sys.argv[1])
    left, upper, right, lower = mainLoop(screen, px)
    im = Image.open(sys.argv[1])
    im = im.crop(( left, upper, right, lower))
    im.save(sys.argv[2])

Hope this helps :) 希望这可以帮助 :)

For what it's worth (coming from another physicist), I would just do this in an image processing program like the GIMP . 对于它的价值(来自另一位物理学家),我会在像GIMP这样的图像处理程序中这样 The main benefit of doing this task in Python (or any language) would be to save time by automating the process, but unless you - well, the professor - can somehow develop an algorithm to automatically figure out what part of the image to crop, there doesn't seem to be much time to be saved by automation. 使用Python(或任何语言)执行此任务的主要好处是通过自动化过程来节省时间,但除非您 - 教授 - 能够以某种方式开发一种算法来自动确定要裁剪的图像部分,似乎没有太多时间可以通过自动化来保存。

If I remember correctly, GIMP is actually scriptable, possibly with Python, so it might be possible to write a time-saving GIMP script to do what your professor friend wants. 如果我没记错的话,GIMP实际上是可编写脚本的,可能是Python,因此可以编写一个节省时间的GIMP脚本来完成教授朋友想要的操作。

Image.show() just calls whatever simple picture viewer it can find on the current platform, one that may or may not have a crop-and-save facility. Image.show()只调用它可以在当前平台上找到的任何简单的图片查看器,一个可能有也可能没有裁剪和保存工具的平台。

If you are on a Windows box and you just need to make it work on your machine, set the 'Open with...' association to make it so running an image loads it into an editor of your choice. 如果您在Windows机器上并且只需要在机器上工作,请设置“打开方式...”关联以使其运行图像将其加载到您选择的编辑器中。 On OS X and *nix you'd want to hack the _showxv() method at the bottom of Image.py to change the command used to open the image. 在OS X和* nix上,你想要破解Image.py底部的_showxv()方法来更改用于打开图像的命令。

If you do actually need to provide a portable solution, you'll need to use a UI framework to power your cropping application. 如果您确实需要提供便携式解决方案,则需要使用UI框架来为裁剪应用程序提供支持。 The choices boil down to Tkinter (ImageTk.py gives you a wrapper for displaying PIL images in Tk), PyQT4 (ImageQt in PIL 1.1.6 gives you a wrapper for displaying images in QT4) or wxPython (a higher-level application authoring toolkit using wxWidgets). 的选择归结为Tkinter的 (ImageTk.py给你的包装在Tk的显示图像PIL), PyQt4的 (ImageQt在PIL 1.1.6给您QT4显示图像的包装纸)或wxPython的 (更高级别的应用程序编写工具包使用wxWidgets)。 It'll be quite a bit of work to get the hang of a full UI kit, but you'll be able to completely customise how your application's interface will work. 掌握完整的UI工具包将会有相当多的工作,但您将能够完全自定义应用程序界面的工作方式。

在python中是否有像自动裁剪图像的库中的脚本: 自动裁剪图像

What you are looking for is the module: matplotlib, it emulates Matlab. 您正在寻找的是模块:matplotlib,它模拟Matlab。 See the ginput() function. 请参阅ginput()函数。 That allow you to find the bounding box, then you can use crop from PIL. 这允许您找到边界框,然后您可以使用PIL中的裁剪。

http://matplotlib.sourceforge.net/api/figure_api.html http://matplotlib.sourceforge.net/api/figure_api.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM