2014-03-05 84 views
0

我有使用base64編碼的未知數量的圖像。它們在以下格式:使用循環解碼base64圖像

image0 = 「」「\ iVBORw0KGgoAAAANSUhEUgAAABcAAAATCAIAAAD02QHhAAAAB3RJTUUH3gMEFjA2JHVG0AAAAe5JREFUeJztUzGr6jAYTZoWVAQRKTooOIja0SK4da6DkyCIi39AN531DzgKIoroLoiDk3ZWB3UoCiIqFDq5OBRKaXKHXOLjPnn3Xd76zpYvyTnfd3ICCSHgn8G9rf6UmieEQAgJIRjjFzfHUS4I4RdeWqRX2C78syzTYEsqgDH + lYvHGHMc93g8DoeD1 + t1HMc0Tb/fr6oqlWJHKY7HYyKREAThswsIAQCc67qEEF3Xd7tdo9GYTqeWZWUyGYQQFaD6EELbtjVNq1ar2 + 12u90 + Hg82KS8IAiFEURRFUfb7fbvdDgQCAIDZbHY + nwVBqNVqk8lkvV63Wq3RaBSNRk + nk23bsVjs5R3G2HVd13Xv93upVHIcx3Gc + XzebDY1TSsUCoSQdDrd7XYJIeVyebVakd/AQwhp59frNRKJ8DwPAFgsFj6f73a79Xq94XBYr9eXy6Xf7z + fz7IsY4xt2/Z4PMyal + GbzSYej1PucDj8fD4lSer3 + 8FgMJVK6bqeTCaz2Wyn0xmPx9RN5i5qtVo0IJZlybIsiiIAIJfLGYZhmmalUkEIDQaDfD6vqqokSZfLpVgshkIh9nDf54X6ZxiGKIpshC + B/GShJZpdllrXdQEACCHWNj2JMUYI/Sy77DJr/i2 + Z/kbvP/T/1k + AL7rRN + R/gD6AAAAAElFTkSuQmCC「」「

image1的= 「」「\ iVBORw0KGgoAAAANSUhEUgAAABcAAAATCAIAAAD02QHhAAAAB3RJTUUH3gMEFjE42tZalgAAAoRJREFUeJyNUz1PKlEQnbl3YzTYEBshodpGo62hIdgaExsLKiojP4DChD9AI50/wGggsSAhivEnYAKJ0kEAjRUaCODKx25C9t6xGFzfU3nPKe7encycPXPmLBIRAPDpuu7V1dV4PAYApZRlWQcHB36/HwC01oiIiESEiLVajYg2Nj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3fMbPXIhACA6enp7e3tr6 + vaDS6sbEBAIVC4fHxsVKp7O7u2ra9urp6fn7earUQcXl52fO8QCDwhxZmJiKl1MPDg23bSinXdQ8PD1dWVhzHCYfDJycniGjbdrVaXVpaWl9f39raisfjiNhjQUQAsCxLStloNBYWFqSUtVrNdd1yuZzNZu/v72dnZ5PJZDqdLhQKsVgMABzHISJm7rFoOYj4/v7e6XTm5 + eJKBqNjo2NMfP19fXn5 + fExIRlWd1ud39 /// j4OJfLfXx8aON7veh2iKher2vDfN9n5kqlks/NY + UyM2cymWw2WywWmfnq6ur19ZWZlVLMjPp3BqB16VVnfN + 3LKvT6UxOTpq8CX5YTCURSSn10YyJuaAz/fkfd43BiCil1IEu8zzv5uamVCr1F0sp9QTpKgEjoEUBQK1Wcxxnb2/v6elJE/192RrFoh9k5kQikUgknp + fx8fHjeQB/KZFKUVEZ2dnyWQyEonEYjHd0fDbo6Anqtvtuq67 trZWrVbN1w5gZEdEJIR4eXkJBoONRmNqampmZmaoKb/5Ypo6OjoSQhwcHEQikVEdDZ86w9JfM3D8Jy1mxvQziCiEMPF/aDF0Qyv78Q28N9fZLnpYLAAAAABJRU5ErkJggg == 「」」

我可以用成功解碼他們:

imageToShow = base64.b64decode(image0) 
    stream1 = cStringIO.StringIO(imageToShow) 
    bmp1 = wx.ImageFromStream(stream1) 
    self.image_spot0.SetBitmap(wx.BitmapFromImage(bmp1)) 

    imageToShow = base64.b64decode(image1) 
    stream1 = cStringIO.StringIO(imageToShow) 
    bmp1 = wx.ImageFromStream(stream1) 
    self.image_spot1.SetBitmap(wx.BitmapFromImage(bmp1)) 

    imageToShow = base64.b64decode(image2) 
    stream1 = cStringIO.StringIO(imageToShow) 
    bmp1 = wx.ImageFromStream(stream1) 
    self.image_spot2.SetBitmap(wx.BitmapFromImage(bmp1)) 

因爲有可能是3,100,300或哎,爲什麼不5000倍編碼的圖像,我想是能夠使用循環來處理解碼。

當我使用下面的代碼嘗試:

image_group = ['image0', 'image1', 'image2'] 
    for img in image_group: 
     imageToShow = base64.b64decode(img) 

我得到一個錯誤的填充,我認爲該數據不會傳遞給base64.b64decode(),只有實際字符串「image0」。任何幫助,將不勝感激。

+0

你把它像'base64.b64decode( 「image0」)的循環' ,而不是變量值。因此,你的問題可以簡化爲「我如何通過名稱評估/獲取局部變量的價值」。但我建議在數組中聲明這些base64字符串以便於處理。 – kirilloid

+0

@kirilloid。感謝您的答覆。你的建議是我已經工作的代碼,沒有報價。如果我可以提前確定需要處理多少個字符串或至少是字符串的上限,那麼工作正常。我試圖在每次代碼運行時能夠處理不同數量的字符串。你能否詳細說明如何聲明數組中的字符串可以提供解決方案? – Karr

+1

其實你可以寫'image_group = [image0,image1,image2]',那應該可以。 – kirilloid

回答

0

您將變量名字符串文字與相同的值混合在一起。

解決方法就是在寫作:

image_group = ['image0', 'image1', 'image2'] 

你甚至可以像組合聲明酌減:

image_group = [ 
    "iVBORw0KGgoAAAANSUhEUgAAA...", 
    "iVBORw0KGgoAAAANSUhEUgAAA...", 
    "iVBORw0KGgoAAAANSUhEUgAAA..." 
]