python---图片格式转换问题,NotImplementedError: tostring() has been removed.

python—图片格式转换问题

学习用python简单处理图片:打开\显示\保存图像:https://www.cnblogs.com/denny402/p/5096001.html

出现如下问题:

1、python—图片格式转换问题

root@kali:~/python/laowangpy/function/binzcmsyzm# python
Python 2.7.3 (default, Nov 24 2017, 16:26:37) 
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from PIL import Image
>>> import matplotlib.pyplot as plt
>>> im = Image.open("11.jpg")
>>> plt.figure("11")
0x978746c>
>>> plt.imshow(im)
0x9a1a7cc>
>>> plt.show()
>>> print im.size
(126, 41)
>>> print im.mode
RGB
>>> print im.format
JPEG
>>> im.save("33.png")
>>> im = Image.open("20180106223129.png")
>>> plt.figure("999")
0x966516c>
>>> plt.imshow(im)
0x9c5f9ec>
>>> plt.show()
>>> print im.size
(50, 20)
>>> print im.mode
P
>>> print im.format
PNG
>>> im.save("20180106223129.jpg")
Traceback (most recent call last):
  File "", line 1, in 
  File "/usr/local/lib/python2.7/dist-packages/Pillow-4.4.0.dev0-py2.7-linux-i686.egg/PIL/Image.py", line 1931, in save
    save_handler(self, fp, filename)
  File "/usr/local/lib/python2.7/dist-packages/Pillow-4.4.0.dev0-py2.7-linux-i686.egg/PIL/JpegImagePlugin.py", line 607, in _save
    raise IOError("cannot write mode %s as JPEG" % im.mode)
IOError: cannot write mode P as JPEG
>>> im = im.convert("RGB")
>>> im.save("20180106223129.jpg")
>>> 

参考:http://bbs.csdn.net/topics/320035857

2、报错信息:NotImplementedError: tostring() has been removed. Please call tobytes() instead.

>>> from PIL import Image
>>> im = Image.open("timg.jpg")
>>> im.show()
>>> 
>>> import matplotlib.pyplot as plt
>>> im = Image.open("timg.jpg")
>>> plt.figure("timg")
0x9de830c>
>>> plt.imshow(im)
Traceback (most recent call last):
  File "", line 1, in 
  File "/usr/lib/pymodules/python2.7/matplotlib/pyplot.py", line 2377, in imshow
    ret = ax.imshow(X, cmap, norm, aspect, interpolation, alpha, vmin, vmax, origin, extent, shape, filternorm, filterrad, imlim, resample, url, **kwargs)
  File "/usr/lib/pymodules/python2.7/matplotlib/axes.py", line 6796, in imshow
    im.set_data(X)
  File "/usr/lib/pymodules/python2.7/matplotlib/image.py", line 402, in set_data
    self._A = pil_to_array(A)
  File "/usr/lib/pymodules/python2.7/matplotlib/image.py", line 1260, in pil_to_array
    x = toarray(im)
  File "/usr/lib/pymodules/python2.7/matplotlib/image.py", line 1245, in toarray
    x_str = im.tostring('raw',im.mode,0,-1)
  File "/usr/local/lib/python2.7/dist-packages/Pillow-4.4.0.dev0-py2.7-linux-i686.egg/PIL/Image.py", line 738, in tostring
    raise NotImplementedError("tostring() has been removed. "
NotImplementedError: tostring() has been removed. Please call tobytes() instead.
>>> 

参考:http://blog.csdn.net/hjxu2016/article/details/71055658

3、裁剪图片出现“SystemError: tile cannot extend outside image“错误信息。后两个数字必须比前两个大。

https://zhidao.baidu.com/question/1689969007353725628.html

>>> from PIL import Image
>>> import matplotlib.pyplot as plt
>>> img = Image.open("nv.jpg")
>>> gray = img.convert("L")
>>> plt.figure("beauty")
at 0x9c6baac>
>>> plt.subplot(1,2,1),plt.title("origin")
(at 0x9c6b9cc>, text.Text object at 0x993fcac>)
>>> plt.imshow(img),plt.axis("off")
(at 0x9e9be0c>, (-0.5, 1023.5, 694.5, -0.5))
>>> box=(300,200,260,300)
>>> roi=img.crop(box)
>>> plt.subplot(1,2,2),plt.title("roi")
(at 0x9a1ad4c>, text.Text object at 0xa1c67ec>)
>>> plt.imshow(roi),plt.axis("off")
Traceback (most recent call last):
  File "", line 1, in 
  File "/usr/lib/pymodules/python2.7/matplotlib/pyplot.py", line 2377, in imshow
    ret = ax.imshow(X, cmap, norm, aspect, interpolation, alpha, vmin, vmax, origin, extent, shape, filternorm, filterrad, imlim, resample, url, **kwargs)
  File "/usr/lib/pymodules/python2.7/matplotlib/axes.py", line 6796, in imshow
    im.set_data(X)
  File "/usr/lib/pymodules/python2.7/matplotlib/image.py", line 402, in set_data
    self._A = pil_to_array(A)
  File "/usr/lib/pymodules/python2.7/matplotlib/image.py", line 1260, in pil_to_array
    x = toarray(im)
  File "/usr/lib/pymodules/python2.7/matplotlib/image.py", line 1245, in toarray
    x_str = im.tostring('raw',im.mode,0,-1)
  File "/usr/local/lib/python2.7/dist-packages/Pillow-4.4.0.dev0-py2.7-linux-i686.egg/PIL/Image.py", line 742, in tostring
    return self.tobytes()
  File "/usr/local/lib/python2.7/dist-packages/Pillow-4.4.0.dev0-py2.7-linux-i686.egg/PIL/Image.py", line 722, in tobytes
    e.setimage(self.im)
SystemError: tile cannot extend outside image
>>> box=(100,200,260,300)
>>> roi=img.crop(box)
>>> plt.subplot(1,2,2),plt.title("roi")
(at 0x9a1ad4c>, text.Text object at 0xa1c67ec>)
>>> plt.imshow(roi),plt.axis("off")
(at 0x9c6bf8c>, (-0.5, 159.5, 99.5, -0.5))
>>> plt.show()
>>> 

参考:http://www.cnblogs.com/denny402/p/5096330.html
中”二、裁剪图片“

你可能感兴趣的:(python,PIL)