0
import PIL
import numpy as np
from PIL import Image
im = Image.open('youngerlrf005.jpg')
a1 = np.array()
width, height = im.size
for i in width:
    if i < 100:
        i = 0
        i = i+1
    else:
        i = 1
        i = i+1
        i.append(a1)
    for j in height:
        if j < 100:
            j = 0
            j = j+1
        else:
            j = 1
            j = j+1
            j.append(a1)


print(a1)

I am trying to get the image size in python but not using with inbuilt methods. I am not able to get the o/p as expected. I want to print the size of the image in the binary format in the matrix.

4
  • What your really doing is iterating through and integer which is not allowed Commented Jul 18, 2018 at 6:33
  • And you're printing an empty array Commented Jul 18, 2018 at 6:37
  • use: np.append(a1, j) instead of j.append(a1). Commented Jul 18, 2018 at 6:43
  • I think need to use pillow as PIL is no longer used in python3 Commented Jul 18, 2018 at 7:10

1 Answer 1

1

The easiest way would probably just be this:

print("{0:b}".format(width))

However, if you want to code the conversion yourself and store the bytes in an array, you could do it like that:

import PIL
import numpy as np
from PIL import Image

im = Image.open('youngerlrf005.jpg')
a1 = []
width, height = im.size

while width > 0:
    a1.append(width & 1)
    width = width >> 1

a1.reverse()

print(a1)

Just keep in mind, that the variable width will equal to zero after you do that, so if you need the width anywhere else, you'd have to redefine it width = im.size[0]

Sign up to request clarification or add additional context in comments.

3 Comments

Hi, its working. Its printing the values in the array. Thanks for the time
what is the c in a1.append(c & 1)?
@JeruLuke oops, that should be width not c

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.