Can't read binary matrix from a file in Python 3.xReading an entire binary file into PythonDownload file from web in Python 3Using Python to read and edit bitmapsfull drawing not displaying on screen pygameHow to read Complex Binary file (.fc32) in python?Error in my code in python 3 (coin change problem)How to build Numpy array from a String written in file in Pythonpandas reading data from column in as float or int and not str despite dtype settingHow to have specific conditions per element for a matrix or 2d array in python?Create binary tiff files (compressed with lzw) from numpy array

How to speed up large double sums in a table?

Debian 9 server no sshd in auth.log

What happens if I accidentally leave an app running and click "Install Now" in Software Updater?

What is monoid homomorphism exactly?

Installing Debian 10, upgrade to stable later?

Endgame puzzle: How to avoid stalemate and win?

How to detect nM levels of Copper(I) Oxide in blood?

How important are good looking people in a novel/story?

Is there precedent or are there procedures for a US president refusing to concede to an electoral defeat?

In "Avengers: Endgame", what does this name refer to?

Determine if a grid contains another grid

What are these two Sewer Pipes Coming up Out the Ground?

Why are condenser mics so much more expensive than dynamics?

Does Thanos's ship land in the middle of the battlefield in "Avengers: Endgame"?

What is a common way to tell if an academic is "above average," or outstanding in their field? Is their h-index (Hirsh index) one of them?

Problem with estimating a sequence with intuition

Sci-fi/fantasy book - ships on steel runners skating across ice sheets

Why didn't this character get a funeral at the end of Avengers: Endgame?

GitLab account hacked and repo wiped

How to preserve a rare version of a book?

Can I combine SELECT TOP() with the IN operator?

Dimmer switch not connected to ground

Is there a reason why Turkey took the Balkan territories of the Ottoman Empire, instead of Greece or another of the Balkan states?

How is Pauli's exclusion principle still valid in these cases?



Can't read binary matrix from a file in Python 3.x


Reading an entire binary file into PythonDownload file from web in Python 3Using Python to read and edit bitmapsfull drawing not displaying on screen pygameHow to read Complex Binary file (.fc32) in python?Error in my code in python 3 (coin change problem)How to build Numpy array from a String written in file in Pythonpandas reading data from column in as float or int and not str despite dtype settingHow to have specific conditions per element for a matrix or 2d array in python?Create binary tiff files (compressed with lzw) from numpy array






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















So, I have to do these two functions, one that saves a binary matrix on a .bin file and one that reads that same file and returns the numpy.array.



My problem is that when I try to .vstack both line and final image (I basically want to save a Black and White image) I get this message error:




'ValueError: all the input array dimensions except for the concatenation axis must match exactly'




which makes sense because after the I read the second line binaryLine and final image have different length, for some reason I can't understand.



def save_binary_matrix(img, fileName):
file = open(fileName, "w+")
heigth, width = img.shape
image = convert_BW_to_0_1(img) # converts from 0 and 255 to 0 and 1
for y in range(heigth):
for x in range(0, width, 8):
bits = image[y][x:x+8]# gets every 8 bits
s = ''
# converts bits to a string
for i in range(len(bits)):
s = s + str(bits[i])
file.write(str(int(s,2)))# saves the string as a integer
file.write("n")# line change
file.close()

def read_binary_matrix(fileName):
file = open(fileName, "r")
#saves first line of the file
finalImage = np.array([])
line = file.readline()
for l in range(len(line)):
if line[l] != 'n':
finalImage = np.append(finalImage, np.array([int(x) for x in list('0:08b'.format(int(line[l])))]))
#reads and saves other lines
for line in file:
binaryLine = np.array([])
for l in range(len(line)):
if line[l] != 'n':
#read and saves line as binary value
binaryLine = np.append(binaryLine, np.array([int(x) for x in list('0:08b'.format(int(line[l])))]))
finalImage = np.vstack((finalImage, binaryLine))
return finalImage









share|improve this question
























  • Why not just use numpy.save() and numpy.load()? And no, your files are not binary, they are text files with binary representations of integer numbers.

    – DYZ
    Mar 23 at 4:55

















0















So, I have to do these two functions, one that saves a binary matrix on a .bin file and one that reads that same file and returns the numpy.array.



My problem is that when I try to .vstack both line and final image (I basically want to save a Black and White image) I get this message error:




