Python Forum
python-resize-image unicode decode error - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: python-resize-image unicode decode error (/thread-26091.html)



python-resize-image unicode decode error - Pedroski55 - Apr-21-2020

I am trying this from pypi.org here:

from PIL import Image
from resizeimage import resizeimage

fd_img = open('/home/pedro/backgrounds/pearlReflection.jpg', 'r')
img = Image.open(fd_img)
img = resizeimage.resize_contain(img, [827, 1169])
img.save('test-image-contain.jpeg', img.format)
fd_img.close()
I have tried other bits of their code and it worked.

I am getting the error below and I don't know why, as I am trying to follow the pypi.org instructions exactly:

Any idea what the problem is??

Quote:>>> pathTojpgs + jpgFiles[0]

'/home/pedro/backgrounds/pearlReflection.jpg'
>>> fd_img = open('/home/pedro/backgrounds/pearlReflection.jpg', 'r')

>>> fd_img

<_io.TextIOWrapper name='/home/pedro/backgrounds/pearlReflection.jpg' mode='r' encoding='UTF-8'>
>>> img = Image.open(fd_img)

Traceback (most recent call last):
File "<pyshell#66>", line 1, in <module>
img = Image.open(fd_img)
File "/home/pedro/.local/lib/python3.6/site-packages/PIL/Image.py", line 2852, in open
prefix = fp.read(16)
File "/usr/lib/python3.6/codecs.py", line 321, in decode
(result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte
>>>



RE: python-resize-image unicode decode error - Pedroski55 - Apr-21-2020

Well, I got past that problem, after lunch, with this:

Quote:>>> fd_img = open('/home/pedro/backgrounds/pearlReflection.jpeg', 'rb')

But now I'm stuck on save:

Quote:>>> img.save(name, img.format)
Traceback (most recent call last):
File "/home/pedro/.local/lib/python3.6/site-packages/PIL/JpegImagePlugin.py", line 612, in _save
rawmode = RAWMODE[im.mode]
KeyError: 'RGBA'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "<pyshell#26>", line 1, in <module>
img.save(name, img.format)
File "/home/pedro/.local/lib/python3.6/site-packages/PIL/Image.py", line 2134, in save
save_handler(self, fp, filename)
File "/home/pedro/.local/lib/python3.6/site-packages/PIL/JpegImagePlugin.py", line 614, in _save
raise OSError("cannot write mode %s as JPEG" % im.mode)
OSError: cannot write mode RGBA as JPEG
>>>

How to get around this error??


RE: python-resize-image unicode decode error - snippsat - Apr-21-2020

Look at cannot write mode RGBA as JPEG (4.2.0) #2609


RE: python-resize-image unicode decode error - Pedroski55 - Apr-21-2020

Thanks, that fixed it:

Quote:img = img.convert("RGB")

After that it saved fine!