我正在使用Python Imaging Library进行一些非常简单的图像处理,但是我无法将灰度图像转换为单色(黑白)图像.如果我在将图像更改为灰度(转换('L'))后保存,则图像呈现为您所期望的.但是,如果我将图像转换为单色,单波段图像,它只会给我噪声,如下图所示.有没有一种简单的方法可以使用PIL/python将彩色png图像转换为纯黑白图像?
from PIL import Image
import ImageEnhance
import ImageFilter
from scipy.misc import imsave
image_file = Image.open("convert_image.png") # open colour image
image_file= image_file.convert('L') # convert image to monochrome - this works
image_file= image_file.convert('1') # convert image to black and white
imsave('result_col.png', image_file)
Run Code Online (Sandbox Code Playgroud)

我的应用程序的用户尝试使用我的应用程序将文件作为电子邮件附件发送.但是,这样做会引发以下异常,我无法解密
Exceeded soft private memory limit with 192.023 MB after servicing
2762 requests total
While handling this request, the process that handled this request was
found to be using too much memory and was terminated. This is likely to
cause a new process to be used for the next request to your application.
If you see this message frequently, you may have a memory leak in
your application.
Run Code Online (Sandbox Code Playgroud)
什么是"软私有内存限制"以及可能导致此异常的原因是什么?
我有几个单色图像(黑色和白色而不是灰度),有一些奇形怪状的物体.我正在尝试使用python27,PIL,scipy&numpy以及以下方法提取每个对象:
我看过http://www.scipy.org/Cookbook/Watershed和http://scikits-image.org/docs/dev/auto_examples/plot_contours.html这些确实有效,但我特别喜欢希望边界框是矩形的,以确保任何"略微断开"的位都包含在边界框中.理想情况下,为了处理断开的位(例如左下方的blob),我会有某种阈值控制.关于什么工具箱最适合这个的任何想法?

