emcconville / wand

The ctypes-based simple ImageMagick binding for Python
http://docs.wand-py.org/
Other
1.41k stars 197 forks source link

UnicodeDecodeError with filenames containing ßöäü for example #210

Closed iconberg closed 9 years ago

iconberg commented 9 years ago

Hello,

i have following script that converts pictures in the "original" folder to two other folders with different sizes. Unfortanly i get "UnicodeDecodeError" for a lot of files. I use python34 and wand 0.3.9.

Here is my code:

import wand.image as imgw
import os

source = 'C:\Temp\media'
dsizes = [('normal', 1024), ('klein', 320)]

for dirName, dirs, files in os.walk(source, topdown=True):
    for fname in files:
        if 'original' in dirName.split(sep='\\'):
            if fname.endswith('.jpg'):
                for dest in dsizes:
                    dd = dirName.replace('original', dest[0])
                    if not os.path.exists(dd):
                        os.makedirs(dd)
                    fp = "%s\%s" % (dirName, fname)
                    fp2 = fp.replace('original', dest[0])
                    with imgw.Image(filename=fp) as img:
                        ratio = img.width / img.height
                        newheight = int(dest[1] / ratio)
                        img.resize(dest[1], newheight)
                        img.save(filename=fp2)

Here is the traceback for a file named "wüste.jpg":

Traceback (most recent call last):
  File "C:/Python34/media_sizes.py", line 17, in <module>
    with imgw.Image(filename=fp) as img:
  File "C:\Python34\lib\site-packages\wand\image.py", line 1991, in __init__
    self.read(filename=filename, resolution=resolution)
  File "C:\Python34\lib\site-packages\wand\image.py", line 2048, in read
    self.raise_exception()
  File "C:\Python34\lib\site-packages\wand\resource.py", line 218, in raise_exception
    e = self.get_exception()
  File "C:\Python34\lib\site-packages\wand\resource.py", line 213, in get_exception
    message = message.decode()
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xfc in position 46: invalid start byte

Is there a quick workaround for that?

dahlia commented 9 years ago

Seems the error is raised when it tries decoding ImageMagick’s internal error message string (that is bytestring). I wonder how the bytestring is encoded.

Anyway here’s a workaround:

diff --git a/wand/resource.py b/wand/resource.py
index 6648bba..609a389 100644
--- a/wand/resource.py
+++ b/wand/resource.py
@@ -210,7 +210,7 @@ class Resource(object):
         exc_cls = TYPE_MAP[severity.value]
         message = desc.value
         if not isinstance(message, string_type):
-            message = message.decode()
+            message = message.decode(errors='replace')
         return exc_cls(message)

     def raise_exception(self, stacklevel=1):
iconberg commented 9 years ago

Thx, i tracked it down to message.decode() but had no clue to hande it. Got 4Gbyte pictures today to convert, you saved my day :)

iconberg commented 9 years ago

Unfortunaly the workaround didnt work as expected:

Traceback (most recent call last):
  File "C:\Python34\media_sizes.py", line 49, in <module>
    convert()
  File "C:\Python34\media_sizes.py", line 30, in convert
    with imgw.Image(filename=fp) as img:
  File "C:\Python34\lib\site-packages\wand\image.py", line 1991, in __init__
    self.read(filename=filename, resolution=resolution)
  File "C:\Python34\lib\site-packages\wand\image.py", line 2048, in read
    self.raise_exception()
  File "C:\Python34\lib\site-packages\wand\resource.py", line 223, in raise_exception
    raise e
wand.exceptions.BlobError: unable to open image `C:\Temp\media\original\W�ste.jpg': No such file or directory @ error/blob.c/OpenBlob/2701

As workaround i run a script that makes renaming before and after.