我有8位灰度TIFF图像,我想使用75%白色(十进制190)阈值转换为单色.在Image.convert(模式)方法部分,PIL手册说:
"将灰度图像转换为位级图像(模式"1")时,所有非零值都设置为255(白色).要使用其他阈值,请使用点法."
Image.point(table)方法表示它通过给定的表映射每个像素.
im.point(table,mode)=> image
im.point(function,mode)=> image"通过表格映射图像,并在飞行中进行转换.在当前版本的PIL中,这只能用于在一步中将'L'和'P'图像转换为'1',例如对图像进行阈值处理."
如何创建与我需要的75%阈值相对应的表(或函数)?
我试图在这个问题中使用答案,但无法使其工作:如何使用Python的ElementTree创建"虚拟根目录"?
这是我的代码:
import xml.etree.cElementTree as ElementTree
from StringIO import StringIO
s = '<?xml version=\"1.0\" encoding=\"UTF-8\" ?><!DOCTYPE tmx SYSTEM \"tmx14a.dtd\" ><tmx version=\"1.4a\" />'
tree = ElementTree.parse(StringIO(s)).getroot()
header = ElementTree.SubElement(tree,'header',{'adminlang': 'EN',})
body = ElementTree.SubElement(tree,'body')
ElementTree.ElementTree(tree).write('myfile.tmx','UTF-8')
Run Code Online (Sandbox Code Playgroud)
当我打开生成的'myfile.tmx'文件时,它包含以下内容:
<?xml version='1.0' encoding='UTF-8'?>
<tmx version="1.4a"><header adminlang="EN" /><body /></tmx>
Run Code Online (Sandbox Code Playgroud)
我错过了什么?或者,有更好的工具吗?
一个Python进程将状态更新写入文件以供其他进程读取.在某些情况下,状态更新会在循环中重复且快速地发生.最简单快捷的方法是在一行中使用open().write():
open(statusfile,'w').write(status)
Run Code Online (Sandbox Code Playgroud)
另一种方法是使用四行强制数据到磁盘.这会显着降低性能:
f = open(self.statusfile,'w')
f.write(status)
os.fsync(f)
f.close()
Run Code Online (Sandbox Code Playgroud)
我不是想避免操作系统崩溃.那么,该方法是否会将数据强制到OS缓冲区,以便其他进程在从磁盘打开文件时读取最新的状态数据?或者,我需要使用os.fsync()吗?
我正在将一个Bash脚本移植到Python.该脚本设置LC_ALL=C并使用Linux sort命令来确保本机字节顺序而不是特定于语言环境的排序顺序(http://stackoverflow.com/questions/28881/why-doesnt-sort-sort-the-same-on-every - 机器).
在Python中,我想使用Python的列表sort()或sorted()函数(没有key=选项).我总能得到与Linux排序相同的结果LC_ALL=C吗?
在 Linux 上,我使用 subbprocess.Popen 来运行应用程序。该应用程序的命令行需要输入文件的路径。我了解到我可以将路径 /dev/stdin 传递到命令行,然后使用 Python 的 subproc.stdin.write() 将输入发送到子进程。
import subprocess
kw['shell'] = False
kw['executable'] = '/path/to/myapp'
kw['stdin'] = subprocess.PIPE
kw['stdout'] = subprocess.PIPE
kw['stderr'] = subprocess.PIPE
subproc = subprocess.Popen(['','-i','/dev/stdin'],**kw)
inbuff = [u'my lines',u'of text',u'to process',u'go here']
outbuff = []
conditionbuff = []
def processdata(inbuff,outbuff,conditionbuff):
for i,line in enumerate(inbuff):
subproc.stdin.write('%s\n'%(line.encode('utf-8').strip()))
line = subproc.stdout.readline().strip().decode('utf-8')
if 'condition' in line:
conditionbuff.append(line)
else:
outbuff.append(line)
processdata(inbuff,outbuff,conditionbuff)
Run Code Online (Sandbox Code Playgroud)
该应用程序还有 MS Windows 版本。MS Windows 上是否有与使用 /dev/stdin 等效的方法,或者是 Linux (Posix) 特定的解决方案?
我们准备转向Python 3.4并添加unicode_literals.我们的代码使用子进程模块广泛依赖于与外部实用程序的管道连接.以下代码片段在Python 2.7上正常工作,可将UTF-8字符串传递给子进程:
kw = {}
kw[u'stdin'] = subprocess.PIPE
kw[u'stdout'] = subprocess.PIPE
kw[u'stderr'] = subprocess.PIPE
kw[u'executable'] = u'/path/to/binary/utility'
args = [u'', u'-l', u'nl']
line = u'¡Basta Ya!'
popen = subprocess.Popen(args,**kw)
popen.stdin.write('%s\n' % line.encode(u'utf-8'))
...blah blah...
Run Code Online (Sandbox Code Playgroud)
以下更改会抛出此错误:
from __future__ import unicode_literals
kw = {}
kw[u'stdin'] = subprocess.PIPE
kw[u'stdout'] = subprocess.PIPE
kw[u'stderr'] = subprocess.PIPE
kw[u'executable'] = u'/path/to/binary/utility'
args = [u'', u'-l', u'nl']
line = u'¡Basta Ya!'
popen = subprocess.Popen(args,**kw)
popen.stdin.write('%s\n' % line.encode(u'utf-8'))
Traceback (most recent call last):
File "test.py", line 138, in …Run Code Online (Sandbox Code Playgroud) 我正在处理使用空格作为千位分隔符的文本,例如400或40 000或40 000 000或4 000 000 000.我需要识别字符串中的数字.一旦确定,有许多选项可以重新格式化数字.我是正则表达式的新秀.这不起作用:
import re
line = '40) He had 120 hours to increase from 40 000 units to 20 000 000.'
regex = re.compile("(\d+ *\d+)")
re.findall(regex, line)
['40', '120', '40 000', '20 000', '000']
Run Code Online (Sandbox Code Playgroud) 我想选择一个随机数,并以彩票系统的格式表示该数字.例如,彩票系统有7组2位数字,从01到40,总可能组合为163,840,000,000(40到7次方).如果我选择一个随机的基数为10的数字,比如说453,867,221,我如何在01-40的7组2位数字中表示?
我选择的编程语言是Python,尽管任何语言/伪语言都会有所帮助.
我正在尝试使用 cElementTree 保存编码为 UTF-16 的 XML 文件。这是同一个项目,但与以下中的 DOCTYPE 问题不同:How to create <!DOCTYPE> with Python's cElementTree
我了解到,如果我没有在字符串中声明编码,cElementTree 将添加它。所以,代码是这样的:
import xml.etree.cElementTree as ElementTree
from StringIO import StringIO
s = '<?xml version=\"1.0\" ?><!DOCTYPE tmx SYSTEM \"tmx14a.dtd\" ><tmx version=\"1.4a\" />'
tree = ElementTree.parse(StringIO(s)).getroot()
header = ElementTree.SubElement(tree,'header',{'adminlang': 'EN',})
body = ElementTree.SubElement(tree,'body')
ElementTree.ElementTree(tree).write('myfile.tmx','UTF-16')
Run Code Online (Sandbox Code Playgroud)
当我用 UTF-8 编写文件时,一切都很好。但是,当我更改为 UTF-16 时,文本编码已损坏。它还缺少所需的字节顺序标记。当我尝试将 BOM 添加到字符串的开头时,
s = '\xFF\xFE<?xml version=\"1.0\"......
Run Code Online (Sandbox Code Playgroud)
ElementTree 报告错误“格式不正确(无效标记)第 1 行第 1 列”。
所有缓冲区都是 unicode 数据。如何保存为 UTF-16 XML 文件?