我正在尝试从appengine应用程序向dotcloud上托管的外部(django)api发送多部分帖子请求.该请求包括一些文本和文件(pdf),并使用以下代码发送
from google.appengine.api import urlfetch
from poster.encode import multipart_encode
from libs.poster.streaminghttp import register_openers
register_openers()
file_data = self.request.POST['file_to_upload']
the_file = file_data
send_url = "http://127.0.0.1:8000/"
values = {
'user_id' : '12341234',
'the_file' : the_file
}
data, headers = multipart_encode(values)
headers['User-Agent'] = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
data = str().join(data)
result = urlfetch.fetch(url=send_url, payload=data, method=urlfetch.POST, headers=headers)
logging.info(result.content)
Run Code Online (Sandbox Code Playgroud)
当此方法运行时,Appengine会发出以下警告(我不确定它是否与我的问题有关)
Stripped prohibited headers from URLFetch request: ['Content-Length']
Run Code Online (Sandbox Code Playgroud)
并且Django通过以下错误发送
<class 'django.utils.datastructures.MultiValueDictKeyError'>"Key 'the_file' not found in <MultiValueDict: {}>"
Run Code Online (Sandbox Code Playgroud)
django代码非常简单,当我使用postman chrome扩展程序发送文件时可以使用.
@csrf_exempt
def index(request):
try:
user_id = …Run Code Online (Sandbox Code Playgroud) 我的问题是,创建新模型实体的最佳方法是什么,然后立即阅读.例如,
class LeftModel(ndb.Model):
name = ndb.StringProperty(default = "John")
date = ndb.DateTimeProperty(auto_now_add=True)
class RightModel(ndb.Model):
left_model = ndb.KeyProperty(kind=LeftModel)
interesting_fact = ndb.StringProperty(default = "Nothing")
def do_this(self):
# Create a new model entity
new_left = LeftModel()
new_left.name = "George"
new_left.put()
# Retrieve the entity just created
current_left = LeftModel.query().filter(LeftModel.name == "George").get()
# Create a new entity which references the entity just created and retrieved
new_right = RightModel()
new_right.left_model = current_left.key
new_right.interesting_fact = "Something"
new_right.put()
Run Code Online (Sandbox Code Playgroud)
这经常抛出一个例外:
AttributeError: 'NoneType' object has no attribute 'key'
Run Code Online (Sandbox Code Playgroud)
即,检索新的LeftModel实体是不成功的.我用appengine几次遇到这个问题,我的解决方案总是有点hacky.通常我只是将所有内容放在try while或while循环中,直到成功检索到实体.如何确保始终检索模型实体而不运行无限循环的风险(在while循环的情况下)或弄乱我的代码(在try语句除外的情况下)?
我花了很多时间阅读有关使用谷歌地图api并将下面的代码放在一起的消息.代码首先以特定位置为中心,然后将地图中心更改为用户当前位置 - 使用第二个标记突出显示该位置.然后以5s的间隔刷新第二个标记的位置,而不重新定位地图.这在不同的设备和浏览器上有不同的作用,我想知道如何使它更具跨设备兼容性.
======================================================================================================================
Device Browser Display map Display map marker Display current location
======================================================================================================================
PC Chrome Yes Yes Yes (if allowed)
----------------------------------------------------------------------------------------------------------------------
iPhone 3 iOS 5 Yes Yes No
----------------------------------------------------------------------------------------------------------------------
Nokia n97 Opera Mobile Yes Yes Yes
----------------------------------------------------------------------------------------------------------------------
Nokia n97 Native symbian browser Yes, though hybrid map is poor No It detects the current location and centres the map there, but doesn't display the image.
----------------------------------------------------------------------------------------------------------------------
Run Code Online (Sandbox Code Playgroud)
我需要在我自己的网站上托管地图,以确保使用我的自定义图标等正确呈现地图.
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<title>mysite - …Run Code Online (Sandbox Code Playgroud) 我正在努力获得简单的音频播放。我有一个曲目列表,每个曲目都有一个唯一的URL,当用户按下相关的播放按钮时,我想播放这些曲目。我已经尝试过soundmanager2和jplayer,但无法在我的用例(台式浏览器和ios)上使用。我已经回到使用代码的html5如下
<audio controls>
<source src="/path/to/file" type="audio/wav">
Your browser does not support the audio element.
</audio>
Run Code Online (Sandbox Code Playgroud)
这在台式机Chrome和台式机Safari中非常有效。在ios chrome和ios Safari(最新)中,不会显示播放器,而是会在灰色框中显示一条消息,提示“无法播放音频文件”。
我是否正确使用此标签?我该如何克服这些错误?
我正在从Google Appengine发送WAV文件(作为Blob)。我发现,除非我将.wav添加到src的末尾,否则safari无法播放-尽管src只是指向文件的间接链接。返回的实际文件确实以.wav结尾,但Safari不够智能,无法识别出该文件。
以下内容适用于所有浏览器(如上所述)-因此它并不特定于wav文件。
<audio controls preload="metadata">
<source src="http://www-mmsp.ece.mcgill.ca/Documents/AudioFormats/WAVE/Samples/AFsp/M1F1-Alaw-AFsp.wav" type="audio/wav">
Your browser does not support the audio element.
</audio>
Run Code Online (Sandbox Code Playgroud)
我已经从Google App Engine发送文件作为MIME附件和原始响应,但这没什么区别。
我换了一个更长的wav文件(http://www.villagegeek.com/downloads/webwavs/ever_again.wav),这也无法播放(在ios上)。目前尚不清楚是因为长度,大小还是其他一些变量。
我已经排除了大小问题,因为此24s WAV文件有效
<audio controls preload="auto">
<source src="http://www.dailywav.com/sites/default/files/wavs/dontlikelaughing.wav" type="audio/wav">
Your browser does not support the audio element.
</audio>
Run Code Online (Sandbox Code Playgroud)
因此,我正在从Google云存储桶中提供文件。保存文件时,我未指定MIME类型,因此将其作为二进制/八位字节流返回。桌面浏览器足够聪明,可以克服这一问题,而移动浏览器则不能。
我在没有 npm 的情况下使用 React(由于服务器端限制)。在我的<head>标签中,我有以下内容
<script src="https://unpkg.com/react@15.4.2/dist/react.js"></script>
<script src="https://unpkg.com/react-dom@15.4.2/dist/react-dom.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-core/5.8.34/browser.js"></script>
Run Code Online (Sandbox Code Playgroud)
我的反应代码被打包在一个text/babel脚本标签中并且运行良好。简化示例:
<script type="text/babel">
ReactDOM.render(
<h1>Hello, world!</h1>,
document.getElementById('root')
);
</script>
Run Code Online (Sandbox Code Playgroud)
我想使用外部模块来绘制图表。但是,如果我使用https://github.com/reactjs/react-chartjs(只需下载并在本地提供服务),包括
任何一个
<script type="text/javascript" src="/js/react-chartjs/chart.js"></script>
Run Code Online (Sandbox Code Playgroud)
或者
<script type="text/babel" src="/js/react-chartjs/chart.js"></script>
Run Code Online (Sandbox Code Playgroud)
抛出错误:
ReferenceError: require is not defined
Run Code Online (Sandbox Code Playgroud)
大概是因为require是一个 npm 命令。我试过使用 require.js 但同样会引发错误。同样,如果我从
https://cdnjs.cloudflare.com/ajax/libs/Chart.js/1.0.2/Chart.min.js
Run Code Online (Sandbox Code Playgroud)
并在我的text/babel脚本标签中尝试将库与任何
// Using CommonJS
var Chart = require('chart.js')
var myChart = new Chart({...})
// ES6
import Chart from 'chart.js'
let myChart = new Chart({...})
// Using requirejs
require(['path/to/Chartjs'], function(Chart){
var …Run Code Online (Sandbox Code Playgroud) 我正在尝试在对象内的数组中本地存储对象.
如果我在我的控制台中尝试以下操作,它可以完美地工
theObject = {}
theObject.theArray = []
arrayObj = {"One":"111"}
theObject.theArray.push(arrayObj)
Run Code Online (Sandbox Code Playgroud)
但是,如果我按照我认为相同的方式执行操作,除了将结果存储在localStorage中之外,它将失败:
localStorage.localObj = {}
localStorage.localObj.localArray = []
stringArrayObj = JSON.stringify(arrayObj)
localStorage.localObj.localArray.push(stringArrayObj)
Run Code Online (Sandbox Code Playgroud)
我收到以下错误...
localStorage.localObj.localArray.push(stringArrayObj)
TypeError
arguments: Array[2]
message: "—"
stack: "—"
type: "non_object_property_call"
__proto__: Error
Run Code Online (Sandbox Code Playgroud)
知道我怎么能让这个工作吗?
干杯
背景
我正在尝试为我的twilio应用程序实施呼叫筛选 - 即一个人按下一个键接受呼叫.我已经看到了几个这样做的例子(例如,如何使用twilio来保证实时答案或语音邮件?)但是,对于将呼叫转发到多个号码的情况,给定的答案使用循环方法.
这个问题
有没有办法让所有人同时打电话,第一个通过挑战的人说话,所有其他电话断开连接?
为什么?
我想这样做是因为如果呼叫仅由循环中的最后一个人回答,则呼入呼叫者将需要等待一段时间.
python ×6
javascript ×2
python-2.7 ×2
chart.js ×1
client-side ×1
django ×1
html5 ×1
html5-audio ×1
ios ×1
numpy ×1
opera-mobile ×1
post ×1
reactjs ×1
scipy ×1
twilio ×1
twilio-twiml ×1