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

Face with "NoMethodError: undefined method `-' for nil:NilClass" error if compare images with NO DIFFERENCES #125

Closed
kay4444 opened this issue Oct 13, 2017 · 6 comments

Comments

@kay4444
Copy link

kay4444 commented Oct 13, 2017

error

When I try to compare images with no differences I face with "NoMethodError: undefined method `-' for nil:NilClass" error. I Use Windows 7 OS. Please see screenshot from my project.
But if I compare images with differences - no errors are shown.

@wvanbergen
Copy link
Owner

The error is coming from your own code (ukr_net_helper.rb), not ChunkyPNG.

If you can provide a snippet of code that reproduces it, I can look into it

@wvanbergen
Copy link
Owner

Closing this due to missing information. Feel free to reopen if the problem persists and you can provide more information.

@shoesCodeFor
Copy link

@wvanbergen I ran into this too. It was caused by trying to compare identical images. @kay4444's implementation does not have a catch for diff.length == 0.

Thanks for this awesome gem 👍 chunky_png is great!

@wvanbergen
Copy link
Owner

Can you share the full stack trace of the error?

@shoesCodeFor
Copy link

Hi @wvanbergen, here is an A/B test(using identical images). The first is the code with a conditional to catch identical images and the 2nd example is the way that @kay4444 implemented the code above.

require("chunky_png")

images = [
    ChunkyPNG::Image.from_file("1.png"),
    ChunkyPNG::Image.from_file("1copy.png")
]
diff = []
images.first.height.times do |y|
  images.first.row(y).each_with_index do |pixel, x|
    diff << [x,y] unless pixel == images.last[x,y]
  end
end

# Catch identical images
if diff.length > 0
  percentage_change = (diff.length.to_f / images.first.pixels.length) * 100
  x, y = diff.map{ |xy| xy[0] }, diff.map{ |xy| xy[1] }
  images.last.rect(x.min, y.min, x.max, y.max, ChunkyPNG::Color.rgb(0,255,0))
  images.last.save("diff.png")
else
  percentage_change = diff.length
end
puts "pixels (total):     #{images.first.pixels.length}"
puts "pixels changed:     #{diff.length}"
puts "pixels changed (%): #{percentage_change}%"

Outputs:

pixels (total):     3686400
pixels changed:     0
pixels changed (%): 0%

Process finished with exit code 0

Here is the intial implementation:

require("chunky_png")

images = [
    ChunkyPNG::Image.from_file("1.png"),
    ChunkyPNG::Image.from_file("1copy.png")
]
diff = []
images.first.height.times do |y|
  images.first.row(y).each_with_index do |pixel, x|
    diff << [x,y] unless pixel == images.last[x,y]
  end
end

percentage_change = (diff.length.to_f / images.first.pixels.length) * 100
x, y = diff.map{ |xy| xy[0] }, diff.map{ |xy| xy[1] }
images.last.rect(x.min, y.min, x.max, y.max, ChunkyPNG::Color.rgb(0,255,0))
images.last.save("diff.png")

puts "pixels (total):     #{images.first.pixels.length}"
puts "pixels changed:     #{diff.length}"
puts "pixels changed (%): #{percentage_change}%"

Returns the following error:

Traceback (most recent call last):
	2: from /Users/someuser/Library/Preferences/RubyMine2018.2/scratches/img_module/image_diff.rb:16:in `<main>'
	1: from /Users/someuser/.rvm/gems/ruby-2.5.1/gems/chunky_png-1.3.11/lib/chunky_png/canvas/drawing.rb:235:in `rect'
/Users/someuser/.rvm/gems/ruby-2.5.1/gems/chunky_png-1.3.11/lib/chunky_png/canvas/drawing.rb:97:in `line_xiaolin_wu': undefined method `-' for nil:NilClass (NoMethodError)

Process finished with exit code 1

I think this is caused by the attempt to draw a rectangle with no data.

Thanks again for this module, it has helped me immensely!
Schuyler

@wvanbergen
Copy link
Owner

The problem is that you are sending a nil value as parameter to the rect call. It expects that all parameters are integers. ChunkyPNG should probably give you a better error message in this case, but the issue is in your sample application.

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

3 participants