2

在我尝试使用 python 2.7 探索期间,我scipy制作了以下简单脚本:

#!/usr/bin/env python
# coding=utf-8
# -*- Mode: python; c-basic-offset: 4 -*-

from scipy import ndimage
import numpy as np
from scipy import misc
import argparse
import Image

def getArguments():
    parser = argparse.ArgumentParser(description="An simple Image processor")
    parser.add_argument('image_file', metavar="FILENAME", type=str,
                        help="The image file that will be read In order to be processed")
    return parser.parse_args()


def getImagePathFromArguments():
    '''
    :return: string
    '''
    args = getArguments()
    return args.image_file


def loadImage(image):
    '''
    :param image: The path of the Image
    :return: 
    '''
    return misc.imread(image)


def grayscale(imgData):
    #Greyscale action done here
    pass


def blur(imgData):
    '''
    :param nparray imgData: 
    :return: 
    '''
    return ndimage.gaussian_filter(imgData, 1)

def saveImage(path, imgData):
    im = Image.fromarray(imgData)
    im.save(path)


def main():
    imagePath = getImagePathFromArguments()
    print "Loading Image from %s" % (imagePath,)
    image = loadImage(imagePath)

    while True:

        print "Select \n"
        print "1. Greyscale"
        print "2. Bluring"
        option = int(raw_input("Please do your option: "))

        if (option != 1 and option != 2):
            print "Wrong Option"
        else:
            processedData=0
            if option == 1:
                processedData = grayscale(image)
            elif option == 2:
                print "Bluring Image"
                processedData = blur(image)

            saveImagePath = raw_input("Where to you want to store the image?")
            saveImage(saveImagePath, processedData)
            break


if __name__ == "__main__":
    main()

这对图像进行了简单的处理,例如模糊和灰度。我设法从已经加载的图像中进行模糊处理,但是灰度呢?

我发现的最接近的是如何在 Python 中将 RGB 图像转换为灰度?但是他们没有提供使用 ndimage 的解决方案。

ndimage 也可以在打开期间转换,而不是使用已经打开的图像。

我还尝试使用http://ebanshi.cc/questions/108516/convert-rgb-image-to-grayscale-in-pythongreyscale中看到的方法来实现该方法:

def grayscale(imgData):
    r=imgData[:,:,0]
    g=imgData[:,:,1]
    b=imgData[:,:,2]
    return  r * 299. / 1000 + g * 587. / 1000 + b * 114. / 1000

但我收到以下错误:

回溯(最后一次调用):文件“/home/pcmagas/Kwdikas/python/Basic/scripy/scipy_image_examples.py”,第 83 行,在 main() 文件“/home/pcmagas/Kwdikas/python/Basic/scripy/ scipy_image_examples.py”,第 78 行,在主 saveImage(saveImagePath, processedData) 文件中“/home/pcmagas/Kwdikas/python/Basic/scripy/scipy_image_examples.py”,第 52 行,在 saveImage im.save(path) 文件中“/ usr/lib/python2.7/dist-packages/PIL/Image.py”,第 1675 行,保存 save_handler(self, fp, filename) 文件“/usr/lib/python2.7/dist-packages/PIL/PngImagePlugin .py",第 682 行,在 _save 中引发 IOError("cannot write mode %s as PNG" % mode) IOError: cannot write mode F as PNG

有任何想法吗?

4

2 回答 2

2

Dimitris 您的解决方案不起作用,因为您尝试使用无效模式保存文件。F 代表 32 位浮点像素,当您调用saveImage图像数据时仍处于 F 模式。您可以通过print im.modesaveImage函数中添加行来自行检查:

有关PIL 库上的所有模式,请参阅http://pillow.readthedocs.io/en/3.4.x/handbook/concepts.html#modes

要解决此问题,您只需convert('RGB')在保存前调用将图像数据再次转换为 RGB 模式。

http://pillow.readthedocs.io/en/3.4.x/reference/Image.html#PIL.Image.Image.convert

于 2017-05-29T08:56:44.877 回答
0

最后我需要创建自己的函数。这并不难:

def grayscale(imgData):
    r=imgData[:,:,0]
    g=imgData[:,:,1]
    b=imgData[:,:,2]
    return  r/3 + g /3 + b/3
于 2017-05-26T19:13:22.557 回答