1

Im trying to grayscale an image in JES, the image is about 3000x2000 in size. Whenever I run it, gives me OutOfMemoryError: java.lang.OutOfMemoryError: Java heap space.

Im fairly new to this language so Im not sure if its my code thats the problem.

def grayscale(pic):
 for p in getPixels(pic):
  intensity = (getRed(p) + getGreen(p) + getBlue(p))/3
  setColor(p, makeColor(intensity, intensity, intensity))

The output should just be a grayscaled version of the image

dexdz
  • 43
  • 4
  • 2
    Either your `getPixels()` or `setColor()` has a memory leak somewhere (calls itself or creates too many variables to store in the stack). That or your image is enormous. – Matt Apr 11 '19 at 02:37
  • @Matt its size is 2953x2088. Any idea how i would go about optimizing my function? – dexdz Apr 11 '19 at 02:40
  • What data structures are you workign with here? – juanpa.arrivillaga Apr 11 '19 at 02:42
  • what happens when you try it on smaller image? – Aditya Santoso Apr 11 '19 at 02:52
  • @AdityaSantoso It works fine, and gives me what I expect. – dexdz Apr 11 '19 at 02:54
  • off-topic: using naive RGB average might not give you the best grayscale conversion. See [here](https://stackoverflow.com/questions/17615963/standard-rgb-to-grayscale-conversion) and [here](https://en.wikipedia.org/wiki/Grayscale#Converting_color_to_grayscale). You might be better of using some image manipulation library, unless if this is done as an exercise – Aditya Santoso Apr 11 '19 at 02:56
  • @dexdz: since you are running python on JVM, can it be the size of your heap is too small? see [here](https://stackoverflow.com/questions/5065840/memory-limit-for-jython) to adjust the JVM heap limit – Aditya Santoso Apr 11 '19 at 02:57

0 Answers0