I have a file .obj
in which I previously converted the image to base64 and saved it with pickle
.
The problem is that I am trying to download a file .obj
using pickle
, convert the code to an image from base64 and load it using pygame
.
Image loading function:
def mainDisplay_load(self):
main_folder = path.dirname(__file__)
img_main_folder = path.join(main_folder, "sd_graphics")
self.mainTerminal = pg.image.load(path.join(img_main_folder, self.main_uncode("tr.obj"))).convert_alpha()
Function that decodes a file:
def main_uncode(self, object):
openFile = open(object, "rb")
str = pickle.load(openFile)
openFile.close()
fileData = base64.b64decode(str)
return fileData
The error I get when running the code:
str = pickle.load(openFile)
EOFError: derived from input
How can i fix this?
- Python Version: 3.6.2
- Pygame Version: 1.9.3
Update 1
This is the code I used to create the file .obj
:
import base64, pickle
with open("terminal.png", "rb") as imageFile:
str = base64.b64encode(imageFile.read())
print(str)
file_pi = open("tr.obj","wb")
pickle.dump(str,file_pi)
file_pi.close()
file_pi2 = open("tr.obj","rb")
str2 = pickle.load(file_pi2)
file_pi2.close()
imgdata = base64.b64decode(str2)
filename = 'some_image.jpg'
with open(filename, 'wb') as f:
f.write(imgdata)
After creating the file, it is downloaded and a second image is created. It is necessary to check whether the image is the same or if there are errors in the conversion.
, , pygame
. , .
2
- .
:
def mainDisplay_load(self):
self.all_graphics = pg.sprite.Group()
self.graphics_menu = pg.sprite.Group()
self.img_mainTerminal = mainGraphics(self, 0, 0, "sd_graphics/tr.obj")
, :
import pygame as pg
import base64 as bs
import pickle as pk
from io import BytesIO as by
from lib.setting import *
class mainGraphics(pg.sprite.Sprite):
def __init__(self, game, x, y, object):
self.groups = game.all_graphics, game.graphics_menu
pg.sprite.Sprite.__init__(self, self.groups)
self.game = game
self.object = object
self.outputGraphics = by()
self.x = x
self.y = y
self.eventType()
self.rect = self.image.get_rect()
self.rect.x = self.x * tilesizeDefault
self.rect.y = self.y * tilesizeDefault
def eventType(self):
openFile = open(self.object, "rb")
str = pk.load(openFile)
openFile.close()
self.outputGraphics.write(bs.b64decode(str))
self.outputGraphics.seek(0)
self.image = pg.image.load(self.outputGraphics).convert_alpha()
, , :
Python .
, , . Python , , .
, , , . , , .
, , , , . , . , .