Python Forum
python-resize-image unicode decode error
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
python-resize-image unicode decode error
#1
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
>>>
Reply
#2
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??
Reply
#3
Look at cannot write mode RGBA as JPEG (4.2.0) #2609
Reply
#4
Thanks, that fixed it:

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

After that it saved fine!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Photo image error pyc0de 2 171 Mar-23-2024, 06:20 PM
Last Post: pyc0de
  error "cannot identify image file" part way through running hatflyer 0 616 Nov-02-2023, 11:45 PM
Last Post: hatflyer
  Decode string ? JohnnyCoffee 1 791 Jan-11-2023, 12:29 AM
Last Post: bowlofred
  Tkinter Pillow Resize Image Looks Choppy While Resizing AaronCatolico1 2 1,326 Jan-03-2023, 05:41 PM
Last Post: AaronCatolico1
  pygame image load error Yegor123 1 1,487 Oct-12-2022, 05:36 AM
Last Post: deanhystad
  Upload image to Instagram using python/selenium using image URL failed, need help greenpine 5 5,340 Feb-22-2022, 09:42 PM
Last Post: snippsat
  code decode, string, image ... teckow 2 2,025 Aug-20-2021, 07:02 PM
Last Post: teckow
  UnicodeDecodeError: 'utf-8' codec can't decode byte 0x92 error from Mysql call AkaAndrew123 1 3,386 Apr-28-2021, 08:16 AM
Last Post: AkaAndrew123
  JSON Decode error when using API to create dataframe Rubstiano7 4 2,882 Jan-11-2021, 07:52 PM
Last Post: buran
  how to encode and decode same value absolut 2 2,280 Sep-08-2020, 09:46 AM
Last Post: TomToad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020