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
Inconsistent size when using thumbnail
#6528
Comments
https://pillow.readthedocs.io/en/stable/reference/Image.html#PIL.Image.Image.thumbnail
I don't think it's random. I think it's resizing the image to fit within the size specified. |
If you want a method to reduce an image by an integer factor, we have the |
>>> 162 / 260 * 250
155.76923076923077 So I don't think you should ever get (250, 155), if the aspect ratio is being strictly kept. If you want (250, 155) anyway, then I think you should If you want to scale an image by only one dimension, then I think that using from PIL import Image, ImageOps
img2 = Image.new('L', (260, 162))
scale = 250 / img2.width
x = img2.width * scale
img3 = ImageOps.contain(img2, (int(x), 1000000))
print(img3.size) # (250, 156) @0phoff does any of that answer your question? |
Thanks for the expansive answer! I am currently optimizing my data loading pipeline for a deep learning application and wanted to use My main frustration comes from the fact that for rescaling while maintaining aspect ratio, you should not have to pass both a width and height. Only one of them is required and the other can be automatically computed. What's more, the current API does not easily allow to control whether you keep a fixed width or height. I could not figure out how to build a pipeline that thumbnails my images to a size of 250x... Depending on the original image size, the Would it be a possibility to expand the API so users can pass FYI, I might be completely mistaken because I am not familiar with the pillow codebase, but |
You make a good point. You can workaround this by calling |
I've been presuming you want this API just for convenience, but if you do mean that you can't figure out how to do it at present, then you could just use a high value for the other dimension. from PIL import Image
img2 = Image.new('L', (260, 162))
scale = 250 / img2.width
img2.thumbnail((img2.width * scale, 100000))
print(img2.size) # (250, 156) I made this comment about |
If this change were added to |
PR #6539 has been merged. |
What did you do?
I want to use the
thumbnail
function to resize images to a certain width, whilst keeping the aspect ratio.I thus computed a rescaling factor by dividing my target width by the actual width, and then used that scale to compute a new size for the thumbnail function (See code below).
The thumbnail function recomputes the width & height to match the aspect ratio as close as possible.
However, you cannot choose whether it adapts the width or the height in order to achieve this.
Looking at the code, it seems to be adapting the width if the new aspect ratio is bigger than the old, and the height otherwise.
I might by wrong on this, but isn't this pretty random?
Couldn't you preserve the aspect ratio by only changing either width or height.
It would be nice if the
thumbnail
method would allow you to either:scale
instead of a width & height. That way we simply compute the new size as:floor(x * scale), floor(y * scale)
.What are your OS, Python and Pillow versions?
The text was updated successfully, but these errors were encountered: