无法在循环中打开PNG设备 [英] Unable to open png device in loop

查看:209
本文介绍了无法在循环中打开PNG设备的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在研究R中的一个函数,长话短说,我有一个for循环,在每一步中,我都使用png保存了一个图,然后立即使用readPNG我可以提取RGB信息.然后,我再绘制第二张图,然后是readPNG,这样我就可以比较两个图像的RGB. 问题是,经过多次循环(可能只有少数几个循环,或者多达一个循环),我一直收到有关无法启动png()设备或无法打开文件进行写入的错误消息.几千).

I've been fiddling around with a function in R, where, long story short, I have a for-loop, and at each step, I save a plot using png, then immediately readPNG so that I can extract RGB information. I then make a second plot, then readPNG this so I can compare the RGB of the two images. The problem is that I keep getting an error message about being unable to start the png() device, or to open the file for writing, after a number of loops (can be as few as a handful of loops, or as many as a few thousand).

这里确实是简化的代码,但是它具有基本的要点,并生成错误消息:

Here is really simplified code, but it has the bare essentials, and generates the error message:

testfun<-function(beg,fini)
{
 library(png)
 setwd("D://mydirectory")
 for (i in beg:fini)
 {
  png("test.png",width=277,height=277) #candidate image
  par(mai=c(0,0,0,0))
  plot(1,type="n",ann=FALSE,xlim=c(0,255),ylim=c(0,255),
         xaxt="n",yaxt="n",frame.plot=F)
  polygon(x=c(10,60,60),y=c(10,10,60),col="red")
  graphics.off()

  image<-readPNG("test.png")
  #code where I get rgb values for original

  png("test2.png",width=277,height=277) #candidate image with diferent params
  par(mai=c(0,0,0,0))
  plot(1,type="n",ann=FALSE,xlim=c(0,255),ylim=c(0,255),
         xaxt="n",yaxt="n",frame.plot=F)
  polygon(x=c(10,60,60),y=c(10,10,60),col="blue")
  graphics.off()

  image<-readPNG("test2.png")
  #code where I get rgb values for second image, and compare
 }
}

错误消息:

Error in png("test.png", width = 277, height = 277) : 
  unable to start png() device
In addition: Warning messages:
1: In png("test.png", width = 277, height = 277) :
  Unable to open file 'test.png' for writing
2: In png("test.png", width = 277, height = 277) : opening device failed

最初,我将graphics.off()设置为dev.off(),但随后可能认为循环是如此之快,以至于关闭一台设备的速度不够快,需要再次打开,并且它以某种方式变得困惑".我还尝试在每个graphics.off之后使用Sys.sleep(0.1),但这也无济于事.我是否缺少一些愚蠢而明显的东西,或者这仅仅是设备错误?

Originally I had graphics.off() as dev.off() but then thought maybe the loop was so fast that turning off one device wasn't fast enough before needing to be open again and it was getting 'confused' somehow. I also tried using Sys.sleep(0.1) after each graphics.off, but that didn't help either. Am I missing something stupid and obvious, or is this just a device bug?

推荐答案

我曾经遇到过同样的问题,尽管不是在循环情况下.就我而言,这是因为我将.png输出指向了不存在的目录.

I've had the same problem occur, although not in a loop situation. In my case, it was because I was pointing the .png output to a directory that did not exist.

png('./tweets/graphics/unique words.png', width=12, height=8, units='in', res=300)

创建目录并正确引用后,错误消失了,我得到了.png图像.

Once I created the directory, and referenced it correctly, the error went away and I got my .png image.

这篇关于无法在循环中打开PNG设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