bytesio

In an HTML table, how to add text beside plot in jupyter notebook using python?

有些话、适合烂在心里 提交于 2019-12-20 07:24:57
问题 Any ideas on how to create a 1 X 2 HTML table where cell {0} is a matplotlib plot and cell {1} is a text description for Python 3.X? import matplotlib.pyplot as plt from io import BytesIO %matplotlib inline def add_split_screen(fig, text, iwidth=None): figdata = BytesIO() fig.savefig(figdata, format='png') figdata.seek(0) figdata figdata.close() iwidth = ' width={0} '.format(iwidth) if iwidth is not None else '' datatable = '<table><tr><td>{0}</td><td>{1}</td></tr></table>'.format(figdata,

PIL cannot identify image file for io.BytesIO object

…衆ロ難τιáo~ 提交于 2019-12-17 07:33:07
问题 I am using the Pillow fork of PIL and keep receiving the error OSError: cannot identify image file <_io.BytesIO object at 0x103a47468> when trying to open an image. I am using virtualenv with python 3.4 and no installation of PIL. I have tried to find a solution to this based on others encountering the same problem, however, those solutions did not work for me. Here is my code: from PIL import Image import io # This portion is part of my test code byteImg = Image.open("some/location/to/a/file

How can I send a StringIO via FTP in python 3?

老子叫甜甜 提交于 2019-12-12 10:58:51
问题 I want to upload a text string as a file via FTP. import ftplib from io import StringIO file = StringIO() file.write("aaa") file.seek(0) with ftplib.FTP() as ftp: ftp.connect("192.168.1.104", 2121) ftp.login("ftp", "ftp123") ftp.storbinary("STOR 123.txt", file) This code returns an error: TypeError: 'str' does not support the buffer interface 回答1: This can be a point of confusion in python 3, especially since tools like csv will only write str , while ftplib will only accept bytes . You can

Pass io.BytesIO object to gzip.GzipFile and write to GzipFile

不想你离开。 提交于 2019-12-11 10:45:32
问题 I basically want to do exactly whats in the documentation of gzip.GzipFile : Calling a GzipFile object’s close() method does not close fileobj, since you might wish to append more material after the compressed data. This also allows you to pass a io.BytesIO object opened for writing as fileobj, and retrieve the resulting memory buffer using the io.BytesIO object’s getvalue() method. With a normal file object it works as expected. >>> import gzip >>> fileobj = open("test", "wb") >>> fileobj

How to use BytesIO with matplotlib and pyqt5?

南楼画角 提交于 2019-12-11 02:33:09
问题 I made a graph in matplotlib, and wanted to make it in to an image and use it in my pyqt5 application. Someone suggested I use BytesIO for this. This is my code so far: Drawing my graph: ... plt.axis('equal') buff = io.BytesIO() plt.savefig(buff, format="png") print(buff) return buff This is then called in another script: def minionRatioGraphSetup(self, recentMinionRatioAvg): image = minionRatioGraph(recentMinionRatioAvg) label = QtWidgets.QLabel() pixmap = QtGui.QPixmap(image) label

equivalent of getbuffer for BytesIO in Python 2

孤街醉人 提交于 2019-12-11 01:48:02
问题 In Python 3, I can get the size of a ByteIO object via object.getbuffer().nbytes (where object = ByteIO() ), but what would be the best equivalent for getbuffer() in Python 2? Doing some exploring, I found out I can use len(object.getvalue()) or sys.getsizeof(object) , but I don't know if Python 2 will accept them. 回答1: After digging in python 2.7 source code I found a simple solution: because io.BytesIO() returns a file descriptor, it has a standard set of functions including tell() . Note

ftp sending python bytesio stream

浪尽此生 提交于 2019-12-10 12:56:43
问题 I want to send a file with python ftplib, from one ftp site to another, so to avoid file read/write processees. I create a BytesIO stream: myfile=BytesIO() And i succesfully retrieve a image file from ftp site one with retrbinary: ftp_one.retrbinary('RETR P1090080.JPG', myfile.write) I can save this memory object to a regular file: fot=open('casab.jpg', 'wb') fot=myfile.readvalue() But i am not able to send this stream via ftp with storbinary. I thought this would work: ftp_two.storbinary(

How to create an BytesIO img and pass to template

混江龙づ霸主 提交于 2019-12-08 10:26:45
问题 AIM I am attempting to: Create a histogram, Store it temporary memory, Pass the image to the template. I am having trouble with Step 3 above. I suspect that I am making a simple and fundamental error in relation to passing the context data to the template. ERROR HTML is rendering with a broken image tag. CODE Views.py class SearchResultsView(DetailView): ... def get(self, request, *args, **kwargs): self.get_histogram(request) return super(SearchResultsView, self).get(request, *args, **kwargs)

Reading in pydub AudioSegment from url. BytesIO returning “OSError [Errno 2] No such file or directory” on heroku only; fine on localhost

拈花ヽ惹草 提交于 2019-12-08 06:45:43
问题 EDIT 1 for anyone with the same error: installing ffmpeg did indeed solve that BytesIO error EDIT 1 for anyone still willing to help: my problem is now that when I AudioSegment.export("filename.mp3", format="mp3"), the file is made, but has size 0 bytes -- details below (as "EDIT 1") EDIT 2: All problems now solved. Files can be read in as AudioSegment using BytesIO I found buildpacks to ensure ffmpeg was installed correctly on my app, with lame support for exporting proper mp3 files Answer

Strange “BadZipfile: Bad CRC-32” problem

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-07 06:14:06
问题 This code is simplification of code in a Django app that receives an uploaded zip file via HTTP multi-part POST and does read-only processing of the data inside: #!/usr/bin/env python import csv, sys, StringIO, traceback, zipfile try: import io except ImportError: sys.stderr.write('Could not import the `io` module.\n') def get_zip_file(filename, method): if method == 'direct': return zipfile.ZipFile(filename) elif method == 'StringIO': data = file(filename).read() return zipfile.ZipFile