4

I'm not sure exactly how the algorithm works.

In the general case, I'm wondering if I resize an image to a larger size and then resize it down back to the original size, will the image be the same as the original?

Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
StarckOverflar
  • 1,577
  • 2
  • 10
  • 13

1 Answers1

2

It depends on the interpolation type that you use, and the type of the image. For example, NEAREST and BILINEAR should give identical results if you use the same method in both directions. BICUBIC and LANCZOS may not be as invertible in the general case. None of the methods will be perfectly invertible if you do downsampling first, before upsampling. Most will not be invertible if you use different filters in the two directions. Using BICUBIC to upsample and NEAREST to downsample should yield identical results most of the time, since splines guarantee the value at the nodes. It will not work if the locations chosen for the downsampling are off (but that can be fixed with appropriate padding).

If your intention is to use the resize method without an explicit argument to upsample and then downsample, the result is likely to be invertible in the general case since it will use NEAREST in both directions.

Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
  • Tried, what you stated on PIL. Only `NEAREST` (or no *resample* argument) on both `resize()` prevails in getting the same data back (I used data as i am referring to color values of pixels). – Vasu Deo.S Jul 15 '19 at 19:36
  • @VasuDeo.S. Thanks for the feedback. I did not have a chance to test, just based my answer off my knowledge of the algorithms involved. It actually makes sense that something like BILINEAR would not line up correctly to make the reverse tranfsormation work out without padding. – Mad Physicist Jul 15 '19 at 19:39
  • Nice answer (+1), Btw can you provide me a source, from where i can learn about working of *resampling* methods (in detail). – Vasu Deo.S Jul 15 '19 at 19:42
  • @VasuDeo.S. I don't have any readily available online resource. However, if you google/wikipedia the filter names on https://pillow.readthedocs.io/en/latest/handbook/concepts.html#concept-filters, you should be off to a good start. I learned most of this stuff on the job and from books, which I don't remember at this point. – Mad Physicist Jul 15 '19 at 19:44