'ValueError: all the input array dimensions except for the concatenation axis must match exactly'




which makes sense because after the I read the second line binaryLine and final image have different length, for some reason I can't understand.



def save_binary_matrix(img, fileName):
file = open(fileName, "w+")
heigth, width = img.shape
image = convert_BW_to_0_1(img) # converts from 0 and 255 to 0 and 1
for y in range(heigth):
for x in range(0, width, 8):
bits = image[y][x:x+8]# gets every 8 bits
s = ''
# converts bits to a string
for i in range(len(bits)):
s = s + str(bits[i])
file.write(str(int(s,2)))# saves the string as a integer
file.write("n")# line change
file.close()

def read_binary_matrix(fileName):
file = open(fileName, "r")
#saves first line of the file
finalImage = np.array([])
line = file.readline()
for l in range(len(line)):
if line[l] != 'n':
finalImage = np.append(finalImage, np.array([int(x) for x in list('0:08b'.format(int(line[l])))]))
#reads and saves other lines
for line in file:
binaryLine = np.array([])
for l in range(len(line)):
if line[l] != 'n':
#read and saves line as binary value
binaryLine = np.append(binaryLine, np.array([int(x) for x in list('0:08b'.format(int(line[l])))]))
finalImage = np.vstack((finalImage, binaryLine))
return finalImage









share|improve this question
























  • Why not just use numpy.save() and numpy.load()? And no, your files are not binary, they are text files with binary representations of integer numbers.

    – DYZ
    Mar 23 at 4:55













0












0








0








So, I have to do these two functions, one that saves a binary matrix on a .bin file and one that reads that same file and returns the numpy.array.



My problem is that when I try to .vstack both line and final image (I basically want to save a Black and White image) I get this message error:




'ValueError: all the input array dimensions except for the concatenation axis must match exactly'




which makes sense because after the I read the second line binaryLine and final image have different length, for some reason I can't understand.



def save_binary_matrix(img, fileName):
file = open(fileName, "w+")
heigth, width = img.shape
image = convert_BW_to_0_1(img) # converts from 0 and 255 to 0 and 1
for y in range(heigth):
for x in range(0, width, 8):
bits = image[y][x:x+8]# gets every 8 bits
s = ''
# converts bits to a string
for i in range(len(bits)):
s = s + str(bits[i])
file.write(str(int(s,2)))# saves the string as a integer
file.write("n")# line change
file.close()

def read_binary_matrix(fileName):
file = open(fileName, "r")
#saves first line of the file
finalImage = np.array([])
line = file.readline()
for l in range(len(line)):
if line[l] != 'n':
finalImage = np.append(finalImage, np.array([int(x) for x in list('0:08b'.format(int(line[l])))]))
#reads and saves other lines
for line in file:
binaryLine = np.array([])
for l in range(len(line)):
if line[l] != 'n':
#read and saves line as binary value
binaryLine = np.append(binaryLine, np.array([int(x) for x in list('0:08b'.format(int(line[l])))]))
finalImage = np.vstack((finalImage, binaryLine))
return finalImage









share|improve this question
















So, I have to do these two functions, one that saves a binary matrix on a .bin file and one that reads that same file and returns the numpy.array.



My problem is that when I try to .vstack both line and final image (I basically want to save a Black and White image) I get this message error:




'ValueError: all the input array dimensions except for the concatenation axis must match exactly'




which makes sense because after the I read the second line binaryLine and final image have different length, for some reason I can't understand.



def save_binary_matrix(img, fileName):
file = open(fileName, "w+")
heigth, width = img.shape
image = convert_BW_to_0_1(img) # converts from 0 and 255 to 0 and 1
for y in range(heigth):
for x in range(0, width, 8):
bits = image[y][x:x+8]# gets every 8 bits
s = ''
# converts bits to a string
for i in range(len(bits)):
s = s + str(bits[i])
file.write(str(int(s,2)))# saves the string as a integer
file.write("n")# line change
file.close()

def read_binary_matrix(fileName):
file = open(fileName, "r")
#saves first line of the file
finalImage = np.array([])
line = file.readline()
for l in range(len(line)):
if line[l] != 'n':
finalImage = np.append(finalImage, np.array([int(x) for x in list('0:08b'.format(int(line[l])))]))
#reads and saves other lines
for line in file:
binaryLine = np.array([])
for l in range(len(line)):
if line[l] != 'n':
#read and saves line as binary value
binaryLine = np.append(binaryLine, np.array([int(x) for x in list('0:08b'.format(int(line[l])))]))
finalImage = np.vstack((finalImage, binaryLine))
return finalImage






python-3.x numpy numpy-ndarray






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 23 at 16:16









double-beep

3,12051632




3,12051632










asked Mar 23 at 4:25









António Dos ReisAntónio Dos Reis

52




52












  • Why not just use numpy.save() and numpy.load()? And no, your files are not binary, they are text files with binary representations of integer numbers.

    – DYZ
    Mar 23 at 4:55

















  • Why not just use numpy.save() and numpy.load()? And no, your files are not binary, they are text files with binary representations of integer numbers.

    – DYZ
    Mar 23 at 4:55
















Why not just use numpy.save() and numpy.load()? And no, your files are not binary, they are text files with binary representations of integer numbers.

– DYZ
Mar 23 at 4:55





Why not just use numpy.save() and numpy.load()? And no, your files are not binary, they are text files with binary representations of integer numbers.

– DYZ
Mar 23 at 4:55












1 Answer
1






active

oldest

votes


















0














Twice you create a np.array([]). Pay attention to its shape:



In [140]: x = np.array([]) 
In [141]: x.shape
Out[141]: (0,)


It does work in np.append - that's because without axis parameter, append is just concatenate((x, y), axis=0), e.g. adding a (0,) shape and a (3,) shape to create a (3,) shape:



In [142]: np.append(x, np.arange(3)) 
Out[142]: array([0., 1., 2.])


But vstack doesn't work. It makes its inputs into 2d arrays, and joins them on the first axis:



In [143]: np.vstack((x, np.arange(3))) 
ValueError: all the input array dimensions except for the concatenation axis must match exactly


So we are joining (0,) and (3,) on a new first axis, e.g. (1,0) and (1,3) on first axis. The 0 and 3 don't match, hence the error.



vstack works when joining a (3,) with a (3,) and a (1,3), and a (4,3). Note the common 'last' dimension.



The underlying problem is that you are trying to emulate the list append, without fully understanding dimensions, or what concatenate does. It makes a whole new array each time. np.append is not a clone of list.append!.



What you should be doing is starting with a [] list (or two), append new values to that, making a list of lists. Then np.array(alist) to turn that into an array (provided, of course that all sublists match in size).



I haven't paid attention to your write, or how you read the lines, so can't say whether that makes sense or not.






