In [1]:
import cv2, os

In [2]:
for f in os.listdir("dog"):
    if f.endswith(".jpg"):
        file_path=os.path.join("dog", f)
        print(f)
        img=cv2.imread(file_path,0)
        h,w=img.shape
        if w<h:
            margin=(h-w)//2
            croped=img[ margin:-margin, :]
        else:
            margin=(w-h)//2
            croped=img[ :, margin:-margin]


000ed05b-9f7e-4f32-8215-613924c0e25d.jpg
01607bd1-7586-4f40-b8e4-aeefcc8a4601.jpg
04230552-a0d8-47b4-b7e5-97f1a8e0a241.jpg
1ea7dd4a-304a-4d37-bbcf-6e94c5cec467.jpg
236e72bc-b1eb-40a7-a632-4845e4304f69.jpg
30681133-2170-46ea-8ef4-ea7491022353.jpg
3c4f73a3-bf98-45e3-951f-69251d0f2e0c.jpg
46079686-f186-4847-b111-800feef97c02.jpg
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-2-59661b6eeab7> in <module>()
      4         print(f)
      5         img=cv2.imread(file_path,0)
----> 6         h,w=img.shape
      7         if w<h:
      8             margin=(h-w)//2

AttributeError: 'NoneType' object has no attribute 'shape'

In [5]:
file_path


Out[5]:
'dog/46079686-f186-4847-b111-800feef97c02.jpg'

In [3]:
%matplotlib inline
from matplotlib import pyplot as plt
plt.imshow(img)


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-3-86c16ec3eaf2> in <module>()
      1 get_ipython().magic('matplotlib inline')
      2 from matplotlib import pyplot as plt
----> 3 plt.imshow(img)

~/anaconda/envs/tensorflow/lib/python3.6/site-packages/matplotlib/pyplot.py in imshow(X, cmap, norm, aspect, interpolation, alpha, vmin, vmax, origin, extent, shape, filternorm, filterrad, imlim, resample, url, hold, data, **kwargs)
   3155                         filternorm=filternorm, filterrad=filterrad,
   3156                         imlim=imlim, resample=resample, url=url, data=data,
-> 3157                         **kwargs)
   3158     finally:
   3159         ax._hold = washold

~/anaconda/envs/tensorflow/lib/python3.6/site-packages/matplotlib/__init__.py in inner(ax, *args, **kwargs)
   1896                     warnings.warn(msg % (label_namer, func.__name__),
   1897                                   RuntimeWarning, stacklevel=2)
-> 1898             return func(ax, *args, **kwargs)
   1899         pre_doc = inner.__doc__
   1900         if pre_doc is None:

~/anaconda/envs/tensorflow/lib/python3.6/site-packages/matplotlib/axes/_axes.py in imshow(self, X, cmap, norm, aspect, interpolation, alpha, vmin, vmax, origin, extent, shape, filternorm, filterrad, imlim, resample, url, **kwargs)
   5122                               resample=resample, **kwargs)
   5123 
-> 5124         im.set_data(X)
   5125         im.set_alpha(alpha)
   5126         if im.get_clip_path() is None:

~/anaconda/envs/tensorflow/lib/python3.6/site-packages/matplotlib/image.py in set_data(self, A)
    594         if (self._A.dtype != np.uint8 and
    595                 not np.can_cast(self._A.dtype, np.float)):
--> 596             raise TypeError("Image data can not convert to float")
    597 
    598         if (self._A.ndim not in (2, 3) or

TypeError: Image data can not convert to float

In [4]:
img

In [ ]: