Questions tagged [bytesio]

A stream implementation using an in-memory bytes buffer.

Refer to BytesIO class details in Python's docs.

258 questions
4
votes
3 answers

equivalent of getbuffer for BytesIO in Python 2

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…
Brian Lee
  • 305
  • 4
  • 14
4
votes
1 answer

PIL with BytesIO: cannot identify image file

I am trying to send an image over socket connection for video chat, but the reconstruction of the image from bytes format is incorrect. Here is my conversion of the image to bytes to send: pil_im = Image.fromarray(img) b =…
ajflj
  • 115
  • 1
  • 2
  • 7
4
votes
2 answers

Convert PDF page to image with PyPDF2 and BytesIO

I have a function that gets a page from a PDF file via PyPDF2 and should convert the first page to a png (or jpg) with Pillow (PIL Fork) from PyPDF2 import PdfFileWriter, PdfFileReader import os from PIL import Image import io # Open PDF Source…
PrimuS
  • 2,505
  • 6
  • 33
  • 66
4
votes
1 answer

Why does truncating a BytesIO mess it up?

Running this on Python 3.5.1 on OSX: import io b = io.BytesIO() b.write(b'222') print(b.getvalue()) b.truncate(0) b.write(b'222') print(b.getvalue()) Produces: b'222' b'\x00\x00\x00222' So truncating the BytesIO somehow causes it to start…
Petri
  • 4,796
  • 2
  • 22
  • 31
4
votes
1 answer

Pillow saving to BytesIO or StringIO KeyError: "JPG"

I have a Pillow Image object image, that I try to save to an IO.BytesIO object called temp using image.save(temp, format="jpg") When I run this however, it gives a KeyError: "JPG" on this line save_handler = SAVE[format.upper()] Thanks!
PaulOlteanu
  • 43
  • 1
  • 4
4
votes
1 answer

Wrap generator into a buffer?

I have a python generator that yields parts of a file (a wsgi app_iter) and I need to pass it to an interface that expects it to have the classical read and readlines methods (I want to pass it as wsgi.input of another Request). Is is possible to do…
enrico.bacis
  • 30,497
  • 10
  • 86
  • 115
4
votes
1 answer

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

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…
timakro
  • 1,739
  • 1
  • 15
  • 31
3
votes
1 answer

How to get video metadata from bytes using imageio.v3?

I am creating a python class to process videos received from a http post. The videos can be from a wide range of sizes, 10 seconds up to 10 hours. I am looking for a way to get video metadata such as fps, height, width etc. without having to store…
brenodacosta
  • 389
  • 2
  • 13
3
votes
1 answer

Open a file (of type BytesIO) with the function

I have the following pieces of code: this part generates the CSV in memory: def to_csv(events: list) -> io.BytesIO(): if not events: return None bio = io.BytesIO() iow = io.TextIOWrapper(bio) writer = csv.DictWriter(iow,…
Romsik788
  • 45
  • 4
3
votes
1 answer

Render NumPy array in FastAPI

I have found How to return a numpy array as an image using FastAPI?, however, I am still struggling to show the image, which appears just as a white square. I read an array into io.BytesIO like so: def iterarray(array): output = io.BytesIO() …
3
votes
1 answer

Encode .parquet into io.Bytes

Goal: Upload a Parquet file to MinIO - this requires converting the file to Bytes. I've been able to do this for .csv, .json and .txt: bytes = data.to_csv().encode('utf-8') bytes = json.dumps(self.data, indent=4, separators=(',', ':…
DanielBell99
  • 896
  • 5
  • 25
  • 57
3
votes
1 answer

How to dump in memorry files to an in memmory tar and than dump that tar to disk in python

I have a lot of images in my python memory that I would like to place in an in memory tar, after which I would like to flush that tar to the disk. I would like to do this in memory to prevent the writing of a lot of tiny files to my disk. What I…
Daan
  • 349
  • 4
  • 16
3
votes
0 answers

Use BytesIO object as a file path

The library I am using doesn't support a BytesIO object or binary data. Instead, it expects a file path in disk to be opened as binary. I want to pass my raw data or even a BytesIO object as a file path argument, without saving it to disk (something…
Julio S.
  • 944
  • 1
  • 12
  • 26
3
votes
1 answer

Uploading image string to Google Drive using pydrive

I need to upload an image string (as the one you get from requests.get(url).content) to google drive using the PyDrive package. I checked a similar question but the answer accepted there was to save it in a temporary file on a local drive and then…
pu239
  • 707
  • 7
  • 17
3
votes
3 answers

How to send BytesIO using requests post

I am trying to send msgpack encoded values to server using requests post to an insert rest endpoint which takes the serialized byte stream as input. But it seems like the values are not getting there properly as I see no values getting inserted in…
ShellZero
  • 4,415
  • 12
  • 38
  • 56
1 2
3
17 18