I am currently writing a Python program that retrieves the pixels from two BMP files and finds the percent difference between them (without using the Pillow library for the purpose of learning). I can think of two ways to approach this problem:

1) Read the pixels from each file in separate streams and compare the results as the files are being read.
2) Read the pixels from one file into a multi-dimensional list, read the pixels from the other into another multi-dimensional list, and then compare the resulting lists one-by-one.

Out of these two approaches, which would be a more efficient choice to tackling my goal? Or is there another approach which would be better? I am currently working on 256x256 24-bit BMPs and plan on introducing images with larger resolutions.

  • By "percent difference" do you mean # of pixels not the same / # of total pixels? Commented Feb 6, 2020 at 18:46
  • @selectstriker2 Yep. Just a straight inequality check.
    – 8ask714
    Commented Feb 6, 2020 at 22:02

1 Answer 1


Reading data into an array requires allocating memory and means that you don't start comparing the data until you've finished loading the files. This, in theory, will take longer that comparing the data as it is read in. It also (naturally) requires more memory.

For such a small amount of data, this is not really a major concern however. Unless you are looking at really large files, it's likely going to be hard to notice a difference. Most people will find the array approach to be simpler to get right as well.

  • Streams are one-dimensional, bitmaps are most often bidimensional entities. Comparing them is way more complicated than looking at it pixel by pixel since you need to look all the pixels nearby and check your proximity rules. Doing this without loading the entire bitmap into memory first is... quite of an interesting bit of extra complexity, to say the least.
    – T. Sar
    Commented Feb 6, 2020 at 18:38
  • @T.Sar-ReinstateMonica, that depends entirely on the definition of "Compare." For simple equality or deviation, the linear stream is sufficient. For more complex comparisons such as object detection, it is probably not enough. In almost 20 years of machine vision work, I have never seen a two dimensional data structure for representing an image. It is always a single array of pixel data with metadata describing image width, height, and depth.
    – Dave Nay
    Commented Feb 6, 2020 at 19:36
  • @T.Sar-ReinstateMonica My answer is very basic because I am interpreting the question as a being about a very simple and straightforward comparison. I agree though that as your comparison algorithm becomes more complex, using a streaming solution becomes more problematic.
    – JimmyJames
    Commented Feb 6, 2020 at 19:47
  • @DaveNay my experience with machine vision isn't that impressive (mere three years), but our usual techniques always involve unpacking the bitmap on a 2d plane for analysis. It isn't that the image is packed as a 2d object on the file by itself, but rather that we use a 2d plane to distribute the pixels for easy of use.
    – T. Sar
    Commented Feb 6, 2020 at 20:37
  • @JimmyJames Thanks for the guidance. You were right that I was going for a simple comparison so your recommendation will work.
    – 8ask714
    Commented Feb 6, 2020 at 22:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.