Created
June 18, 2014 11:28
-
-
Save paulwinex/54ed8efd44a8b82c26c6 to your computer and use it in GitHub Desktop.
Save image to json and load back
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import base64, json | |
from PySide.QtCore import * | |
from PySide.QtGui import * | |
filename = 'C:/image.jpg' | |
# save to json | |
f = open(filename, 'rb') | |
img_data = f.read() | |
f.close() | |
enc_data = base64.b64encode(img_data) | |
json.dump({'image':enc_data}, open('c:/out.json', 'w')) | |
# load from image | |
img = json.load(open('c:/out.json', 'r'))['image'] | |
dec_data = base64.b64decode(img) | |
# show | |
app = QApplication([]) | |
label = QLabel() | |
data = QByteArray(dec_data) | |
pix = QPixmap() | |
pix.loadFromData(data) | |
label.setPixmap(pix) | |
label.show() | |
app.exec_() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment