Object does not appear to be a 8-bit string path or a Python file-like object

matplotlib imread
python read pgm image
matplotlib read pdf
image to matrix python
python plot png
image to array python
python imread example
matplotlib imread grayscale

I'm playing with Flask and Python 3.5. I want to show a matplotplib chart on a website. I found this example https://gist.github.com/wilsaj/862153

I made minor modifications to the StringIO part since I'm using 3.5 version of Python.

@bp.route("/simple.png")
def simple():
    import datetime
    import io
    import numpy as np
    import random

    from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
    from matplotlib.figure import Figure
    from matplotlib.dates import DateFormatter

    fig=Figure()
    ax=fig.add_subplot(111)
    x=[]
    y=[]
    now=datetime.datetime.now()
    delta=datetime.timedelta(days=1)
    for i in range(10):
        x.append(now)
        now+=delta
        y.append(random.randint(0, 1000))
    ax.plot_date(x, y, '-')
    ax.xaxis.set_major_formatter(DateFormatter('%Y-%m-%d'))
    fig.autofmt_xdate()
    canvas=FigureCanvas(fig)
    png_output = io.BytesIO()
    png_output_text = np.genfromtxt(png_output, delimiter=",")
    canvas.print_png(png_output_text)
    response=make_response(png_output_text.getvalue())
    response.headers['Content-Type'] = 'image/png'
    return response

Error I'm receiving:

Traceback (most recent call last):
  File "c:\proofdevelopment\flask\flask\app.py", line 1988, in wsgi_app
    response = self.full_dispatch_request()
  File "c:\proofdevelopment\flask\flask\app.py", line 1641, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "c:\proofdevelopment\flask\flask\app.py", line 1544, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "c:\proofdevelopment\flask\flask\_compat.py", line 33, in reraise
    raise value
  File "c:\proofdevelopment\flask\flask\app.py", line 1639, in full_dispatch_request
    rv = self.dispatch_request()
  File "c:\proofdevelopment\flask\flask\app.py", line 1625, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "C:\ProofDevelopment\flask\examples\flaskr\flaskr\blueprints\flaskr.py", line 107, in simple
    canvas.print_png(png_output_text)
  File "C:\Users\nmartinez\AppData\Local\Continuum\miniconda3\envs\py35\lib\site-packages\matplotlib\backends\backend_agg.py", line 526, in print_png
    self.figure.dpi, metadata=metadata)
TypeError: Object does not appear to be a 8-bit string path or a Python file-like object

I just want to run that 'simple' example and I haven't been able to :(


I'm not sure why you added the np.genfromtxt() call. The following lines to create the in-memory file object are enough; these are the last lines in the view function, starting at your png_output line:

png_output = io.BytesIO()
canvas.print_png(png_output)
response = make_response(png_output.getvalue())
response.headers['Content-Type'] = 'image/png'
return response

With those changes, the code produces a plot:

savefig() does not support PosixPath object for file name · Issue , The savefig() method does not allow a PosixPath object to be passed for Object does not appear to be a 8-bit string path or a Python file-like  Object does not appear to be a 8-bit string path or a Python file-like object. 570. TypeError: Object does not appear to be a 8-bit string path or a Python file


x_train=np.array(list(map(img_preprocess,x_train)))
x_valid=np.array(list(map(img_preprocess,x_valid)))

bit shows object-does-not-appear-to-be-a-8-bit-string-path-or-a-python-file-like-object

This Error is because Here you already changed shape of x_train by executing x_train=np.array(list(map(img_preprocess,x_train))) this line, so x_train is updated now and again if you will give x_train as input to the x_train=np.array(list(map(img_preprocess,x_train))) function then it will give this error

solution: reset your python kernel and run all code in sequence again.

matplotlib.pyplot.imread, The image file to read. This can be a filename, a URL or a Python file-like object opened in read-binary mode. format : str  " Object does not appear to be a 8-bit string path or a Python file-like object "); " Object does not appear to be a 8-bit string path or "


x_train=np.array(list(map(img_preprocess,x_train)))
x_valid=np.array(list(map(img_preprocess,x_valid)))
bit shows object-does-not-appear-to-be-a-8-bit-string-path-or-a-python-file-like-object

Whats's the problem behind it?

matplotlib.pyplot.imread Python Example, You can vote up the examples you like or vote down the ones you don't like. Example 1. Project: mdp Author: BlackHC File: __  Revision: 5025 http://matplotlib.svn.sourceforge.net/matplotlib/?rev=5025&view=rev Author: mdboom Date: 2008-03-27 07:26:19 -0700 (Thu, 27 Mar 2008) Log Message


matplotlib / List matplotlib-checkins Archives, throw Py::TypeError("Object does not appear to be a 8-bit string path or a Python file-like object"); + } + + PyObject_CallFunction(write_method, "s#", pixBuffer,  Revision: 5475 http://matplotlib.svn.sourceforge.net/matplotlib/?rev=5475&view=rev Author: mdboom Date: 2008-06-11 11:05:21 -0700 (Wed, 11 Jun 2008) Log Message


7.5. StringIO — Read and write strings as files, This module implements a file-like class, StringIO , that reads and writes a string buffer (also The StringIO object can accept either Unicode or 8-bit strings, but mixing the two may take some care. Unlike an object created without a string parameter, it does not have write methods. These objects are not generally visible. Just trying to do something simple like: %matplotlib notebook import matplotlib.pyplot as plt plt.plot(range(10)) plt.tight_layout() results in Traceback (most recent call last): File "/Users/


Issue while using albumentation for image augmentation, in read_png return _png.read_png(*args, **kwargs) TypeError: Object does not appear to be a 8-bit string path or a Python file-like object  Bug report Bug summary The savefig() method does not allow a PosixPath object to be passed for the file name. Code for reproduction The following code produces the bug: import matplotlib.pyplot as plt import numpy as np from pathlib impo