Skip to content

Resize of pillow in python gives different results #21

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

Open
codeblocker-locc opened this issue Apr 2, 2025 · 1 comment
Open

Resize of pillow in python gives different results #21

codeblocker-locc opened this issue Apr 2, 2025 · 1 comment

Comments

@codeblocker-locc
Copy link

cpp code :
#include <PillowResize/PillowResize.hpp>
#include <opencv2/opencv.hpp>
#include
int main()
{
cv::Mat image = cv::imread("/docker/output_detection_1.png", cv::IMREAD_UNCHANGED);
cv::cvtColor(image, image, cv::COLOR_BGR2RGB);

      int target_size = 224;
      std::cout << "Size of image in function : " << image.size() << std::endl;
      cv::Size dst_size(target_size, target_size);
      int filter = cv::INTER_LINEAR;
      image = PillowResize::resize(image, dst_size, filter);
  return 0;
  }

Python Code :
from PIL import Image
import numpy as np
image = Image.open("/workspace/output_detection_1.png")
image = image.convert("RGB")
resized_image = image.resize((224, 224), Image.BILINEAR )

I have dumped the resized image numpy array in file and got significant diff in both the arrays when comparing both the images.

@marcov868
Copy link
Contributor

It is likely caused by a mismatch between the value of the filter you are using in C++. As stated in the docs, PillowResize::resize uses values from the enum PillowResize::InterpolationMethods, which has a 1:1 mapping with the resampling filters enum from PIL.
Instead, the cv::InterpolationFlags has a different enumeration, so you are not actually applying the bilinear interpolation but the Lanczos one.

Can you try changing your code to something like this?

{
      cv::Mat image = cv::imread("/docker/output_detection_1.png", cv::IMREAD_UNCHANGED);
      cv::cvtColor(image, image, cv::COLOR_BGR2RGB);

      int target_size = 224;
      std::cout << "Size of image in function : " << image.size() << std::endl;
      cv::Size dst_size(target_size, target_size);
      int filter = PillowResize::InterpolationMethods::INTERPOLATION_BILINEAR;  // <-- value from PillowResize::InterpolationMethods
      image = PillowResize::resize(image, dst_size, filter);
      return 0;
  }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants