Python,在while循环和Image.open中尝试

时间:2014-02-17 08:13:48

标签: python python-imaging-library

我试图让我的循环继续,如果它遇到错误的图像文件失败。我正在使用PIL并检查文件是否是有效的jpg。当jpg的字节大小为零时,我注意到它正在崩溃。

def extract_images_2():

vc = cv2.VideoCapture(videofilename)
saved_file_name=""
frame_rate=str (vc.get(cv2.cv.CV_CAP_PROP_FPS))

c=1

if vc.isOpened():
        rval , frame = vc.read()
else:
        rval = False

while rval:
        rval, frame = vc.read()
    saved_file_name=str(c) + '.jpg'
        cv2.imwrite(saved_file_name,frame)

    #Extract the image size:
    myimage= Image.open(saved_file_name)
    image_width, image_height = myimage.size
    img_new_magnitude=3

    aalib_screen_width= int(image_width/25.28)*img_new_magnitude
    aalib_screen_height= int(image_height/41.39)*img_new_magnitude

    screen = aalib.AsciiScreen(width=aalib_screen_width, height=aalib_screen_height )

        try:
                myimage= Image.open(saved_file_name).convert("L").resize(screen.virtual_size)
        except IOError:
                print ("ERROR processing ", myimage)
        continue

    screen.put_image((0,0), myimage)    

    print >> open('file.txt', 'w'), screen.render(), 2+3

    subprocess.call(["convert", "-background", "rgb(120,20,20)",  "-fill",  "rgb(255,255,0)",  "-font",  "/usr/share/fonts/truetype/freefont/FreeMonoBold.ttf",  "-pointsize", "15" , "label:@file.txt" , str(c)+'.png'])

    subprocess.call(["rm", saved_file_name ])
        c = c + 1
        cv2.waitKey(1)

vc.release()

subprocess.call(["ffmpeg", "-r",  frame_rate, "-b", "320k", "-i", "%d.gif","-vcodec", "libx264", "output.mp4" ])

[已解决的解决方案]

http://pastebin.com/WackucCg

我不得不在那里过去,因为当我将它粘贴在这里时格式化了。

0 个答案:

没有答案