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

Optimise ImageOps.fit by combining resize and crop #3409

Merged
merged 3 commits into from
Oct 18, 2018

Conversation

homm
Copy link
Member

@homm homm commented Oct 16, 2018

This optimises crop+resize operations sequence by using box parameter of resize function. This gives us:

  1. Performance. Results for lilliput-bench:

Before:

JPEG 256x256 => 32x32:  1109 Bytes,     avg: 1.43 ms    min: 0.92 ms    max: 4.43 ms
PNG 256x256 => 32x32:   2603 Bytes,     avg: 3.70 ms    min: 3.62 ms    max: 7.14 ms
WEBP 256x256 => 32x32:  654 Bytes,      avg: 2.77 ms    min: 2.48 ms    max: 6.30 ms
GIF 256x256 => 32x32:   1897 Bytes,     avg: 4.89 ms    min: 4.51 ms    max: 11.58 ms
JPEG 1920x1080 => 800x600:      107480 Bytes,   avg: 26.28 ms   min: 24.90 ms   max: 37.08 ms
PNG 1920x1080 => 800x600:       810489 Bytes,   avg: 282.60 ms  min: 277.43 ms  max: 377.78 ms
WEBP 1920x1080 => 800x600:      84960 Bytes,    avg: 126.52 ms  min: 123.86 ms  max: 149.28 ms
GIF 1920x1080 => 800x600:       411470 Bytes,   avg: 102.40 ms  min: 95.52 ms   max: 126.27 ms

After:

JPEG 256x256 => 32x32:	1109 Bytes,	avg: 0.95 ms	min: 0.74 ms	max: 4.93 ms
PNG 256x256 => 32x32:	2603 Bytes,	avg: 3.56 ms	min: 3.45 ms	max: 7.60 ms
WEBP 256x256 => 32x32:	654 Bytes,	avg: 2.77 ms	min: 2.47 ms	max: 6.36 ms
GIF 256x256 => 32x32:	1897 Bytes,	avg: 4.73 ms	min: 4.37 ms	max: 11.39 ms
JPEG 1920x1080 => 800x600:	107450 Bytes,	avg: 21.36 ms	min: 20.99 ms	max: 27.02 ms
PNG 1920x1080 => 800x600:	810516 Bytes,	avg: 277.31 ms	min: 272.55 ms	max: 328.37 ms
WEBP 1920x1080 => 800x600:	84952 Bytes,	avg: 125.57 ms	min: 121.98 ms	max: 154.98 ms
GIF 1920x1080 => 800x600:	411470 Bytes,	avg: 98.35 ms	min: 94.72 ms	max: 117.11 ms
  1. Accuracy. Unlike crop method, box for resize method accepts float coordinates. For example:
from PIL import Image, ImageOps
dot = Image.open('../dot.png')
ImageOps.fit(dot, (220, 200), Image.BICUBIC).save('../_out.png')

Source image (50x30px), processed image before, processed image after:

screenshot 2018-10-17 at 02 30 15
screenshot 2018-10-17 at 02 30 04
screenshot 2018-10-17 at 02 31 40

src/PIL/ImageOps.py Outdated Show resolved Hide resolved
@hugovk
Copy link
Member

hugovk commented Oct 17, 2018

Let's remember to add this to the release notes.

@homm
Copy link
Member Author

homm commented Oct 18, 2018

@hugovk Added release notes. Could you check?

Copy link
Member

@hugovk hugovk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

ImageOps.fit
^^^^^^^^^^^^

Now uses the one resize operation with ``box`` argument internally
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Now uses the one resize operation with ``box`` argument internally
Now uses one resize operation with ``box`` argument internally

^^^^^^^^^^^^

Now uses the one resize operation with ``box`` argument internally
instead of the crop and scale operations sequence.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
instead of the crop and scale operations sequence.
instead of a crop and scale sequence.

Now uses the one resize operation with ``box`` argument internally
instead of the crop and scale operations sequence.
This improves the performance and accuracy of cropping since
``box`` parameter accepts float values.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
``box`` parameter accepts float values.
the ``box`` parameter accepts float values.

@homm homm changed the title Optimise ImageOps.fit by combining resize in crop Optimise ImageOps.fit by combining resize and crop Oct 18, 2018
@homm homm merged commit 2222a58 into python-pillow:master Oct 18, 2018
@homm homm deleted the resize-from-box-in-fit branch October 18, 2018 08:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants