1

I am working on setting up image data set for styleGAN

I got this error TypeError: write_undefined() takes 2 positional arguments but 5 were given

I'd really appreciated if you can help! thanks

import os import glob from PIL import Image from PIL import ImageOps

imgDir = "/content/drive/MyDrive/EXIF" #@param {type:"string"}
imgOutDir = "/content/drive/MyDrive/2B/GAN_output" #@param {type:"string"}
width = 1024 #@param {type:"integer"}
height = 1024 #@param {type:"integer"}

files = glob.glob(imgDir + '/*')
print(files)
for f in files:
    title, ext = f.split('.')
    print(title, ext)
    if ext in ['jpg', 'png', 'jpeg', 'JPG', 'PNG', 'JPEG']:
        img = Image.open(f)
        ImageOps.exif_transpose(img)
        _width, _height = img.size
        
        size = 0
        if _width > _height:
            size = _height
        else:
            size = _width
        
        left = (_width - size) / 2
        top = (_height - size) / 2
        right = (_width + size) / 2
        bottom = (_height + size) / 2
        
        folder  = title.split("/")[:-1]
        file_name = title.split("/")[-1]
        img = img.crop((left, top, right, bottom))
        img_resize = img.resize((width, height))
       
takeru_910
  • 11
  • 1

2 Answers2

1

I also just ran into this bug so here are a few things I've found:

Here's a link to a relevant github issue

According to this comment, this problem only affects images with specific tags. Also this only applies to version 7, while version 6 had a different but similar issue that stopped exif_transpose from working. The comment says this wasn't fixed until version 8.1.0. I'm trying on version 9 and it appears to be fixed.

I'd recommend upgrading your version of pillow. If you're not able to do that, you could try this. I haven't tested it on pillow 7 but it looks like it may work.

    import Image, ExifTags

try :
    image=Image.open(os.path.join(path, fileName))
    for orientation in ExifTags.TAGS.keys() : 
        if ExifTags.TAGS[orientation]=='Orientation' : break 
    exif=dict(image._getexif().items())

    if   exif[orientation] == 3 : 
        image=image.rotate(180, expand=True)
    elif exif[orientation] == 6 : 
        image=image.rotate(270, expand=True)
    elif exif[orientation] == 8 : 
        image=image.rotate(90, expand=True)

    image.thumbnail((THUMB_WIDTH , THUMB_HIGHT), Image.ANTIALIAS)
    image.save(os.path.join(path,fileName))

except:
    traceback.print_exc()
0

ImageOps.exif_transpose(img) is not an in place operation.

That means: it returns a rotated image, but it does not rotate the img automatically.

So you should add img = before the operation, like so:

img = ImageOps.exif_transpose(img)
Andre Goulart
  • 528
  • 2
  • 20