share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55310603%2fcant-read-binary-matrix-from-a-file-in-python-3-x%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Twice you create a np.array([]). Pay attention to its shape:



    In [140]: x = np.array([]) 
    In [141]: x.shape
    Out[141]: (0,)


    It does work in np.append - that's because without axis parameter, append is just concatenate((x, y), axis=0), e.g. adding a (0,) shape and a (3,) shape to create a (3,) shape:



    In [142]: np.append(x, np.arange(3)) 
    Out[142]: array([0., 1., 2.])


    But vstack doesn't work. It makes its inputs into 2d arrays, and joins them on the first axis:



    In [143]: np.vstack((x, np.arange(3))) 
    ValueError: all the input array dimensions except for the concatenation axis must match exactly


    So we are joining (0,) and (3,) on a new first axis, e.g. (1,0) and (1,3) on first axis. The 0 and 3 don't match, hence the error.



    vstack works when joining a (3,) with a (3,) and a (1,3), and a (4,3). Note the common 'last' dimension.



    The underlying problem is that you are trying to emulate the list append, without fully understanding dimensions, or what concatenate does. It makes a whole new array each time. np.append is not a clone of list.append!.



    What you should be doing is starting with a [] list (or two), append new values to that, making a list of lists. Then np.array(alist) to turn that into an array (provided, of course that all sublists match in size).



    I haven't paid attention to your write, or how you read the lines, so can't say whether that makes sense or not.






    share|improve this answer





























      0














      Twice you create a np.array([]). Pay attention to its shape:



      In [140]: x = np.array([]) 
      In [141]: x.shape
      Out[141]: (0,)


      It does work in np.append - that's because without axis parameter, append is just concatenate((x, y), axis=0), e.g. adding a (0,) shape and a (3,) shape to create a (3,) shape:



      In [142]: np.append(x, np.arange(3)) 
      Out[142]: array([0., 1., 2.])


      But vstack doesn't work. It makes its inputs into 2d arrays, and joins them on the first axis:



      In [143]: np.vstack((x, np.arange(3))) 
      ValueError: all the input array dimensions except for the concatenation axis must match exactly


      So we are joining (0,) and (3,) on a new first axis, e.g. (1,0) and (1,3) on first axis. The 0 and 3 don't match, hence the error.



      vstack works when joining a (3,) with a (3,) and a (1,3), and a (4,3). Note the common 'last' dimension.



      The underlying problem is that you are trying to emulate the list append, without fully understanding dimensions, or what concatenate does. It makes a whole new array each time. np.append is not a clone of list.append!.



      What you should be doing is starting with a [] list (or two), append new values to that, making a list of lists. Then np.array(alist) to turn that into an array (provided, of course that all sublists match in size).



      I haven't paid attention to your write, or how you read the lines, so can't say whether that makes sense or not.






      share|improve this answer



























        0












        0








        0







        Twice you create a np.array([]). Pay attention to its shape:



        In [140]: x = np.array([]) 
        In [141]: x.shape
        Out[141]: (0,)


        It does work in np.append - that's because without axis parameter, append is just concatenate((x, y), axis=0), e.g. adding a (0,) shape and a (3,) shape to create a (3,) shape:



        In [142]: np.append(x, np.arange(3)) 
        Out[142]: array([0., 1., 2.])


        But vstack doesn't work. It makes its inputs into 2d arrays, and joins them on the first axis:



        In [143]: np.vstack((x, np.arange(3))) 
        ValueError: all the input array dimensions except for the concatenation axis must match exactly


        So we are joining (0,) and (3,) on a new first axis, e.g. (1,0) and (1,3) on first axis. The 0 and 3 don't match, hence the error.



        vstack works when joining a (3,) with a (3,) and a (1,3), and a (4,3). Note the common 'last' dimension.



        The underlying problem is that you are trying to emulate the list append, without fully understanding dimensions, or what concatenate does. It makes a whole new array each time. np.append is not a clone of list.append!.



        What you should be doing is starting with a [] list (or two), append new values to that, making a list of lists. Then np.array(alist) to turn that into an array (provided, of course that all sublists match in size).



        I haven't paid attention to your write, or how you read the lines, so can't say whether that makes sense or not.






        share|improve this answer















        Twice you create a np.array([]). Pay attention to its shape:



        In [140]: x = np.array([]) 
        In [141]: x.shape
        Out[141]: (0,)


        It does work in np.append - that's because without axis parameter, append is just concatenate((x, y), axis=0), e.g. adding a (0,) shape and a (3,) shape to create a (3,) shape:



        In [142]: np.append(x, np.arange(3)) 
        Out[142]: array([0., 1., 2.])


        But vstack doesn't work. It makes its inputs into 2d arrays, and joins them on the first axis:



        In [143]: np.vstack((x, np.arange(3))) 
        ValueError: all the input array dimensions except for the concatenation axis must match exactly


        So we are joining (0,) and (3,) on a new first axis, e.g. (1,0) and (1,3) on first axis. The 0 and 3 don't match, hence the error.



        vstack works when joining a (3,) with a (3,) and a (1,3), and a (4,3). Note the common 'last' dimension.



        The underlying problem is that you are trying to emulate the list append, without fully understanding dimensions, or what concatenate does. It makes a whole new array each time. np.append is not a clone of list.append!.



        What you should be doing is starting with a [] list (or two), append new values to that, making a list of lists. Then np.array(alist) to turn that into an array (provided, of course that all sublists match in size).



        I haven't paid attention to your write, or how you read the lines, so can't say whether that makes sense or not.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Mar 23 at 5:33

























        answered Mar 23 at 4:57









        hpauljhpaulj

        119k788162




        119k788162





























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55310603%2fcant-read-binary-matrix-from-a-file-in-python-3-x%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

            용인 삼성생명 블루밍스 목차 통계 역대 감독 선수단 응원단 경기장 같이 보기 외부 링크 둘러보기 메뉴samsungblueminx.comeh선수 명단용인 삼성생명 블루밍스용인 삼성생명 블루밍스ehsamsungblueminx.comeheheheh

            155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해