def export_layers_info(image, drawable, filepath, ddspath, ddspathInXml): rootElem = ET.Element("TextureAtlas") rootElem.set("file", ddspathInXml) id = 1; for layer in image.layers: write_layer(layer, rootElem, id) id += 1 indent(rootElem) tree = ET.ElementTree(rootElem) tree.write(filepath) dplimg = image.duplicate() dplimg.merge_visible_layers(1) # EXPAND-AS-NECESSARY (0), CLIP-TO-IMAGE (1), CLIP-TO-BOTTOM-LAYER (2) #Compression format (0 = None, 1 = BC1/DXT1, 2 = BC2/DXT3, 3 = BC3/DXT5, 4 = BC3n/DXT5nm, #5 = BC4/ATI1N, 6 = BC5/ATI2N, 7 = RXGB (DXT5), 8 = Alpha Exponent (DXT5), 9 = YCoCg (DXT5), 10 = YCoCg scaled (DXT5)) compression_format = 0 mipmaps = 0 savetype = 0 format = 0 transparent_index = -1 mipmap_filter=0 mipmap_wrap = 0 gamma_correct=0 gamma = 2.2 srgb = 0 perceptual_metric = 0 preserve_alpha_coverage = 0 alpha_test_threshold = 0.5 dplimg.active_layer.resize_to_image_size() gimp.pdb.file_dds_save(dplimg, dplimg.active_layer, ddspath, ddspath, compression_format, mipmaps, savetype, format, transparent_index, mipmap_filter, mipmap_wrap, gamma_correct, srgb, gamma, perceptual_metric, preserve_alpha_coverage, alpha_test_threshold) gimp.delete(dplimg)
def export_layers_info(image, drawable, filepath, ddspath, ddspathInXml): rootElem = ET.Element("TextureAtlas") rootElem.set("file", ddspathInXml) id = 1; for layer in image.layers: write_layer(layer, rootElem, id) id += 1 indent(rootElem) tree = ET.ElementTree(rootElem) tree.write(filepath) dplimg = image.duplicate() dplimg.merge_visible_layers(1) # EXPAND-AS-NECESSARY (0), CLIP-TO-IMAGE (1), CLIP-TO-BOTTOM-LAYER (2) #Compression format (0 = None, 1 = BC1/DXT1, 2 = BC2/DXT3, 3 = BC3/DXT5, 4 = BC3n/DXT5nm, #5 = BC4/ATI1N, 6 = BC5/ATI2N, 7 = RXGB (DXT5), 8 = Alpha Exponent (DXT5), 9 = YCoCg (DXT5), 10 = YCoCg scaled (DXT5)) compression_format = 0 mipmaps = 0 savetype = 0 format = 0 transparent_index = -1 color_type=0 dither = 0 mipmap_filter=0 gamma_correct=0 gamma = 2.2 dplimg.active_layer.resize_to_image_size() gimp.pdb.file_dds_save(dplimg, dplimg.active_layer, ddspath, ddspath, compression_format, mipmaps, savetype, format, transparent_index, color_type, dither, mipmap_filter, gamma_correct, gamma) gimp.delete(dplimg)
def python_ndsexport_rgb256(img, drawable, filename, raw_filename): pdb.gimp_layer_resize_to_image_size(drawable) width = drawable.width height = drawable.height fileOut = open(filename, "wb") ## TODO : Use pixelregion instead of gimp_drawable_get_pixel #pr = drawable.get_pixel_rgn(0, 0, width, height, False, False) gimp.progress_init(_("Saving as Nintendo DS RGB256 (256 colors palette)")) imageIndex, indexedDrawable = index_copy_of_image(img, 16) fileOut.write("256c") # file header fileOut.write(struct.pack("<II", width, height)) for y in range(0, height): for x in range(0, width): (channels, pixel) = pdb.gimp_drawable_get_pixel(indexedDrawable, x, y) # TO OPTIMIZE, very slow value = pixel[0] fileOut.write(struct.pack("<B", value)) gimp.progress_update( float((x + y * width) * 0.8) / (width * height)) createPalette(imageIndex, 256, fileOut) gimp.progress_update(1) fileOut.close() gimp.delete(img)
def validate(self, widget): #self.bar_progress.set_fraction(0.2) #gtk.main_iteration() #TODO : préciser dépendance python-yaml import yaml if os.path.exists(self.yaml_file): try: serialize = yaml.load(open(self.config_chooser.get_filename())) except: gimp.pdb.gimp_message(_('Your Yaml file is corrrupt. Check it carefully!')) gtk.main_quit() #increment for the progress bar progress_inc = 1/float(len(serialize)) self.progress = 0 self.inc = 0 self.sprite_name = '' for instruction in serialize: #advance the barre progression self.inc = self.inc + 1 self.progress = self.inc*progress_inc self.sprite_name = instruction extension = os.path.splitext(instruction)[1] attr = serialize[instruction] instruction = self.chooser.get_current_folder()+'/'+instruction new_img = pdb.gimp_image_duplicate(self.img) #picture become an composite file if type(attr['layers']) == dict: self._composite_picture(attr, new_img, instruction) continue #remove layers not selected for layer in new_img.layers: if layer.name in attr['layers']: layer.visible = True else: new_img.remove_layer(layer) #merge when the picture is not .xcf or .ora file if extension not in ['.xcf', '.ora']: new_img.merge_visible_layers(CLIP_TO_IMAGE) new_img.active_layer.resize_to_image_size() new_img.crop(attr['crop_width'], attr['crop_height'], attr['x'], attr['y']) new_img.scale(attr['width'] , attr['height']) pdb.gimp_file_save(new_img, new_img.active_layer, instruction, instruction) gimp.delete(new_img) pdb.gimp_progress_uninstall(self.process) else: #TODO : Create good Error Message #gimp.pdb.gimp_message("error message") #gimp.pdb.gimp_message_set_handler( ERROR_CONSOLE ) print 'null' gtk.main_quit()
def saveaspng(self, fname, image, transparency): imgcopy = pdb['gimp_image_duplicate'](image) if transparency: imgcopy.merge_visible_layers(CLIP_TO_BOTTOM_LAYER) else: imgcopy.flatten() pdb['file_png_save'](imgcopy, imgcopy.active_layer, fname, fname, False, 9, False, False, False, False, True) gimp.delete(imgcopy)
def image_gimp_to_pil(image): try: sandbox = image.duplicate() for layer in sandbox.layers: if not layer.visible: sandbox.remove_layer(layer) sandbox.merge_visible_layers(CLIP_TO_IMAGE) layer = sandbox.layers[0] if not layer.is_rgb: pdb.gimp_image_convert_rgb(sandbox) if layer.has_alpha: pdb.gimp_layer_flatten(layer) w, h = layer.width, layer.height pixels = layer.get_pixel_rgn(0, 0, w, h)[:, :] # all pixels return Image.frombuffer('RGB', (w, h), pixels, 'raw', 'RGB', 0, 0) finally: gimp.delete(sandbox)
def python_ndsexport_A3I5(img, drawable, filename, raw_filename): pdb.gimp_layer_resize_to_image_size(drawable) width = drawable.width height = drawable.height fileOut = open(filename, "wb") ## TODO : Use pixelregion instead of gimp_drawable_get_pixel #pr = drawable.get_pixel_rgn(0, 0, width, height, False, False) gimp.progress_init(_("Saving as Nintendo DS A3I5")) imageIndex, indexedDrawable = index_copy_of_image(img, 16) fileOut.write("a3i5") # file header fileOut.write(struct.pack("<II", width, height)) for y in range(0, height): for x in range(0, width): (channels, pixel) = pdb.gimp_drawable_get_pixel(drawable, x, y) # TO OPTIMIZE, very slow if channels < 4: alpha = 7 else: alpha = round((pixel[3] / 255) * 7) (channels, pixel) = pdb.gimp_drawable_get_pixel(indexedDrawable, x, y) # TO OPTIMIZE, very slow index = pixel[0] value = int(alpha) << 5 value += index fileOut.write(struct.pack("<B", value)) gimp.progress_update( float((x + y * width) * 0.8) / (width * height)) createPalette(imageIndex, 32, fileOut) gimp.progress_update(1) fileOut.close() gimp.delete(img)
def contact_sheet(file_type, location, all_subdirs, inc_filename, inc_extension, contact_name, contact_type, contact_location, contact_size, dpi, orient, num_col, num_rows, PageBorderLR, PageBorderTB, mmTHUMB_MARGIN, mmFONT_SIZE, Dump_Filename_list, Sorted_Images, Print_Contactsheet): # pdb.gimp_message (location) if os.path.exists(u'' + location): # pdb.gimp_message ("continuing1") # do nothing just set a variable xblot = 1 else: pdb.gimp_message(_("%s doesn't exist") % (location)) return if os.path.exists(u'' + contact_location): # pdb.gimp_message ("continuing2") # do nothing just set a variable xblot = 1 else: pdb.gimp_message(_("%s doesn't exist") % (contact_location)) return #collect 'all' images in the choosen directory and subdirs images = get_images(file_type, location, all_subdirs, Dump_Filename_list, Sorted_Images) num_images = len(images) #calculate number of images #if necessary make a txt file with image name and image directory if (Dump_Filename_list == True): Make_DirFile_List(images, contact_location, contact_name) #make a new drawing canvas of the correct size width, height = CalcPaperSize(contact_size, dpi) #dimensions in px #calculate the required size for the thumbs based on the number of images #per row. Sizes are in px and floored with maximum error of one px LEFT_PAGE_BORDER = (PageBorderLR / 25.4) * dpi RIGHT_PAGE_BORDER = (PageBorderLR / 25.4) * dpi BOTTOM_PAGE_BORDER = (PageBorderTB / 25.4) * dpi THUMB_MARGIN = (mmTHUMB_MARGIN / 25.4) * dpi FONT_SIZE = (mmFONT_SIZE / 25.4) * dpi TOP_PAGE_BORDER = (PageBorderTB / 25.4) * dpi + FONT_SIZE #include sheet .. of .. #number of rows is limited so is ThumbsPerSheet #Thumb sizes are in px, based on dpi setting if (orient == "port"): Thumb_width = ((width - LEFT_PAGE_BORDER - RIGHT_PAGE_BORDER) / num_col) - 2 * THUMB_MARGIN if (inc_filename == True): UsableRows = floor( (height - TOP_PAGE_BORDER - BOTTOM_PAGE_BORDER) / (Thumb_width + FONT_SIZE + 2 * THUMB_MARGIN)) else: UsableRows = floor( (height - TOP_PAGE_BORDER - BOTTOM_PAGE_BORDER) / (Thumb_width + 2 * THUMB_MARGIN)) else: Thumb_width = ((height - LEFT_PAGE_BORDER - RIGHT_PAGE_BORDER) / num_col) - 2 * THUMB_MARGIN if (inc_filename == True): UsableRows = floor((width - TOP_PAGE_BORDER - BOTTOM_PAGE_BORDER) / (Thumb_width + FONT_SIZE + 2 * THUMB_MARGIN)) else: UsableRows = floor((width - TOP_PAGE_BORDER - BOTTOM_PAGE_BORDER) / (Thumb_width + 2 * THUMB_MARGIN)) Thumb_height = Thumb_width #Number of chosen rows can never be bigger then usable rows if (num_rows > UsableRows): num_rows = UsableRows ThumbsPerSheet = int(num_col * num_rows) #added 'int' for python v2.6 img_no = 1 for sheetcount in range(int(ceil(num_images / float(ThumbsPerSheet)))): if (orient == "land"): sheetimg = gimp.Image(height, width, RGB) bklayer = gimp.Layer(sheetimg, "Background", height, width, RGB_IMAGE, 100, LAYER_MODE_NORMAL) else: sheetimg = gimp.Image(width, height, RGB) bklayer = gimp.Layer(sheetimg, "Background", width, height, RGB_IMAGE, 100, LAYER_MODE_NORMAL) sheetimg.disable_undo() sheetimg.add_layer(bklayer, 0) #set the image resolution sheetimg.resolution = (float(dpi), float(dpi)) #now calculate sizes; printable sheet dimensions are given #in px based on the dpi setting Canvas_width = sheetimg.width - LEFT_PAGE_BORDER - RIGHT_PAGE_BORDER Canvas_height = sheetimg.height - TOP_PAGE_BORDER - BOTTOM_PAGE_BORDER #print ("Canvas width %d height %d" % ( Canvas_width,Canvas_height)) #Log(str(sheetimg.resolution)) #now fill with white, How to fill with a other color????? bklayer.fill(FILL_WHITE) # gimp.set_background(bk_color) # gimp-bklayer-fill(bklayer, IMAGE-FILL-BG) # Log (str(bk_color)) # bklayer.fill(bk_color) # Log(str(gimp.get_background())) bklayer.flush() sheetdsp = gimp.Display(sheetimg) #print "sheet display" + str(sheetdsp) gimp.displays_flush() txtw, CalcTextHeight, txte, txtd = pdb.gimp_text_get_extents_fontname( _("Sheet %03d of %03d") % (sheetcount + 1, int(ceil(num_images / float(ThumbsPerSheet)))), FONT_SIZE, PIXELS, "Arial") ## txtfloat = pdb.gimp_text_fontname(sheetimg, sheetimg.active_layer, #only for me ## LEFT_PAGE_BORDER, TOP_PAGE_BORDER-CalcTextHeight, ## _("Sheet %03d of %03d"), ## contactsheet designed by R, Gilham (za) and E. Sullock Enzlin (nl)" ## % (sheetcount+1,int(ceil(num_images/float(ThumbsPerSheet)))), ## -1, False, FONT_SIZE, PIXELS, "Arial") txtfloat = pdb.gimp_text_fontname( sheetimg, sheetimg.active_layer, LEFT_PAGE_BORDER, TOP_PAGE_BORDER - CalcTextHeight, _("Sheet %03d of %03d") % (sheetcount + 1, int(ceil(num_images / float(ThumbsPerSheet)))), -1, False, FONT_SIZE, PIXELS, "Arial") pdb.gimp_floating_sel_anchor(txtfloat) CalcTextHeight = 0 txtw, txth, txte, txtd = (0, 0, 0, 0) if (inc_filename == True): txtw, CalcTextHeight, txte, txtd = pdb.gimp_text_get_extents_fontname( images[0]['base_name'], FONT_SIZE, PIXELS, "Arial") #print "CalcText Height %d " %(CalcTextHeight) files = images[sheetcount * ThumbsPerSheet:(sheetcount + 1) * ThumbsPerSheet] #now for each of the image files generate a thumbnail rcount = 0 ccount = 0 #generate thumb for file in files: thumbimg, x_size, y_size, valid_image = generate_thumb( file['image_file'], Thumb_width, Thumb_height) if valid_image == False: continue #next image cpy = pdb.gimp_edit_copy(thumbimg.active_layer) #center image within its minipage if (x_size > y_size): #landscape image, center vertical y_offset = (Thumb_width - y_size) / 2 x_offset = 0 else: #portrait image, center horizontal x_offset = (Thumb_height - x_size) / 2 y_offset = 0 gimp.delete(thumbimg) #now paste the new thumb into contact sheet newselect = pdb.gimp_edit_paste(sheetimg.active_layer, True) #print str(newselect) #print str(newselect.offsets) #positition in top left corner newselect.translate(-newselect.offsets[0], -newselect.offsets[1]) #now position in correct position, modified with x- and y-offset xpos = LEFT_PAGE_BORDER + ccount * ( Thumb_width + (2 * THUMB_MARGIN)) + THUMB_MARGIN + x_offset ypos = TOP_PAGE_BORDER + rcount * ( Thumb_height + (2 * THUMB_MARGIN) + CalcTextHeight) + THUMB_MARGIN + y_offset xpos = int( xpos ) #changed to int: on ubuntu type error integer expected got float. ypos = int(ypos) newselect.translate(xpos, ypos) pdb.gimp_floating_sel_anchor(newselect) if (inc_filename == True): if (inc_extension == True): ThumbName = file['base_name'] + file['extension'] else: ThumbName = file['base_name'] Size, txtwidth = CalcFontSize(ThumbName, "Arial", FONT_SIZE, CalcTextHeight, Thumb_width) #calculate text position, round the center of the image txt_xpos = xpos + (Thumb_width - txtwidth) / 2 - x_offset txt_ypos = ypos + Thumb_height + THUMB_MARGIN - y_offset txtfloat = pdb.gimp_text_fontname(sheetimg, sheetimg.active_layer, txt_xpos, txt_ypos, ThumbName, -1, False, Size, PIXELS, "Arial") pdb.gimp_floating_sel_anchor(txtfloat) ccount = ccount + 1 if (ccount >= num_col): ccount = 0 rcount = rcount + 1 gimp.displays_flush() #save contactsheet contact_filename = contact_name + "_%03d" % (sheetcount + 1) + contact_type contact_full_filename = os.path.join(contact_location, contact_filename) #print "File to save " + contact_full_filename if (contact_type == ".jpg"): save_jpeg(sheetimg, contact_full_filename, "") else: save_png(sheetimg, pdb.gimp_image_get_active_drawable(sheetimg), contact_full_filename, False) if (Print_Contactsheet == True): pdb.file_print_gtk(sheetimg) gimp.delete(sheetimg) pdb.gimp_display_delete(sheetdsp)
def layer_destroy(self): if self.layer is not None: self.image.remove_layer(self.layer) gimp.delete(self.layer) self.layer = None
def _composite_picture(self, attr, new_img, instruction): extension = os.path.splitext(instruction)[1] for prop in attr['layers']: new_layers = [] for layer in reversed(new_img.layers): if layer.name in attr['layers'][prop]: new_layer = layer.copy() new_layers.append(new_layer.name) new_img.add_layer(new_layer) #select layers for merging for layer in new_img.layers: if layer.name in new_layers: layer.visible = True else: layer.visible = False sprite = new_img.merge_visible_layers(CLIP_TO_IMAGE) #remove duplicate layer with the same name for layer in new_img.layers: if layer.name == prop: new_img.remove_layer(layer) sprite.name = prop #remove layers not selected for layer in new_img.layers: if layer.name in attr['layers']: #TODO : layer.linked = False ? layer.visible = True #pdb.gimp_image_set_active_layer(new_img, layer) #pdb.plug_in_autocrop_layer(new_img, layer) else: new_img.remove_layer(layer) new_img.crop(attr['crop_width'], attr['crop_height'], attr['x'], attr['y']) new_img.scale(attr['width'] , attr['height']) if extension not in ['.xcf', '.ora']: # resize layers for layer in new_img.layers: layer.resize_to_image_size() #algorithm to determine the good width and height of the picture #and place all the sprites #the generated picture must be a power of 2 pixel size #first, calculate the max width and height size w = new_img.width h = new_img.height max_w = w*len(new_img.layers) max_h = h*len(new_img.layers) pow2_w = pow2_h = max_pow2_w = max_pow2_h = 2 while pow2_w < w: pow2_w = pow2_w*2 while pow2_h < h: pow2_h = pow2_h*2 while max_pow2_w < max_w: max_pow2_w = max_pow2_w*2 while max_pow2_h < max_h: max_pow2_h = max_pow2_h*2 #stock all posibilities possibilities = [] while pow2_h < max_pow2_h or max_pow2_w > pow2_w: possibilities.append([max_pow2_w, pow2_h]) max_pow2_w = max_pow2_w/2 pow2_h = pow2_h*2 #keep the best best_p = possibilities[int(math.floor(len(possibilities)/2))] #resize the final picture new_img.resize(best_p[0], best_p[1]) # place all layers spritepos = '' x_index = y_index = 0 for layer in new_img.layers: index = new_img.layers.index(layer) spritepos += layer.name+': '+str(x_index*w)+' '\ +str(y_index*h)+' '\ +str(layer.width)+' '+str(layer.height)+'\n' new_img.layers[index].translate(x_index*w, y_index*h) if (x_index+2)*pow2_w > best_p[0]: x_index = 0 y_index = y_index + 1 else: x_index = x_index + 1 new_img.merge_visible_layers(CLIP_TO_IMAGE) new_img.active_layer.resize_to_image_size() create_spritepos = True if 'spritepos' in attr: if attr['spritepos'] != True: create_spritepos = False if create_spritepos: head = "# The format of the lines is\n# picture: x y width height\n\n" spritepos_file = open(os.path.splitext(instruction)[0]+'.spritepos', 'w') spritepos_file.write(head+spritepos[:len(spritepos)-2:]) pdb.gimp_file_save(new_img, new_img.active_layer, instruction, instruction) gimp.delete(new_img)
def Contact_Sheet(file_type, location, all_subdirs, inc_filename, inc_extension, contact_name, contact_type, contact_location, contact_size, dpi, orient, num_col, num_rows, PageBorderLR, PageBorderTB, mmTHUMB_MARGIN, mmFONT_SIZE, Dump_Filename_list, Sorted_Images, Print_Contactsheet): #collect 'all' images in the choosen directory and subdirs images = get_images(file_type, location, all_subdirs, Dump_Filename_list, Sorted_Images) num_images = len(images) #calculate number of images #if necessary make a txt file with image name and image directory if (Dump_Filename_list == True): Make_DirFile_List(images, contact_location, contact_name) #make a new drawing canvas of the correct size width,height = CalcPaperSize(contact_size, dpi) #dimensions in px #calculate the required size for the thumbs based on the number of images #per row. Sizes are in px and floored with maximum error of one px LEFT_PAGE_BORDER = (PageBorderLR/25.4)*dpi RIGHT_PAGE_BORDER = (PageBorderLR/25.4)*dpi BOTTOM_PAGE_BORDER = (PageBorderTB/25.4)*dpi THUMB_MARGIN = (mmTHUMB_MARGIN/25.4)*dpi FONT_SIZE = (mmFONT_SIZE/25.4)*dpi TOP_PAGE_BORDER = (PageBorderTB/25.4)*dpi + FONT_SIZE #include sheet .. of .. #number of rows is limited so is ThumbsPerSheet #Thumb sizes are in px, based on dpi setting if (orient=="port"): Thumb_width = ((width- LEFT_PAGE_BORDER - RIGHT_PAGE_BORDER)/num_col)- 2*THUMB_MARGIN if (inc_filename == True): UsableRows = floor((height- TOP_PAGE_BORDER - BOTTOM_PAGE_BORDER)/ (Thumb_width + FONT_SIZE + 2*THUMB_MARGIN)) else: UsableRows = floor((height- TOP_PAGE_BORDER - BOTTOM_PAGE_BORDER)/ (Thumb_width + 2*THUMB_MARGIN)) else: Thumb_width = ((height- LEFT_PAGE_BORDER - RIGHT_PAGE_BORDER)/num_col)- 2*THUMB_MARGIN if (inc_filename == True): UsableRows = floor((width- TOP_PAGE_BORDER - BOTTOM_PAGE_BORDER)/ (Thumb_width + FONT_SIZE + 2*THUMB_MARGIN)) else: UsableRows = floor((width- TOP_PAGE_BORDER - BOTTOM_PAGE_BORDER)/ (Thumb_width + 2*THUMB_MARGIN)) Thumb_height = Thumb_width #Number of chosen rows can never be bigger then usable rows if (num_rows > UsableRows): num_rows = UsableRows ThumbsPerSheet = int(num_col*num_rows) #added 'int' for python v2.6 img_no = 1 for sheetcount in range(int(ceil(num_images/float(ThumbsPerSheet)))): if (orient=="land"): sheetimg = gimp.Image(height,width,RGB) bklayer = gimp.Layer(sheetimg,"Background",height,width, RGB_IMAGE,100,NORMAL_MODE) else: sheetimg = gimp.Image(width,height,RGB) bklayer = gimp.Layer(sheetimg,"Background",width,height, RGB_IMAGE,100,NORMAL_MODE) sheetimg.disable_undo() sheetimg.add_layer(bklayer,0) #set the image resolution sheetimg.resolution = (float(dpi), float(dpi)) #now calculate sizes; printable sheet dimensions are given #in px based on the dpi setting Canvas_width = sheetimg.width - LEFT_PAGE_BORDER - RIGHT_PAGE_BORDER Canvas_height = sheetimg.height - TOP_PAGE_BORDER - BOTTOM_PAGE_BORDER #print ("Canvas width %d height %d" % ( Canvas_width,Canvas_height)) #Log(str(sheetimg.resolution)) #now fill with white, How to fill with a other color????? bklayer.fill(WHITE_FILL) # gimp.set_background(bk_color) # gimp-bklayer-fill(bklayer, BG-IMAGE-FILL) # Log (str(bk_color)) # bklayer.fill(bk_color) # Log(str(gimp.get_background())) bklayer.flush() sheetdsp = gimp.Display(sheetimg) #print "sheet display" + str(sheetdsp) gimp.displays_flush() txtw,CalcTextHeight,txte,txtd = pdb.gimp_text_get_extents_fontname( _("Sheet %03d of %03d") % (sheetcount+1,int(ceil(num_images/float(ThumbsPerSheet)))), FONT_SIZE,PIXELS,"Arial") ## txtfloat = pdb.gimp_text_fontname(sheetimg, sheetimg.active_layer, #only for me ## LEFT_PAGE_BORDER, TOP_PAGE_BORDER-CalcTextHeight, ## _("Sheet %03d of %03d"), ## contactsheet designed by R, Gilham (za) and E. Sullock Enzlin (nl)" ## % (sheetcount+1,int(ceil(num_images/float(ThumbsPerSheet)))), ## -1, False, FONT_SIZE, PIXELS, "Arial") txtfloat = pdb.gimp_text_fontname(sheetimg, sheetimg.active_layer, LEFT_PAGE_BORDER, TOP_PAGE_BORDER-CalcTextHeight, _("Sheet %03d of %03d") % (sheetcount+1,int(ceil(num_images/float(ThumbsPerSheet)))), -1, False, FONT_SIZE, PIXELS, "Arial") pdb.gimp_floating_sel_anchor(txtfloat) CalcTextHeight =0 txtw,txth,txte,txtd = (0,0,0,0) if (inc_filename == True): txtw,CalcTextHeight,txte,txtd = pdb.gimp_text_get_extents_fontname(images[0]['base_name'], FONT_SIZE,PIXELS,"Arial") #print "CalcText Height %d " %(CalcTextHeight) files = images[sheetcount*ThumbsPerSheet:(sheetcount+1)*ThumbsPerSheet] #now for each of the image files generate a thumbnail rcount = 0 ccount = 0 #generate thumb for file in files: thumbimg,x_size,y_size = generate_thumb(file['image_file'],Thumb_width,Thumb_height) cpy = pdb.gimp_edit_copy(thumbimg.active_layer) #center image within its minipage if (x_size>y_size): #landscape image, center vertical y_offset = (Thumb_width - y_size)/2 x_offset = 0 else: #portrait image, center horizontal x_offset = (Thumb_height - x_size)/2 y_offset = 0 gimp.delete(thumbimg) #now paste the new thumb into contact sheet newselect = pdb.gimp_edit_paste(sheetimg.active_layer,True) #print str(newselect) #print str(newselect.offsets) #positition in top left corner newselect.translate(-newselect.offsets[0],-newselect.offsets[1]) #now position in correct position, modified with x- and y-offset xpos = LEFT_PAGE_BORDER + ccount * (Thumb_width + (2 * THUMB_MARGIN))+ THUMB_MARGIN + x_offset ypos = TOP_PAGE_BORDER + rcount * (Thumb_height + (2 * THUMB_MARGIN) + CalcTextHeight) + THUMB_MARGIN + y_offset newselect.translate(xpos,ypos) pdb.gimp_floating_sel_anchor(newselect) if (inc_filename == True): if (inc_extension == True): ThumbName = file['base_name'] + file['extension'] else: ThumbName = file['base_name'] Size,txtwidth = CalcFontSize(ThumbName,"Arial",FONT_SIZE,CalcTextHeight,Thumb_width) #calculate text position, round the center of the image txt_xpos = xpos + (Thumb_width - txtwidth)/2 - x_offset txt_ypos = ypos+Thumb_height+THUMB_MARGIN-y_offset txtfloat = pdb.gimp_text_fontname(sheetimg, sheetimg.active_layer, txt_xpos, txt_ypos, ThumbName,-1, False, Size, PIXELS, "Arial") pdb.gimp_floating_sel_anchor(txtfloat) ccount = ccount + 1 if (ccount>= num_col): ccount = 0 rcount = rcount + 1 gimp.displays_flush() #save contactsheet contact_filename = contact_name + "_%03d" % (sheetcount) + contact_type contact_full_filename = os.path.join(contact_location, contact_filename) #print "File to save " + contact_full_filename if (contact_type == ".jpg"): save_jpeg(sheetimg,contact_full_filename,"") else: save_png(sheetimg,pdb.gimp_image_get_active_drawable(sheetimg),contact_full_filename,False) if (Print_Contactsheet == True): pdb.file_print_gtk(sheetimg) gimp.delete(sheetimg) pdb.gimp_display_delete(sheetdsp)
def makebuttons(self, filename=None, outdir=None, font=None, strcolor=None, transparency=False, bgcolor=None, glow=False, glowcolor=None, usepattern=False, pattern=None, buttoncolor=None, roundradius=None, padding=None, glowsize=None, bevelwidth=None, nova=False, novasparkles=None, novaradius=None, novacolor=None, writexcf=False, makeinactive=True, makeactive=True, makepressed=True, makejscript=True): # import used gimp pdb functions createtext = pdb['gimp_text_fontname'] selectionlayeralpha = pdb['gimp_selection_layer_alpha'] selectionfeather = pdb['gimp_selection_feather'] bucketfill = pdb['gimp_edit_bucket_fill'] selectionall = pdb['gimp_selection_all'] editclear = pdb['gimp_edit_clear'] rectselect = pdb['gimp_rect_select'] ellipseselect = pdb['gimp_ellipse_select'] selectionshrink = pdb['gimp_selection_shrink'] selectionnone = pdb['gimp_selection_none'] fill = pdb['gimp_edit_fill'] bumpmap = pdb['plug_in_bump_map'] novaplugin = pdb['plug_in_nova'] xcfsave = pdb['gimp_xcf_save'] gimp.progress_init() stringfile = open(filename) strings = [ line.strip() for line in stringfile.readlines() if self.toprocess(line) ] stringfile.close() t2nm = text_to_name_mapper(strings) (fontname, fontsize) = self.parsefont(font) (maxx, maxy) = self.getmaxextents(strings, fontsize, fontname) logging.debug("fontname: %s, fontsize: %d, maxx: %d, maxy: %d", fontname, int(fontsize), maxx, maxy) width = maxx + (padding * 4) height = maxy + (padding * 4) logging.debug("width: %d, height: %d", width, height) if roundradius > height / 2: roundradius = height / 2 - 1 if roundradius > width / 2: roundradius = width / 2 - 1 logging.debug("roundradius: %d", roundradius) for text in strings: image = gimp.Image(width, height, RGB) image.disable_undo() gimp.set_foreground(strcolor) textlayer = createtext(image, None, padding * 2, padding * 2, text, 0, 1, fontsize, 1, fontname) # center the text textlayer.set_offsets((image.width - textlayer.width) / 2 - 1, (image.height - textlayer.height) / 2 - 1) textlayer.lock_alpha = True textlayer.name = text if glow: texteffect = textlayer.copy(True) image.add_layer(texteffect, len(image.layers)) offs = texteffect.offsets texteffect.resize(image.width, image.height, offs[0], offs[1]) texteffect.lock_alpha = False image.active_layer = texteffect selectionlayeralpha(texteffect) selectionfeather(image, glowsize) gimp.set_foreground(glowcolor) bucketfill(texteffect, FG_BUCKET_FILL, NORMAL_MODE, 100, 0, True, 0, 0) btnlayer0 = gimp.Layer(image, "Background", width, height, RGBA_IMAGE, 100, NORMAL_MODE) image.add_layer(btnlayer0, len(image.layers)) selectionall(image) editclear(btnlayer0) offs = btnlayer0.offsets rectselect(image, offs[0] + roundradius, offs[1], btnlayer0.width - roundradius * 2, btnlayer0.height, CHANNEL_OP_REPLACE, 0, 0) rectselect(image, offs[0], offs[1] + roundradius, btnlayer0.width, btnlayer0.height - roundradius * 2, CHANNEL_OP_ADD, 0, 0) ellipseselect(image, offs[0], offs[1], roundradius * 2, roundradius * 2, CHANNEL_OP_ADD, False, 0, 0) ellipseselect(image, offs[0] + btnlayer0.width - roundradius * 2, offs[1], roundradius * 2, roundradius * 2, CHANNEL_OP_ADD, False, 0, 0) ellipseselect(image, offs[0], offs[1] + btnlayer0.height - roundradius * 2, roundradius * 2, roundradius * 2, CHANNEL_OP_ADD, False, 0, 0) ellipseselect(image, offs[0] + btnlayer0.width - roundradius * 2, offs[1] + btnlayer0.height - roundradius * 2, roundradius * 2, roundradius * 2, CHANNEL_OP_ADD, False, 0, 0) selectionshrink(image, 1) selectionfeather(image, 2) if usepattern: pdb['gimp_context_set_pattern'](pattern) bucketfill(btnlayer0, PATTERN_BUCKET_FILL, NORMAL_MODE, 100, 0, True, 0, 0) else: gimp.set_background(buttoncolor) bucketfill(btnlayer0, BG_BUCKET_FILL, NORMAL_MODE, 100, 0, True, 0, 0) selectionnone(image) selectionlayeralpha(btnlayer0) selectionfeather(image, 2) bumplayer = gimp.Layer(image, "Bumpmap", width, height, RGBA_IMAGE, 100, NORMAL_MODE) gimp.set_background(0, 0, 0) image.add_layer(bumplayer, 0) fill(bumplayer, BACKGROUND_FILL) for index in range(1, bevelwidth - 1): greyness = index * 255 / bevelwidth gimp.set_background(greyness, greyness, greyness) bucketfill(bumplayer, BG_BUCKET_FILL, NORMAL_MODE, 100, 0, False, 0, 0) selectionshrink(image, 1) gimp.set_background(255, 255, 255) bucketfill(bumplayer, BG_BUCKET_FILL, NORMAL_MODE, 100, 0, False, 0, 0) selectionnone(image) btnlayer1 = btnlayer0.copy(True) btnlayer2 = btnlayer0.copy(True) image.add_layer(btnlayer1, len(image.layers)) image.add_layer(btnlayer2, len(image.layers)) bumpmap(image, btnlayer1, bumplayer, 125, 45, 3, 0, 0, 0, 0, 0, 0, 1) bumpmap(image, btnlayer2, bumplayer, 125, 45, 3, 0, 0, 0, 0, 0, 1, 1) image.remove_layer(bumplayer) #gimp.delete(bumplayer) if nova: novalayer = gimp.Layer(image, "Nova", width, height, RGBA_IMAGE, 75, NORMAL_MODE) image.add_layer(novalayer, 0) selectionall(image) image.active_layer = novalayer editclear(novalayer) selectionnone(image) novaplugin(image, novalayer, width / 4, height / 4, novacolor, novaradius, novasparkles, 0) blackboard = gimp.Layer(image, "Blackboard", width, height, RGBA_IMAGE, 100, NORMAL_MODE) image.add_layer(blackboard, len(image.layers)) selectionall(image) if transparency: blackboard.lock_alpha = True editclear(blackboard) else: gimp.set_background(bgcolor) bucketfill(blackboard, BG_BUCKET_FILL, NORMAL_MODE, 100, 0, False, 0, 0) selectionnone(image) if writexcf: fname = t2nm.asfilename(text, 'xcf', dirname=outdir) xcfsave(0, image, textlayer, fname, fname) if makepressed: btnlayer0.visible = False btnlayer1.visible = False btnlayer2.visible = True if nova: novalayer.visible = True self.saveaspng(t2nm.asfilename(text, 'png', 'p_', outdir), image, transparency) if makeactive: btnlayer0.visible = False btnlayer1.visible = True btnlayer2.visible = False if nova: novalayer.visible = True self.saveaspng(t2nm.asfilename(text, 'png', 'a_', outdir), image, transparency) if makeinactive: btnlayer0.visible = True btnlayer1.visible = False btnlayer2.visible = False if nova: novalayer.visible = False self.saveaspng(t2nm.asfilename(text, 'png', 'i_', outdir), image, transparency) image.enable_undo() #gimp.Display(image) gimp.progress_update((strings.index(text) + 1) / len(strings)) gimp.delete(image) if makejscript: self.writejs(outdir, strings, width, height, t2nm) self.writecss(outdir, bgcolor) self.writehtml(outdir, strings, width, height, t2nm)
def makebuttons(self, filename = None, outdir = None, font = None, strcolor = None, transparency = False, bgcolor = None, glow = False, glowcolor = None, usepattern = False, pattern = None, buttoncolor = None, roundradius = None, padding = None, glowsize = None, bevelwidth = None, nova = False, novasparkles = None, novaradius = None, novacolor = None, writexcf = False, makeinactive = True, makeactive = True, makepressed = True, makejscript = True): # import used gimp pdb functions createtext = pdb['gimp_text_fontname'] selectionlayeralpha = pdb['gimp_selection_layer_alpha'] selectionfeather = pdb['gimp_selection_feather'] bucketfill = pdb['gimp_edit_bucket_fill'] selectionall = pdb['gimp_selection_all'] editclear = pdb['gimp_edit_clear'] rectselect = pdb['gimp_rect_select'] ellipseselect = pdb['gimp_ellipse_select'] selectionshrink = pdb['gimp_selection_shrink'] selectionnone = pdb['gimp_selection_none'] fill = pdb['gimp_edit_fill'] bumpmap = pdb['plug_in_bump_map'] novaplugin = pdb['plug_in_nova'] xcfsave = pdb['gimp_xcf_save'] gimp.progress_init() stringfile = open(filename) strings = [line.strip() for line in stringfile.readlines() if self.toprocess(line)] stringfile.close() t2nm = text_to_name_mapper(strings) (fontname, fontsize) = self.parsefont(font) (maxx, maxy) = self.getmaxextents(strings, fontsize, fontname) logging.debug("fontname: %s, fontsize: %d, maxx: %d, maxy: %d", fontname, int(fontsize), maxx, maxy) width = maxx + (padding*4) height = maxy + (padding*4) logging.debug("width: %d, height: %d", width, height) if roundradius > height/2: roundradius = height/2 - 1 if roundradius > width/2: roundradius = width/2 - 1 logging.debug("roundradius: %d", roundradius) for text in strings: image = gimp.Image(width, height, RGB) image.disable_undo() gimp.set_foreground(strcolor) textlayer = createtext(image, None, padding*2, padding*2, text, 0, 1, fontsize, 1, fontname) # center the text textlayer.set_offsets((image.width - textlayer.width)/2 - 1, (image.height - textlayer.height)/2 - 1) textlayer.lock_alpha = True textlayer.name = text if glow: texteffect = textlayer.copy(True) image.add_layer(texteffect, len(image.layers)) offs = texteffect.offsets texteffect.resize(image.width, image.height, offs[0], offs[1]) texteffect.lock_alpha = False image.active_layer = texteffect selectionlayeralpha(texteffect) selectionfeather(image, glowsize) gimp.set_foreground(glowcolor) bucketfill(texteffect, FG_BUCKET_FILL, NORMAL_MODE, 100, 0, True, 0, 0) btnlayer0 = gimp.Layer(image, "Background", width, height, RGBA_IMAGE, 100, NORMAL_MODE) image.add_layer(btnlayer0, len(image.layers)) selectionall(image) editclear(btnlayer0) offs = btnlayer0.offsets rectselect(image, offs[0] + roundradius, offs[1], btnlayer0.width - roundradius*2, btnlayer0.height, CHANNEL_OP_REPLACE, 0, 0) rectselect(image, offs[0], offs[1] + roundradius, btnlayer0.width, btnlayer0.height - roundradius*2, CHANNEL_OP_ADD, 0, 0) ellipseselect(image, offs[0], offs[1], roundradius*2, roundradius*2, CHANNEL_OP_ADD, False, 0, 0) ellipseselect(image, offs[0] + btnlayer0.width - roundradius*2, offs[1], roundradius*2, roundradius*2, CHANNEL_OP_ADD, False, 0, 0) ellipseselect(image, offs[0], offs[1] + btnlayer0.height - roundradius*2, roundradius*2, roundradius*2, CHANNEL_OP_ADD, False, 0, 0) ellipseselect(image, offs[0] + btnlayer0.width - roundradius*2, offs[1] + btnlayer0.height - roundradius*2, roundradius*2, roundradius*2, CHANNEL_OP_ADD, False, 0, 0) selectionshrink(image, 1) selectionfeather(image, 2) if usepattern: pdb['gimp_context_set_pattern'](pattern) bucketfill(btnlayer0, PATTERN_BUCKET_FILL, NORMAL_MODE, 100, 0, True, 0, 0) else: gimp.set_background(buttoncolor) bucketfill(btnlayer0, BG_BUCKET_FILL, NORMAL_MODE, 100, 0, True, 0, 0) selectionnone(image) selectionlayeralpha(btnlayer0) selectionfeather(image, 2) bumplayer = gimp.Layer(image, "Bumpmap", width, height, RGBA_IMAGE, 100, NORMAL_MODE) gimp.set_background(0, 0, 0) image.add_layer(bumplayer, 0) fill(bumplayer, BACKGROUND_FILL) for index in range(1, bevelwidth -1): greyness = index*255/bevelwidth; gimp.set_background(greyness, greyness, greyness) bucketfill(bumplayer, BG_BUCKET_FILL, NORMAL_MODE, 100, 0, False, 0, 0) selectionshrink(image, 1) gimp.set_background(255, 255, 255) bucketfill(bumplayer, BG_BUCKET_FILL, NORMAL_MODE, 100, 0, False, 0, 0) selectionnone(image) btnlayer1 = btnlayer0.copy(True) btnlayer2 = btnlayer0.copy(True) image.add_layer(btnlayer1, len(image.layers)) image.add_layer(btnlayer2, len(image.layers)) bumpmap(image, btnlayer1, bumplayer, 125, 45, 3, 0, 0, 0, 0, 0, 0, 1) bumpmap(image, btnlayer2, bumplayer, 125, 45, 3, 0, 0, 0, 0, 0, 1, 1) image.remove_layer(bumplayer) #gimp.delete(bumplayer) if nova: novalayer = gimp.Layer(image, "Nova", width, height, RGBA_IMAGE, 75, NORMAL_MODE) image.add_layer(novalayer, 0) selectionall(image) image.active_layer = novalayer editclear(novalayer) selectionnone(image) novaplugin(image, novalayer, width/4, height/4, novacolor, novaradius, novasparkles, 0) blackboard = gimp.Layer(image, "Blackboard", width, height, RGBA_IMAGE, 100, NORMAL_MODE) image.add_layer(blackboard, len(image.layers)) selectionall(image) if transparency: blackboard.lock_alpha = True editclear(blackboard) else: gimp.set_background(bgcolor) bucketfill(blackboard, BG_BUCKET_FILL, NORMAL_MODE, 100, 0, False, 0, 0) selectionnone(image) if writexcf: fname = t2nm.asfilename(text, 'xcf', dirname = outdir) xcfsave(0, image, textlayer, fname, fname) if makepressed: btnlayer0.visible = False btnlayer1.visible = False btnlayer2.visible = True if nova: novalayer.visible = True self.saveaspng(t2nm.asfilename(text, 'png', 'p_', outdir), image, transparency) if makeactive: btnlayer0.visible = False btnlayer1.visible = True btnlayer2.visible = False if nova: novalayer.visible = True self.saveaspng(t2nm.asfilename(text, 'png', 'a_', outdir), image, transparency) if makeinactive: btnlayer0.visible = True btnlayer1.visible = False btnlayer2.visible = False if nova: novalayer.visible = False self.saveaspng(t2nm.asfilename(text, 'png', 'i_', outdir), image, transparency) image.enable_undo() #gimp.Display(image) gimp.progress_update((strings.index(text)+1)/len(strings)) gimp.delete(image) if makejscript: self.writejs(outdir, strings, width, height, t2nm) self.writecss(outdir, bgcolor) self.writehtml(outdir, strings, width, height, t2nm)