Grasshopper

algorithmic modeling for Rhino

Hi Giulio

I have the below class in a GH Python component. After running it for the first time it locks the image. I can only release it by closing Rhino.I do not keep any global references in the GH component. Usage is similar to the test() example. I tried invoking garbage collection and setting is = None but the problem persists.

Do you have any idea how I could work around this problem or how it is caused? I need to edit the image in Photoshop while GH is running.

Thanks,

Silvan

class ImageSampler(object):
    """
    Bitmap sampling class that uses the .NET frameworks System.Drawing object
    """
    def __init__(self, path):
        self.img = System.Drawing.Bitmap(path)
    
    def sample_relative(self, u, v):
        ua = int(self.img.Width * u)
        va = int(self.img.Height * v)
        return self.img.GetPixel(ua, va)

def test():

    is = ImageSampler(some_path)

test()

Views: 1067

Replies to This Discussion

Hi Silvan,

Try calling the Dispose function on self.img

Hi Steve

Dispose() seems to work. Thanks. For everybody reading this thread, you have to call Dispose() from outside the class that wraps System.Drawing.Bitmap(), in the above example like this: is.img.Dispose(). I first wrapped Dispose() in the class and called the wrapping method. This did not release the resource and resulted in a memory corruption error in GH.

Best,

Silvan

Hi Silvan

if the class design truly expresses the underlying concept, then you should probably implement a close() method that freezes the resources used by the class.

One way could be adding a "def close(self):" with self.img.Dispose() inside to more properly expose a method that clears resources and closes the file.

I hope this helps,

- Giulio
________________
giulio@mcneel.com

Hi Giulio

Thanks for your answer. This is what I did first and what for some strange reason did not work, but in the below test code it does. Maybe the component was still calculating while I tried to save the image. It is pretty slow.

Anyway I'm fine. Thanks a lot for your help!

Cheers,

Silvan

import clr
clr.AddReference('System.Drawing')
import os
import rhinoscriptsyntax as rs


class ImageSampler(object):
    """
    Bitmap sampling class that uses the .NET frameworks System.Drawing object
    """
    def __init__(self, path):
        self.img = System.Drawing.Bitmap(path)
    
    def sample_relative(self, u, v):
        ua = int(self.img.Width * u)
        va = int(self.img.Height * v)
        return self.img.GetPixel(ua, va)
    
    def dispose(self):
        self.img.Dispose()


def test(img_name):
    if not img_name: return
    folder = rs.WorkingFolder()
    image_path = '{0}/{1}'.format(folder, img_name)
    img_sampler = ImageSampler(image_path)
    #img_sampler.img.Dispose()
    img_sampler.dispose()


test('TestMap.jpg')

RSS

About

Translate

Search

Photos

  • Add Photos
  • View All

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service