1

Code

import cv2
import numpy as np
import sys
import pytesseract
from PIL import Image
reload(sys)
sys.setdefaultencoding('utf-8')


# Read image with opencv
img = cv2.imread("T.jpg")

print img.size
width , height = img.size

Error

   (C:\Users\SACHIN\Anaconda2) D:\>python R6extractor.py
    6220800
    Traceback (most recent call last):
      File "R6extractor.py", line 14, in <module>
        width , height = img.size
    TypeError: 'int' object is not iterable

But when I googled how to get the width and height of the image .Almost every example showed using width,height = image.size .And I went with it .But I am getting this error so I checked the content of image.size and I am getting 6220800.So what am I doing wrong here

2

1 Answer 1

3

As @theWanderer4865 said in the comments, img.size returns an integer, and you can't unpack it.

What you need to do is:

height, width, channels = img.shape

EDIT

If you wanted to open it using the Image library, the code would be like this:

from PIL import Image

# Read image with Image
img = Image('T.jpg')
img = cv2.imread("T.jpg")

width, height = img.size
Sign up to request clarification or add additional context in comments.

5 Comments

I will accept this as answer but can you clarify how this works stackoverflow.com/questions/6444548/…
In that question, and in the documentation, they open the image through the Image.open method. In your code, you're using cv2.imread instead.
so if I use open I can actually use image.size ?
Edited the answer
In short: CV2 returns img.size as integer. PIL returns img.size as (w, h)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.