我们正在尝试将在 Linux 上运行良好的家庭作业的支持文件移植到 Windows。作业的一部分要求学生操作原始图像数据,支持文件使用 Python 在原始数据和图像文件之间进行转换。将图像文件转换为原始数据的代码是:
import os, sys
from PIL import Image
from struct import *
fileName = sys.argv[1]
try:
myImg = Image.open(fileName)
width,height = myImg.size
sys.stdout.write(pack("ii",width,height))
rgbImg = myImg.convert("RGB")
pixels = rgbImg.getdata()
for (r,g,b) in pixels:
sys.stdout.write(pack("BBB", r,g,b))
except IOError, e:
print >> sys.stderr, "%s: %s\n\nCannot open or understand %s" % (sys.argv[0], str(e), fileName)
而转换回来的代码是:
import os, sys
from PIL import Image
from struct import *
fileName = sys.argv[1]
try:
dimensions = sys.stdin.read(2*4)
width,height = unpack("ii", dimensions)
pixels = sys.stdin.read(3*width*height)
myImg = Image.frombytes("RGB", (width, height), pixels, "raw", "RGB", 0, 1)
myImg.save(fileName, "PNG")
except IOError, e:
print >> sys.stderr, "%s: %s\n\nCannot open or write to %s" % (sys.argv[0], str(e), fileName)
标准输出和输入被重定向到测试设施代码中的文件。该代码在 Linux 上运行良好,但在 Windows 上运行不佳。尝试在 Windows 上写入图像文件时,我们总是遇到以下错误:
Traceback (most recent call last):
File "image-rewrite.py", line 16, in <module>
myImg = Image.frombytes("RGB", (width, height), pixels, "raw", "RGB", 0, 1)
File "C:\Python27\lib\site-packages\PIL\Image.py", line 2100, in frombytes
im.frombytes(data, decoder_name, args)
File "C:\Python27\lib\site-packages\PIL\Image.py", line 742, in frombytes
raise ValueError("not enough image data")
ValueError: not enough image data
你知道发生了什么事吗?非常感谢。
最佳答案
在 Windows 上使用 stdin/stdout 传输二进制数据是个坏主意。 Windows 使用 CRLF ('\r\n' ) 作为行尾标记,并在输入时转换为 \n 并在输出时转换回来;翻译过程会对二进制数据造成严重破坏。
相反,您应该使用命名文件,并以二进制模式打开它们。
顺便说一句,在 Python 3 中,即使在 Linux 上,您也不能直接从/向 sys.stdin/sys.stdout 读取/写入二进制数据。相反,您需要使用 sys.stdin.buffer/sys.stdout.buffer 的 read/write 方法>.
关于python - 为什么我在 Windows 上从 Pillow 得到 "Not Enough Image Data",而同样的代码在 Linux 上运行良好?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41736152/
类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc
我需要在客户计算机上运行Ruby应用程序。通常需要几天才能完成(复制大备份文件)。问题是如果启用sleep,它会中断应用程序。否则,计算机将持续运行数周,直到我下次访问为止。有什么方法可以防止执行期间休眠并让Windows在执行后休眠吗?欢迎任何疯狂的想法;-) 最佳答案 Here建议使用SetThreadExecutionStateWinAPI函数,使应用程序能够通知系统它正在使用中,从而防止系统在应用程序运行时进入休眠状态或关闭显示。像这样的东西:require'Win32API'ES_AWAYMODE_REQUIRED=0x0
关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我想在固定时间创建一系列低音和高音调的哔哔声。例如:在150毫秒时发出高音调的蜂鸣声在151毫秒时发出低音调的蜂鸣声200毫秒时发出低音调的蜂鸣声250毫秒的高音调蜂鸣声有没有办法在Ruby或Python中做到这一点?我真的不在乎输出编码是什么(.wav、.mp3、.ogg等等),但我确实想创建一个输出文件。
我正在尝试测试是否存在表单。我是Rails新手。我的new.html.erb_spec.rb文件的内容是:require'spec_helper'describe"messages/new.html.erb"doit"shouldrendertheform"dorender'/messages/new.html.erb'reponse.shouldhave_form_putting_to(@message)with_submit_buttonendendView本身,new.html.erb,有代码:当我运行rspec时,它失败了:1)messages/new.html.erbshou
我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-
我有一个模型:classItem项目有一个属性“商店”基于存储的值,我希望Item对象对特定方法具有不同的行为。Rails中是否有针对此的通用设计模式?如果方法中没有大的if-else语句,这是如何干净利落地完成的? 最佳答案 通常通过Single-TableInheritance. 关于ruby-on-rails-Rails-子类化模型的设计模式是什么?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.co
我正在使用的第三方API的文档状态:"[O]urAPIonlyacceptspaddedBase64encodedstrings."什么是“填充的Base64编码字符串”以及如何在Ruby中生成它们。下面的代码是我第一次尝试创建转换为Base64的JSON格式数据。xa=Base64.encode64(a.to_json) 最佳答案 他们说的padding其实就是Base64本身的一部分。它是末尾的“=”和“==”。Base64将3个字节的数据包编码为4个编码字符。所以如果你的输入数据有长度n和n%3=1=>"=="末尾用于填充n%
我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i
为什么4.1%2返回0.0999999999999996?但是4.2%2==0.2。 最佳答案 参见此处:WhatEveryProgrammerShouldKnowAboutFloating-PointArithmetic实数是无限的。计算机使用的位数有限(今天是32位、64位)。因此计算机进行的浮点运算不能代表所有的实数。0.1是这些数字之一。请注意,这不是与Ruby相关的问题,而是与所有编程语言相关的问题,因为它来自计算机表示实数的方式。 关于ruby-为什么4.1%2使用Ruby返
为了将Cucumber用于命令行脚本,我按照提供的说明安装了arubagem。它在我的Gemfile中,我可以验证是否安装了正确的版本并且我已经包含了require'aruba/cucumber'在'features/env.rb'中为了确保它能正常工作,我写了以下场景:@announceScenario:Testingcucumber/arubaGivenablankslateThentheoutputfrom"ls-la"shouldcontain"drw"假设事情应该失败。它确实失败了,但失败的原因是错误的:@announceScenario:Testingcucumber/ar