Skip to content

thumbnail(BICUBIC) after copy() makes the image look like NEAREST #964

Closed

Description

@atreyas

I noticed that some of my images showed aliasing artifacts while others did not when I used the BICUBIC interpolation. Interestingly, this seemed to be directly related to the code path, so I dug deeper and found that if I do the following:

im = Image.open('/tmp/image.jpg')
im.thumbnail((256,256), Image.BICUBIC)
im.save('/tmp/image_cut1.jpg')

I get no artifacts, but when I do this:

im = Image.open('/tmp/image.jpg')
im1 = im.copy()
im1.thumbnail((256,256), Image.BICUBIC)
im1.save('/tmp/image_cut2.jpg')

I see the artifacts. It shows up exactly as I would get in NEAREST

Interesting, it seems like some state changes in the original Image object, which causes this:

im = Image.open('/tmp/image.jpg')
im1 = im.copy()
im.thumbnail((256,256), Image.BICUBIC)
im.save('/tmp/image_cut3.jpg')

Still gives me the same artifacts.
Note: I am cutting the "original" after copying.

This seems to happen with any operation that makes a copy - transpose, rotate, etc.

I will update this if I find anything more, but just throwing this out in case someone else notices or finds something here that I'm missing.

Activity

  1. wiredfool commented on Oct 21, 2014

    @wiredfool
    Member

    I can replicate a difference between open+thumbnail and open+copy+thumbnail, as well as open+thumbnail and open+load+thumbnail, but only when loading a JPEG. (not png or ppm).

    So what's happening is that the thumbnail call uses the draft method -- implemented in JpegImagePlugin, not implemented elsewhere. If you thumbnail the image prior to loading the bits from disk, Pillow will only use as many of the JPEG coefficients as necessary to get the resolution desired. If you load the image, either by explicitly calling load() or copy() or by using a different image type, you load all the pixels into memory and the call to draft() will be a noop.

    Note -- the docs on draft noted that it was not yet implemented, that is incorrect. It is implemented, but only for certain *ImagePlugins and only prior to loading.

  2. homm commented on Oct 21, 2014

    @homm
    Member

    @atreyas, could you share the image where you see artifacts?

  3. atreyas commented on Oct 21, 2014

    @atreyas
    Author

    Attaching the original and output after the operation.
    sample_bic1
    sample_bic2
    sample

  4. atreyas commented on Oct 21, 2014

    @atreyas
    Author

    The first image is the one after the copy operation - if you notice, it shows the same artifacts as a NEAREST. I know the PIL BICUBIC isn't accurate, but the same BICUBIC on JPEG shows no graininess. On images that have potential to show aliasing, this is predominantly aliased.

  5. homm commented on Oct 21, 2014

    @homm
    Member

    @atreyas, I get it.

    The problem is for now there two implementation of resize in PIL: im.stretch and im.resize. As you can see, the first one is used for Antialias filter, when second one for any other, including Bicubic. In fact both of them can handle Bicubic and Bilinear filtering, but only im.stretch can handle Lanczos filter (Image.ANTIALIAS).

    While both of them is relatively same for upscaling, they have differences in downscaling algorithm. im.stretch uses as match pixels for each result pixel as required. im.resize uses only two (Bilinear) nearest or four (Bicubic) pixels. That is why im.resize can drop pixels, which was not near resulting pixels. In my opinion im.resize should not be used for downscaling to ratio more than 2x.

    I don't know motivation of original PIL authors, but I see next difficulties with im.stretch: Performance. It is about 3 times lower than im.resize. And incorrect result for Bicubic.

    In your case you should try Lanczos filter. In my software I am using im.resize only for Nearest filter and im.stretch for Bilinear and Lanczos. And does not use Bicubic at all.

    def resize(self, size, resample=Image.NEAREST):
        """
        Modified version of PIL.Image.Image.resize.
        This version always uses im.stretch instead of im.resize.
        https://github.com/python-imaging/Pillow/blob/b5a128a/PIL/Image.py#L1352-L1362
        """
    
        if resample not in (Image.NEAREST, Image.BILINEAR,
                            Image.ANTIALIAS):
            raise ValueError("unknown resampling filter")
    
        self.load()
    
        if self.mode == 'RGBA':
            return resize(self.convert('RGBa'), size, resample).convert('RGBA')
    
        if self.mode in ("1", "P") or resample == Image.NEAREST:
            im = self.im.resize(size, Image.NEAREST)
        else:
            im = self.im.stretch(size, resample)
    
        return self._new(im)

    I think this can be fixed by fixing im.stretch implementation with Bicubic (and other bugs if exists) and removing im.resize at all, because it is incorrect. This is a long way to go.

  6. atreyas commented on Oct 21, 2014

    @atreyas
    Author

    @homm I noticed this piece of code. However it still doesn't explain why the artifacts show only when I make the copy of the image. Both the resizes are using BICUBIC. The mode at that point is 'RGB' - before and after the copy. So in both cases it is calling im = self.im.resize(size, resample) here: https://github.com/python-pillow/Pillow/blob/master/PIL/Image.py#L1550

    As @wiredfool mentioned, it seems that if the file isn't read, the fewer DCT components might cause it to behave better.

    Unfortunately, when I tested the AA filtering, it takes 30x more time consistently over BICUBIC (timeit at 450ms per resize vs 15ms for BICUBIC over 1000 runs best of 3). This would make the system too slow to be useful where I use this for a gallery view cutting images on the fly. However you do raise a good point that BICUBIC on images over 1/2 with a 4 point BICUBIC is not a good idea. Maybe we could get a parameter to switch to a 9-point implementation.

  7. homm commented on Oct 21, 2014

    @homm
    Member

    However it still doesn't explain why the artifacts show only when I make the copy of the image.

    Oh, it's simple. As @wiredfool explained, Pillow loads JPEG images lazy. This allows to read image downscaled with ratio 2x, 4x and 8x. The quality of this downsampling is better than im.resize. So in first case you're resizing high quality 509×679 image with algorithm which is fine with ratios up to 2x.

  8. homm commented on Oct 21, 2014

    @homm
    Member

    Unfortunately, when I tested the AA filtering, it takes 30x more time consistently over BICUBIC

    But Bicubic doesn't provide the result which you're expecting, while Lanczos does, right? This is because Lanczos have handled all pixels, while Bicubic only small part, similar to Nearest. I am also very fast when do nothing :)

  9. atreyas commented on Oct 21, 2014

    @atreyas
    Author

    I am also very fast when do nothing

    Right :)
    I think, for JPEGs, I will downscale first before any other operation. Or rescale to 1/2^x then resize using antialiasing - that would improve the performance. I'm basically worried about the massive 16+MP images from DSLRs if I use AA directly.

    Thanks for all the help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions