1 回答

TA貢獻(xiàn)1876條經(jīng)驗(yàn) 獲得超6個(gè)贊
對(duì)我來(lái)說(shuō)工作完美,如下所述:
>>> with open("osx.png","rb") as imageFile:
... img = repr(imageFile.read())
>>> imageFile.close()
>>> with open("sample.py","w") as pyfile:
... pyfile.write(img)
>>> pyfile.close()
然后,我手動(dòng)編輯 pyfile 看起來(lái)像
img = '\x89PNG\r\n\x1a\n[remainder skipped for brevity]'
print(img)
最后但并非最不重要的是,我運(yùn)行了以下代碼:
cmd := exec.Command("python", "sample.py")
stdout, err := cmd.StdoutPipe()
if err != nil {
panic(err)
}
err = cmd.Start()
if err != nil {
panic(err)
}
body, err := ioutil.ReadAll(stdout) //I need the image in []byte
cmd.Wait()
if err != nil {
log.Fatalf("error reading bytes: %s", err)
}
log.Printf("Read %d bytes", len(body))
o, err := os.OpenFile("img.png", os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0660)
if err != nil {
log.Fatalf("opening img.png: %s", err)
}
defer o.Close()
n, err := o.Write(body)
if err != nil {
log.Fatalf("writing image to file: %s", err)
}
if n < len(body) {
log.Fatal("short write")
}
完整代碼可在https://gist.github.com/mwmahlberg/eb47495f68bed4e3cb8073447efdb53d下找到
我想可以肯定的是,您在 Python 文件中存儲(chǔ)圖像數(shù)據(jù)的方式有些奇怪。
- 1 回答
- 0 關(guān)注
- 160 瀏覽
添加回答
舉報(bào)