manpreet singh
manpreet singh

Reputation: 55

Converting image size into binary in python with using inbuilt methods

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.

Upvotes: 0

Views: 64

Answers (1)

pascscha
pascscha

Reputation: 1673

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]

Upvotes: 1

Related Questions