- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
在尝试使用 scipy
的 python 2.7 探索过程中,我制作了以下简单脚本:
#!/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()
这对图像进行简单的处理,例如模糊和灰度。我设法从已加载的图像中进行模糊处理,但灰度如何?
我找到的最接近的是 How can I convert an RGB image into grayscale in Python?但是他们没有提供使用 ndimage 的解决方案。
ndimage 也可以在打开期间进行转换,而不是使用已经打开的图像。
我还尝试实现 greyscale
方法,如 http://ebanshi.cc/questions/108516/convert-rgb-image-to-grayscale-in-python 中所示:
def grayscale(imgData):
r=imgData[:,:,0]
g=imgData[:,:,1]
b=imgData[:,:,2]
return r * 299. / 1000 + g * 587. / 1000 + b * 114. / 1000
但是我得到以下错误:
Traceback (most recent call last): File "/home/pcmagas/Kwdikas/python/Basic/scripy/scipy_image_examples.py", line 83, in main() File "/home/pcmagas/Kwdikas/python/Basic/scripy/scipy_image_examples.py", line 78, in main saveImage(saveImagePath, processedData) File "/home/pcmagas/Kwdikas/python/Basic/scripy/scipy_image_examples.py", line 52, in saveImage im.save(path) File "/usr/lib/python2.7/dist-packages/PIL/Image.py", line 1675, in save save_handler(self, fp, filename) File "/usr/lib/python2.7/dist-packages/PIL/PngImagePlugin.py", line 682, in _save raise IOError("cannot write mode %s as PNG" % mode) IOError: cannot write mode F as PNG
有什么想法吗?
最佳答案
Dimitris 您的解决方案不起作用,因为您正尝试使用无效模式保存文件。 F 用于 32 位浮点像素,当您调用 saveImage
时,图像数据仍处于 F 模式。您可以通过在 saveImage
函数中添加以下行来自行检查:print im.mode
。
参见 http://pillow.readthedocs.io/en/3.4.x/handbook/concepts.html#modes对于 PIL 库中的所有模式
要解决此问题,您只需在保存前调用 convert('RGB')
将图像数据再次转换为 RGB 模式。
http://pillow.readthedocs.io/en/3.4.x/reference/Image.html#PIL.Image.Image.convert
关于 python ndimage : Converting an existing ndarray into greyscale,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44206113/
我正在使用SpringBoot和JPA来调用db,我遇到异常 org.springframework.core.convert.ConverterNotFoundException: No conve
我尝试实现 Spring Converter,但在单元测试中出现错误: Kotlin: Null can not be a value of a non-null type TodoItem 如果我尝
我在 Spring Boot 2.0 示例中使用 Spring Data Redis。在此示例中,我尝试将客户数据 + 学生数据保存在一起。我不太确定这里的数据建模是如何发生的,但假设它与 Mongo
我在 Spring 的 XML 配置文件之一中有以下代码:
我们正在尝试使用 hibernate Converter 来加密/解密通过 hibernate 存储的几列数据 @Convert(attributeName="myattr",converter=Da
我有this我必须实现的功能: protected override ValidationResult IsValid( Object value, ValidationContext
我看到了 std::convert::Into有任何实现 std::convert::From 的实现: impl Into for T where U: From, 在Rust 1.0标准库
Convert.ChangeType 或 Convert.ToInt32 或 int.Parse 之间是否存在性能优势 最佳答案 如果您知道要将 string 转换为 Int32,使用 Convert
我会定期浏览我的家庭作业以供上课。我的扫描仪将原始 jpg 文件导出到 USB,然后我可以从那里使用 gimp 编辑文件并将其另存为 pdf。我发现一种节省时间的方法是将我的多页作业导出为 .mng
Grails版本:2.3.8我在BootStrap.groovy中注册了一个自定义日期编码器,但是当我使用日期填充为Json的Object时,它将引发异常:Exception message is C
我会定期浏览我的家庭作业以供上课。我的扫描仪将原始 jpg 文件导出到 USB,然后我可以从那里使用 gimp 编辑文件并将其另存为 pdf。我发现一种节省时间的方法是将我的多页作业导出为 .mng
我正在尝试制作一个 SKAction,以便我的玩家慢慢地被拉向一个要杀死他的敌人。实际上,问题在于玩家和敌人处于不同的节点,遵循以下层次结构: 场景(SKScene)-PARENT->播放器(SKNo
我通过 xml 设置了 spring data mongo 自定义转换器,如下所示 在自定义读/写转换器中,我想
我正在尝试使用名为 Simple Captcha 的 gem 这需要在机器上安装 ImageMagick。我已经安装了它并且 convert --version 显示了这个 Version: Imag
我正在尝试使用名为 Simple Captcha 的 gem 这需要在机器上安装 ImageMagick。我已经安装了它并且 convert --version 显示了这个 Version: Imag
我正在使用 Spring JPA,我需要有一个 native 查询来调用存储过程。从结果中,我只需要获取两个字段,即代码和消息。我创建了一个包含两个字段代码和消息的类。它不起作用,这是我收到的错误:
我首先有多部分文件,我想将其发送到camel管道并使用原始名称保存该文件。 我的代码: @Autowired ProducerTemplate producerTemplate; ...
我的maven项目使用了spring、hibernate。我得到“没有这样的方法错误”。我相信这是由于依赖项中的版本冲突造成的,但不知道是什么。构建成功。但是在“NetBeans:在 GlassFis
TL;DR:Vaadin 8 中是否有类似于 Vaadin 7 的转换器来更新 UI 中输入字段的表示? IE。在输入字段失去焦点后立即从用户输入中删除所有非数字,或将小数转换为货币? Vaadin
我昨天问了一个问题here关于从匿名对象读取属性并将它们写入类的私有(private)字段。问题解决了。这是一个小故事: 我有一些 json 格式的数据。我将它们反序列化为 ExpandoObject
我是一名优秀的程序员,十分优秀!