Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot use 'keep'. jpg not recognized as jpg #4036

Closed
soundreactor opened this issue Aug 26, 2019 · 6 comments
Closed

Cannot use 'keep'. jpg not recognized as jpg #4036

soundreactor opened this issue Aug 26, 2019 · 6 comments
Labels
Projects

Comments

@soundreactor
Copy link

soundreactor commented Aug 26, 2019

i was trying to resize images and keep the quality setting.
with quality set to a digit it works fine.
probably similar to issue:
#857

i suspect i have some sort of jpg type that is also not recognized as jpg, just like in the issue.
so i added it inside a zip.

What are your OS, Python and Pillow versions?

  • OS: windows
  • Python: 3.7.3
  • Pillow: 6.1.0

IMG_7679.zip

from PIL import Image
import os, sys

Image.MAX_IMAGE_PIXELS = None

path = "C:/Users/KE/Desktop/omas 80 Panting Images/"
dirs = os.listdir( path )

def resize():
    for item in dirs:
        if os.path.isfile(path+item) and item.endswith('.jpg'):
            print(path+item)
            # break
            im = Image.open(path+item)
            size = im.size


            f, e = os.path.splitext(path+item)

            if(size[0]>size[1]):
                imResize = im.resize((5000,int(5000*size[1]/size[0])), Image.ANTIALIAS)

            if(size[0]<size[1]):
                imResize = im.resize((int(5000*size[0]/size[1]),5000), Image.ANTIALIAS)


            f = f.replace(path, path + "max/")
            imResize.save(f + '.jpg', 'JPEG', quality='keep')
            # imResize.save(f + '.jpg', 'JPEG', quality=20)

resize()
@hugovk
Copy link
Member

hugovk commented Aug 26, 2019

Thank you for the report.

That's quite a bit of code, please could you reduce it to the smallest self-contained, runnable amount that still reproduces the problem? Tips here:

@radarhere radarhere changed the title Cannot use 'keep'. jpg not recognized as jpg. Cannot use 'keep'. jpg not recognized as jpg Aug 26, 2019
@radarhere radarhere added this to Backlog in Pillow Aug 26, 2019
@soundreactor
Copy link
Author

soundreactor commented Aug 26, 2019

so i think i found the problem.

this is what happens.

im = Image.open(path)
print(im.format)

JPEG

imResize = im.resize((500,500), Image.ANTIALIAS)
print(imResize.format)

None

somehow after im.resize the image is not a jpeg anymore.
i guess its not intended for that use case.
i'm now using. im.thumbnail((500,500)) which works fine. its still a jpeg and so it can keep the quality.

the only question is: if the None format is intended behavior or not?

test code

from PIL import Image

Image.MAX_IMAGE_PIXELS = None

path = "C:/Users/KE/Desktop/omas 80 Panting Images/IMG_7679.jpg"

im = Image.open(path)
print(im.format)

## this won't work
# imResize = im.resize((500,500), Image.ANTIALIAS)

##this will work
im.thumbnail((500,500))
imResize = im

print(imResize.format)

nupath = path.replace("IMG_7679", "IMG_7679_resized")
imResize.save(nupath, 'JPEG', quality='keep')

@radarhere
Copy link
Member

Yes. So im.thumbnail is modifying the image in-place, while im.resize is creating a copy of the image. The copy is no longer a JPEG - it doesn't take with it all of the information from im. This is how the Pillow copy operation behaves in general, it's not specific to your situation.

@radarhere
Copy link
Member

https://pillow.readthedocs.io/en/stable/reference/Image.html#PIL.Image.format

PIL.Image.format
The file format of the source file. For images created by the library itself (via a factory function, or by running a method on an existing image), this attribute is set to None.

@radarhere radarhere moved this from Backlog to In progress in Pillow Aug 26, 2019
@soundreactor
Copy link
Author

okey. makes sense.
i think i was confused by the error message and that's why i didn't look at the right place.
"Cannot use 'keep' when original image is not a JPEG"
i directly thought of the original input image. now its clear to me that that is of course silly, how would save know what the original input image was in the first place. it just cares if it gets a type JPEG or not.

@radarhere radarhere added the JPEG label Aug 26, 2019
@radarhere
Copy link
Member

Cool. Enjoy continuing to use Pillow. Let us know if you have any further questions

Pillow automation moved this from In progress to Closed Aug 26, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Pillow
  
Closed
Development

No branches or pull requests

3 participants