def doExport(self): """ Saves the image to the defined filename. """ import Krita, Image, ImageFile krtimage = Krita.image() krtlayer = Krita.activeLayer().paintDevice() #krtcolorspaceid = krtlayer.colorSpaceId() height = krtlayer.height() width = krtlayer.width() shell = Krita.shell() shell.slotSetStatusBarText("Python Imaging Library Export") shell.slotProgress(0) size = width * height progress = 0 pixeldone = 0 try: pilimage = Image.new( "RGB", (width,height) ) it = krtlayer.createRectIterator(0, 0, width, height) finesh = it.isDone() while (not finesh): pilimage.putpixel( (it.x(),it.y()), tuple(it.pixel()) ) percent = pixeldone * 100 / size if percent != progress: progress = percent shell.slotProgress( progress ) pixeldone += 1 finesh = it.next() #pilimage.save(self.filename,"JPEG") pilimage.save(self.filename) finally: shell.slotProgress(-1) shell.slotSetStatusBarText("")
def doImport(self): """ Loads the image from the defined filename and imports it. """ print ">>>>>>>>>>>>>>>>>>>>>>>> IMPORT filename=\"%s\" colorspace=\"%s\" destination=\"%s\" size=\"%s\"" % (self.filename,self.colorspace,self.destination,self.size) import Krita, Image, ImageFile # read the imagefile which should be imported. pilimage = Image.open(self.filename) # convert the readed image into the defined colorspace. if not self.colorspace in ["RGB","CMYK"]: raise "Unknown colorspace option \"%s\"" % self.colorspace pilimage = pilimage.convert( self.colorspace ) (width, height) = pilimage.size krtimage = Krita.image() # evaluate the image size options if self.size == "Resize": if krtimage.width() != width or krtimage.height() != height: krtimage.resize(width, height, 0, 0) elif self.size == "Scale": if krtimage.width() != width or krtimage.height() != height: width = krtimage.width() height = krtimage.height() pilimage = pilimage.resize( (width, height) ) elif self.size == "Ignore": if width > krtimage.width(): width = krtimage.width() if height > krtimage.height(): height = krtimage.height() else: raise "Unknown size option \"%s\"" % self.size # evaluate the destination options. if self.destination == "ActiveLayer": krtlayer = Krita.activeLayer().paintDevice() elif self.destination == "NewLayer": if self.colorspace == "CMYK": cs = "CMYK" else: cs = "RGBA" krtlayer = krtimage.createPaintLayer("pilimport", 100, cs) else: raise "Unknown destination option \"%s\"" % self.destination #krtcolorspaceid = krtlayer.colorSpaceId() #krtlayer.convertToColorspace() #if colorspaceid == "RGBA": # RGB (8-bit integer/channel) #self.mode = "P" # 8-bit palette-mapped image. ##elif colorspaceid == "RGBA16": # RGB (16-bit integer/channel) ##elif colorspaceid == "RGBAF16HALF": # "RGB (16-bit float/channel) ##elif colorspaceid == "RGBAF32": # RGB (32-bit float/channel) ##elif colorspaceid == "GRAYA": # Grayscale (8-bit integer/channel) ##elif colorspaceid == "GRAYA16": # Grayscale (16-bit integer/channel) ##elif colorspaceid == "CMYK": # CMYK (8-bit integer/channel) ##elif colorspaceid == "CMYKA16": # CMYK (16-bit integer/channel) ##elif colorspaceid == "LMSAF32": # "LMS Cone Space (32-bit float/channel)" ##elif colorspaceid == "YCbCrAU16": # YCBCR (16-bit integer/channel) ##elif colorspaceid == "YCbCrAU8": # YCBCR (8-bit integer/channel) ##elif colorspaceid == "WET": # Watercolors ##elif colorspaceid == "W&S": # Wet & Sticky #else: #raise "The Krita colorspace \"%s\" is not supported by the KritaPil-plugin" shell = Krita.shell() shell.slotSetStatusBarText("Python Imaging Library Import") shell.slotProgress(0) size = width * height progress = 0 pixeldone = 0 krtlayer.beginPainting("PIL import") try: it = krtlayer.createRectIterator(0, 0, width, height) finesh = it.isDone() while (not finesh): data = pilimage.getpixel( (it.x(),it.y()) ) it.setPixel( list(data) ) percent = pixeldone * 100 / size if percent != progress: progress = percent shell.slotProgress( progress ) pixeldone += 1 finesh = it.next() finally: krtlayer.endPainting() shell.slotProgress(-1) shell.slotSetStatusBarText("")
def __init__(self): # import the Krita module. try: import Krita except: raise "Import of the Krita module failed." # fetch the image. image = Krita.image() # we like to manipulate the active painting layer. layer = Krita.activeLayer().paintDevice() # get the height and the width the layer has. width = layer.width() height = layer.height() # we like to use the progressbar shell = Krita.shell() shell.slotSetStatusBarText("invert.py") shell.slotProgress(0) size = width * height progress = 0 pixeldone = 0 try: # tell Krita that painting starts. the whole painting session will be # counted till layer.endPainting() was called as one undo/redo-step. layer.beginPainting("invert") # create an iterator to walk over all pixels the layer has. it = layer.createRectIterator(0, 0, width, height) # iterate over all pixels and invert each pixel. print "kikoo\n" finesh = it.isDone() while not finesh: # p = it.channel() # p[0] = 255 - p[0] # p[1] = 255 - p[1] # p[2] = 255 - p[2] # it.setCannel(p) # invert the color of the pixel. it.invertColor() # increment the progress to show, that work on this pixel is done. percent = pixeldone * 100 / size if percent != progress: progress = percent shell.slotProgress(progress) pixeldone += 1 # go to the next pixel. finesh = it.next() # painting is done now. layer.endPainting() finally: # finish progressbar shell.slotProgress(-1) shell.slotSetStatusBarText("")