def _translation(output_png_path, rotation_x, rotation_y): buffered_image = _create_buffered_image( ImageIcon(output_png_path).getImage()) width, height = buffered_image.getWidth(), buffered_image.getHeight() buffered_image_matrix = [[ buffered_image.getRGB(i, j) for j in xrange(height) ] for i in xrange(width)] buffered_image_matrix = _transpose_matrix(buffered_image_matrix) m, n = len(buffered_image_matrix), len(buffered_image_matrix[0]) ''' x_coords_list, y_coords_list = [], [] for y in xrange(m): for x in xrange(n): pixel = buffered_image_matrix[y][x] if (pixel >> 24) != 0x00: x_coords_list.append(x) y_coords_list.append(y) ''' _log.info("Output path: {}".format(output_png_path)) #_log.info("height, width: ({}, {})".format(m, n)) # start_x = min(x_coords_list) if len(x_coords_list) > 0 else 0 # end_x = max(x_coords_list) if len(x_coords_list) > 0 else 0 # start_y = min(y_coords_list) if len(y_coords_list) > 0 else 0 # end_y = max(y_coords_list) if len(y_coords_list) > 0 else 0 start_x, start_y = 0, 0 end_x, end_y = n, m if start_x == 0 and end_x == 0 and start_y == 0 and end_y == 0: _log.info("ANTENNA-ERROR") return buffered_image #_log.info("start y, start x: ({}, {})".format(start_y, start_x)) #_log.info("end y, end x: ({}, {})".format(end_y, end_x)) # if start_x > rotation_x: # start_x = rotation_x # if end_x < rotation_x: # end_x = rotation_x # if start_y > rotation_y: # start_y = rotation_y # if end_y < rotation_y: # end_y = rotation_y #org_st_x = start_x #org_st_y = start_y dst_new_width = end_x dst_new_height = end_y # overlapping x enhancement if (rotation_x < end_x) and (rotation_x > 0): #_log.info("x overlap") if end_x - rotation_x > end_x / 2: dst_new_width = (end_x - rotation_x) * 2 start_x = dst_new_width / 2 - rotation_x end_x = start_x + end_x elif end_x - rotation_x < end_x / 2: end_x = 2 * rotation_x dst_new_width = start_x + end_x # non-overlapping x enhancement elif rotation_x < 0: #_log.info("2nd quadrant x rotation") start_x = 2 * abs(rotation_x) + end_x dst_new_width = 2 * (abs(rotation_x) + end_x) #_log.info("({})".format(dst_new_width)) end_x = start_x + end_x elif rotation_x >= end_x: #_log.info("huge x rotation") dst_new_width = 2 * rotation_x #+ 2*org_st_x if (rotation_y < end_y) and (rotation_y > 0): #_log.info("y overlap") if end_y - rotation_y > end_y / 2: dst_new_height = (end_y - rotation_y) * 2 start_y = dst_new_height / 2 - rotation_y end_y = start_y + end_y elif end_y - rotation_y < end_y / 2: end_y = 2 * rotation_y dst_new_height = start_y + end_y elif rotation_y < 0: #_log.info("4th quadrant y rotation") start_y = 2 * abs(rotation_y) + end_y dst_new_height = 2 * (abs(rotation_y) + end_y) end_y = start_y + end_y elif rotation_y >= end_y: #_log.info("huge y rotation") dst_new_height = 2 * rotation_y #+ 2*org_st_y #_log.info("start y, start x: ({}, {})".format(start_y, start_x)) #_log.info("end y, end x: ({}, {})".format(end_y, end_x)) #_log.info("dwidth, dheight: ({}, {})".format(dst_new_width, dst_new_height)) #_log.info("-" * 80) #new_buffered_image = BufferedImage(dst_new_width + org_st_x + 1, dst_new_height + org_st_y + 1, BufferedImage.TYPE_INT_ARGB) new_buffered_image = BufferedImage(dst_new_width + 1, dst_new_height + 1, BufferedImage.TYPE_INT_ARGB) g2d = new_buffered_image.createGraphics() g2d.setComposite(AlphaComposite.Clear) g2d.fillRect(0, 0, dst_new_width, dst_new_height) #try to assemble image for row_y in xrange(start_y, end_y + 1): for column_x in xrange(start_x, end_x + 1): if row_y - start_y < buffered_image.getHeight( ) and column_x - start_x < buffered_image.getWidth(): new_buffered_image.setRGB( column_x, row_y, buffered_image_matrix[row_y - start_y][column_x - start_x]) #rgb = java.awt.Color(255,20,147) #new_buffered_image.setRGB(dst_new_width/2, dst_new_height/2, rgb.getRGB()) return new_buffered_image
def _translation(output_png_path, rotation_x, rotation_y): buffered_image = _create_buffered_image(ImageIcon(output_png_path).getImage()) width, height = buffered_image.getWidth(), buffered_image.getHeight() buffered_image_matrix = [[buffered_image.getRGB(i, j) for j in xrange(height)] for i in xrange(width)] buffered_image_matrix = _transpose_matrix(buffered_image_matrix) m, n = len(buffered_image_matrix), len(buffered_image_matrix[0]) _log.info("Output path: {}".format(output_png_path)) start_x, start_y = 0, 0 end_x, end_y = n, m if start_x == 0 and end_x == 0 and start_y == 0 and end_y == 0: _log.info("ANTENNA-ERROR") return buffered_image dst_new_width = end_x dst_new_height = end_y # overlapping x enhancement if (rotation_x < end_x) and (rotation_x > 0): if end_x - rotation_x > end_x/2: dst_new_width = (end_x - rotation_x) * 2 start_x = dst_new_width/2 - rotation_x end_x = start_x + end_x elif end_x - rotation_x < end_x/2: end_x = 2*rotation_x dst_new_width = start_x + end_x # non-overlapping x enhancement elif rotation_x < 0: start_x = 2*abs(rotation_x) + end_x dst_new_width = 2*(abs(rotation_x) + end_x) end_x = start_x + end_x elif rotation_x >= end_x: dst_new_width = 2*rotation_x if (rotation_y < end_y) and (rotation_y > 0): if end_y - rotation_y > end_y/2: dst_new_height = (end_y - rotation_y) * 2 start_y = dst_new_height/2 - rotation_y end_y = start_y + end_y elif end_y - rotation_y < end_y/2: end_y = 2*rotation_y dst_new_height = start_y + end_y elif rotation_y < 0: start_y = 2*abs(rotation_y) + end_y dst_new_height = 2*(abs(rotation_y) + end_y) end_y = start_y + end_y elif rotation_y >= end_y: dst_new_height = 2*rotation_y new_buffered_image = BufferedImage(dst_new_width + 1, dst_new_height + 1, BufferedImage.TYPE_INT_ARGB) g2d = new_buffered_image.createGraphics() g2d.setComposite(AlphaComposite.Clear) g2d.fillRect(0, 0, dst_new_width, dst_new_height) for row_y in xrange(start_y, end_y + 1): for column_x in xrange(start_x, end_x + 1): if row_y - start_y < buffered_image.getHeight() and column_x - start_x < buffered_image.getWidth(): new_buffered_image.setRGB(column_x,row_y, buffered_image_matrix[row_y-start_y][column_x-start_x]) return new_buffered_image
def _translation(output_png_path, rotation_x, rotation_y): buffered_image = _create_buffered_image(ImageIcon(output_png_path).getImage()) width, height = buffered_image.getWidth(), buffered_image.getHeight() buffered_image_matrix = [[buffered_image.getRGB(i, j) for j in xrange(height)] for i in xrange(width)] buffered_image_matrix = _transpose_matrix(buffered_image_matrix) m, n = len(buffered_image_matrix), len(buffered_image_matrix[0]) _log.info("Output path: {}".format(output_png_path)) start_x, start_y = 0, 0 end_x, end_y = n, m if start_x == 0 and end_x == 0 and start_y == 0 and end_y == 0: _log.info("ANTENNA-ERROR") return buffered_image dst_new_width = end_x dst_new_height = end_y # overlapping x enhancement if (rotation_x < end_x) and (rotation_x > 0): if end_x - rotation_x > end_x/2: dst_new_width = (end_x - rotation_x) * 2 start_x = dst_new_width/2 - rotation_x end_x = start_x + end_x elif end_x - rotation_x < end_x/2: end_x = 2*rotation_x dst_new_width = start_x + end_x # non-overlapping x enhancement elif rotation_x < 0: start_x = 2*abs(rotation_x) + end_x dst_new_width = 2*(abs(rotation_x) + end_x) end_x = start_x + end_x elif rotation_x >= end_x: dst_new_width = 2*rotation_x if (rotation_y < end_y) and (rotation_y > 0): if end_y - rotation_y > end_y/2: dst_new_height = (end_y - rotation_y) * 2 start_y = dst_new_height/2 - rotation_y end_y = start_y + end_y elif end_y - rotation_y < end_y/2: end_y = 2*rotation_y dst_new_height = start_y + end_y elif rotation_y < 0: start_y = 2*abs(rotation_y) + end_y dst_new_height = 2*(abs(rotation_y) + end_y) end_y = start_y + end_y elif rotation_y >= end_y: dst_new_height = 2*rotation_y new_buffered_image = BufferedImage(int(dst_new_width + 1), int(dst_new_height + 1), BufferedImage.TYPE_INT_ARGB) g2d = new_buffered_image.createGraphics() g2d.setComposite(AlphaComposite.Clear) g2d.fillRect(0, 0, int(dst_new_width + 1), int(dst_new_height + 1)) start_x = int(start_x) start_y = int(start_y) end_x = int(end_x) end_y = int(end_y) for row_y in xrange(start_y, end_y + 1): for column_x in xrange(start_x, end_x + 1): if row_y - start_y < buffered_image.getHeight() and column_x - start_x < buffered_image.getWidth(): new_buffered_image.setRGB(column_x,row_y, buffered_image_matrix[row_y-start_y][column_x-start_x]) return new_buffered_image
class Image: """Holds an image of RGB pixels accessed by column and row indices (col, row). Origin (0, 0) is at upper left.""" # QUESTION: For efficiency, should we also extract and save self.pixels (at image reading time)? # Also make setPixel(), getPixel(), setPixels() and getPixels() work on/with self.pixels. # And when writing, use code in current setPixels() to update image buffer, before writing it out? # This is something to try. def __init__(self, filename, width=None, height=None): """Create an image from a file, or an empty (black) image with specified dimensions.""" # Since Python does not allow constructors with different signatures, # the trick is to reuse the first argument as a filename or a width. # If it is a string, we assume they want is to open a file. # If it is an int, we assume they want us to create a blank image. if type(filename) == type(""): # is it a string? self.filename = filename # treat is a filename self.image = BufferedImage( 1, 1, BufferedImage.TYPE_INT_RGB) # create a dummy image self.read(filename) # and read external image into ti elif type(filename) == type(1): # is it a int? # create blank image with specified dimensions self.filename = "Untitled" self.width = filename # holds image width (shift arguments) self.height = width # holds image height self.image = BufferedImage( self.width, self.height, BufferedImage.TYPE_INT_RGB) # holds image buffer (pixels) else: raise TypeError( "Image(): first argument must a filename (string) or an blank image width (int)." ) # display image self.display = JFrame() # create frame window to hold image icon = ImageIcon( self.image) # wrap image appropriately for displaying in a frame container = JLabel(icon) self.display.setContentPane(container) # and place it self.display.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE) self.display.setTitle(self.filename) self.display.setResizable(False) self.display.pack() self.display.setVisible(True) # remember that this image has been created and is active (so that it can be stopped/terminated by JEM, if desired) _ActiveImages_.append(self) def getWidth(self): """Returns the width of the image.""" return self.width def getHeight(self): """Returns the height of the image.""" return self.height def getPixel(self, col, row): """Returns a list of the RGB values for this pixel, e.g., [255, 0, 0].""" # Obsolete - convert the row so that row zero refers to the bottom row of pixels. #row = self.height - row - 1 color = Color(self.image.getRGB(col, row)) # get pixel's color return [color.getRed(), color.getGreen(), color.getBlue()] # create list of RGB values (0-255) def setPixel(self, col, row, RGBlist): """Sets this pixel's RGB values, e.g., [255, 0, 0].""" # Obsolete - convert the row so that row zero refers to the bottom row of pixels. #row = self.height - row - 1 color = Color(RGBlist[0], RGBlist[1], RGBlist[2]) # create color from RGB values self.image.setRGB(col, row, color.getRGB()) def getPixels(self): """Returns a 2D list of pixels (col, row) - each pixel is a list of RGB values, e.g., [255, 0, 0].""" pixels = [] # initialize list of pixels #for row in range(self.height-1, 0, -1): # load pixels from image for row in range(0, self.height): # load pixels from image pixels.append([]) # add another empty row for col in range(self.width): # populate row with pixels # RGBlist = self.getPixel(col, row) # this works also (but slower) color = Color(self.image.getRGB(col, row)) # get pixel's color RGBlist = [color.getRed(), color.getGreen(), color.getBlue() ] # create list of RGB values (0-255) pixels[-1].append( RGBlist) # add a pixel as (R, G, B) values (0-255, each) # now, 2D list of pixels has been created, so return it return pixels def setPixels(self, pixels): """Sets image to the provided 2D list of pixels (col, row) - each pixel is a list of RGB values, e.g., [255, 0, 0].""" self.height = len(pixels) # get number of rows self.width = len( pixels[0] ) # get number of columns (assume all columns have same length #for row in range(self.height-1, 0, -1): # iterate through all rows for row in range(0, self.height): # iterate through all rows for col in range( self.width): # iterate through every column on this row RGBlist = pixels[row][col] #self.setPixel(col, row, RGBlist) # this works also (but slower) color = Color(RGBlist[0], RGBlist[1], RGBlist[2]) # create color from RGB values self.image.setRGB(col, row, color.getRGB()) def read(self, filename): """Read an image from a .png, .gif, or .jpg file. as 2D list of RGB pixels.""" # JEM working directory fix (see above) filename = fixWorkingDirForJEM(filename) # does nothing if not in JEM # *** #print "fixWorkingDirForJEM( filename ) =", filename file = File(filename) # read file from current directory self.image = ImageIO.read(file) self.width = self.image.getWidth(None) self.height = self.image.getHeight(None) pixels = [] # initialize list of pixels # load pixels from image for row in range(self.height): pixels.append([]) # add another empty row for col in range(self.width): # now, populate row with pixels color = Color(self.image.getRGB(col, row)) # get pixel's color RGBlist = [color.getRed(), color.getGreen(), color.getBlue() ] # create list of RGB values (0-255) pixels[-1].append( RGBlist) # add a pixel as (R, G, B) values (0-255, each) # now, pixels have been loaded from image file, so create an image self.setPixels(pixels) def write(self, filename): """Saves the pixels to a file (.png or .jpg).""" # JEM working directory fix (see above) filename = fixWorkingDirForJEM(filename) # does nothing if not in JEM # *** #print "fixWorkingDirForJEM( filename ) =", filename # get suffix suffix = filename[-3:] suffix = suffix.lower() if suffix == "jpg" or suffix == "png": ImageIO.write(self.image, suffix, File(filename)) # save, and also self.filename = filename # update image filename self.display.setTitle(self.filename) # update display title else: print "Filename must end in .jpg or .png" def show(self): """It displays the image.""" self.display.setVisible(True) #self.display.repaint() # draw it def hide(self): """It hides the image.""" self.display.setVisible(False)
class Image: """Holds an image of RGB pixels accessed by column and row indices (col, row). Origin (0, 0) is at upper left.""" # QUESTION: For efficiency, should we also extract and save self.pixels (at image reading time)? # Also make setPixel(), getPixel(), setPixels() and getPixels() work on/with self.pixels. # And when writing, use code in current setPixels() to update image buffer, before writing it out? # This is something to try. def __init__(self, filename, width=None, height=None): """Create an image from a file, or an empty (black) image with specified dimensions.""" # Since Python does not allow constructors with different signatures, # the trick is to reuse the first argument as a filename or a width. # If it is a string, we assume they want is to open a file. # If it is an int, we assume they want us to create a blank image. if type(filename) == type(""): # is it a string? self.filename = filename # treat is a filename self.image = BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB) # create a dummy image self.read(filename) # and read external image into ti elif type(filename) == type(1): # is it a int? # create blank image with specified dimensions self.filename = "Untitled" self.width = filename # holds image width (shift arguments) self.height = width # holds image height self.image = BufferedImage(self.width, self.height, BufferedImage.TYPE_INT_RGB) # holds image buffer (pixels) else: raise TypeError("Image(): first argument must a filename (string) or an blank image width (int).") # display image self.display = JFrame() # create frame window to hold image icon = ImageIcon(self.image) # wrap image appropriately for displaying in a frame container = JLabel(icon) self.display.setContentPane(container) # and place it self.display.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE) self.display.setTitle(self.filename) self.display.setResizable(False) self.display.pack() self.display.setVisible(True) def getWidth(self): """Returns the width of the image.""" return self.width def getHeight(self): """Returns the height of the image.""" return self.height def getPixel(self, col, row): """Returns a list of the RGB values for this pixel, e.g., [255, 0, 0].""" # Obsolete - convert the row so that row zero refers to the bottom row of pixels. #row = self.height - row - 1 color = Color(self.image.getRGB(col, row)) # get pixel's color return [color.getRed(), color.getGreen(), color.getBlue()] # create list of RGB values (0-255) def setPixel(self, col, row, RGBlist): """Sets this pixel's RGB values, e.g., [255, 0, 0].""" # Obsolete - convert the row so that row zero refers to the bottom row of pixels. #row = self.height - row - 1 color = Color(RGBlist[0], RGBlist[1], RGBlist[2]) # create color from RGB values self.image.setRGB(col, row, color.getRGB()) def getPixels(self): """Returns a 2D list of pixels (col, row) - each pixel is a list of RGB values, e.g., [255, 0, 0].""" pixels = [] # initialize list of pixels #for row in range(self.height-1, 0, -1): # load pixels from image for row in range(0, self.height): # load pixels from image pixels.append( [] ) # add another empty row for col in range(self.width): # populate row with pixels # RGBlist = self.getPixel(col, row) # this works also (but slower) color = Color(self.image.getRGB(col, row)) # get pixel's color RGBlist = [color.getRed(), color.getGreen(), color.getBlue()] # create list of RGB values (0-255) pixels[-1].append( RGBlist ) # add a pixel as (R, G, B) values (0-255, each) # now, 2D list of pixels has been created, so return it return pixels def setPixels(self, pixels): """Sets image to the provided 2D list of pixels (col, row) - each pixel is a list of RGB values, e.g., [255, 0, 0].""" self.height = len(pixels) # get number of rows self.width = len(pixels[0]) # get number of columns (assume all columns have same length #for row in range(self.height-1, 0, -1): # iterate through all rows for row in range(0, self.height): # iterate through all rows for col in range(self.width): # iterate through every column on this row RGBlist = pixels[row][col] #self.setPixel(col, row, RGBlist) # this works also (but slower) color = Color(RGBlist[0], RGBlist[1], RGBlist[2]) # create color from RGB values self.image.setRGB(col, row, color.getRGB()) def read(self, filename): """Read an image from a .png, .gif, or .jpg file. as 2D list of RGB pixels.""" # JEM working directory fix (see above) filename = fixWorkingDirForJEM( filename ) # does nothing if not in JEM # *** #print "fixWorkingDirForJEM( filename ) =", filename file = File(filename) # read file from current directory self.image = ImageIO.read(file) self.width = self.image.getWidth(None) self.height = self.image.getHeight(None) pixels = [] # initialize list of pixels # load pixels from image for row in range(self.height): pixels.append( [] ) # add another empty row for col in range(self.width): # now, populate row with pixels color = Color(self.image.getRGB(col, row)) # get pixel's color RGBlist = [color.getRed(), color.getGreen(), color.getBlue()] # create list of RGB values (0-255) pixels[-1].append( RGBlist ) # add a pixel as (R, G, B) values (0-255, each) # now, pixels have been loaded from image file, so create an image self.setPixels(pixels) def write(self, filename): """Saves the pixels to a file (.png or .jpg).""" # JEM working directory fix (see above) filename = fixWorkingDirForJEM( filename ) # does nothing if not in JEM # *** #print "fixWorkingDirForJEM( filename ) =", filename # get suffix suffix = filename[-3:] suffix = suffix.lower() if suffix == "jpg" or suffix =="png": ImageIO.write(self.image, suffix, File(filename)) # save, and also self.filename = filename # update image filename self.display.setTitle(self.filename) # update display title else: print "Filename must end in .jpg or .png" def show(self): """It displays the image.""" self.display.setVisible(True) #self.display.repaint() # draw it def hide(self): """It hides the image.""" self.display.setVisible(False)
def _translation(output_png_path, rotation_x, rotation_y): buffered_image = _create_buffered_image(ImageIcon(output_png_path).getImage()) width, height = buffered_image.getWidth(), buffered_image.getHeight() buffered_image_matrix = [[buffered_image.getRGB(i, j) for j in xrange(height)] for i in xrange(width)] buffered_image_matrix = _transpose_matrix(buffered_image_matrix) m, n = len(buffered_image_matrix), len(buffered_image_matrix[0]) ''' x_coords_list, y_coords_list = [], [] for y in xrange(m): for x in xrange(n): pixel = buffered_image_matrix[y][x] if (pixel >> 24) != 0x00: x_coords_list.append(x) y_coords_list.append(y) ''' _log.info("Output path: {}".format(output_png_path)) #_log.info("height, width: ({}, {})".format(m, n)) # start_x = min(x_coords_list) if len(x_coords_list) > 0 else 0 # end_x = max(x_coords_list) if len(x_coords_list) > 0 else 0 # start_y = min(y_coords_list) if len(y_coords_list) > 0 else 0 # end_y = max(y_coords_list) if len(y_coords_list) > 0 else 0 start_x, start_y = 0, 0 end_x, end_y = n, m if start_x == 0 and end_x == 0 and start_y == 0 and end_y == 0: _log.info("ANTENNA-ERROR") return buffered_image #_log.info("start y, start x: ({}, {})".format(start_y, start_x)) #_log.info("end y, end x: ({}, {})".format(end_y, end_x)) # if start_x > rotation_x: # start_x = rotation_x # if end_x < rotation_x: # end_x = rotation_x # if start_y > rotation_y: # start_y = rotation_y # if end_y < rotation_y: # end_y = rotation_y #org_st_x = start_x #org_st_y = start_y dst_new_width = end_x dst_new_height = end_y # overlapping x enhancement if (rotation_x < end_x) and (rotation_x > 0): #_log.info("x overlap") if end_x - rotation_x > end_x/2: dst_new_width = (end_x - rotation_x) * 2 start_x = dst_new_width/2 - rotation_x end_x = start_x + end_x elif end_x - rotation_x < end_x/2: end_x = 2*rotation_x dst_new_width = start_x + end_x # non-overlapping x enhancement elif rotation_x < 0: #_log.info("2nd quadrant x rotation") start_x = 2*abs(rotation_x) + end_x dst_new_width = 2*(abs(rotation_x) + end_x) #_log.info("({})".format(dst_new_width)) end_x = start_x + end_x elif rotation_x >= end_x: #_log.info("huge x rotation") dst_new_width = 2*rotation_x #+ 2*org_st_x if (rotation_y < end_y) and (rotation_y > 0): #_log.info("y overlap") if end_y - rotation_y > end_y/2: dst_new_height = (end_y - rotation_y) * 2 start_y = dst_new_height/2 - rotation_y end_y = start_y + end_y elif end_y - rotation_y < end_y/2: end_y = 2*rotation_y dst_new_height = start_y + end_y elif rotation_y < 0: #_log.info("4th quadrant y rotation") start_y = 2*abs(rotation_y) + end_y dst_new_height = 2*(abs(rotation_y) + end_y) end_y = start_y + end_y elif rotation_y >= end_y: #_log.info("huge y rotation") dst_new_height = 2*rotation_y #+ 2*org_st_y #_log.info("start y, start x: ({}, {})".format(start_y, start_x)) #_log.info("end y, end x: ({}, {})".format(end_y, end_x)) #_log.info("dwidth, dheight: ({}, {})".format(dst_new_width, dst_new_height)) #_log.info("-" * 80) #new_buffered_image = BufferedImage(dst_new_width + org_st_x + 1, dst_new_height + org_st_y + 1, BufferedImage.TYPE_INT_ARGB) new_buffered_image = BufferedImage(dst_new_width + 1, dst_new_height + 1, BufferedImage.TYPE_INT_ARGB) g2d = new_buffered_image.createGraphics() g2d.setComposite(AlphaComposite.Clear) g2d.fillRect(0, 0, dst_new_width, dst_new_height) #try to assemble image for row_y in xrange(start_y, end_y + 1): for column_x in xrange(start_x, end_x + 1): if row_y - start_y < buffered_image.getHeight() and column_x - start_x < buffered_image.getWidth(): new_buffered_image.setRGB(column_x,row_y, buffered_image_matrix[row_y-start_y][column_x-start_x]) #rgb = java.awt.Color(255,20,147) #new_buffered_image.setRGB(dst_new_width/2, dst_new_height/2, rgb.getRGB()) return new_buffered_image
class FunctionRepresentation(core.DataViewComponent): def __init__(self,view,name,config,func,args=(),label=None): core.DataViewComponent.__init__(self,label) self.view=view self.name=name self.func=func self.data=self.view.watcher.watch(name,func,args=args) self.border_top=20 self.border_left=20 self.border_right=20 self.border_bottom=20 self.config=config self.start = 0 # self.setSize(440,440+self.label_offset) self.setSize(240,240+self.label_offset) self.max = 0.0001 self.min = -0.0001 self.grid_size = 50 self.image = None self.counter = 0 ###################################### ## initialize function input grid grid_size = self.grid_size # construct input grid row = array([map(float,range(grid_size))]) row = row / (self.grid_size-1) * 2 - 2 # normalized to [-1, 1] gridX = dot(ones([grid_size,1]), row) gridY = transpose(gridX) # get function value x1 = reshape(gridX,[1, grid_size*grid_size]) x2 = reshape(gridY,[1, grid_size*grid_size]) self.func_input = concatenate([x1, x2], 0) def paintComponent(self,g): f,dimension,minx,maxx,miny,maxy, params, color, time_step = self.config core.DataViewComponent.paintComponent(self,g) width=self.size.width-self.border_left-self.border_right height=self.size.height-self.border_top-self.border_bottom-self.label_offset if width<2: return dt_tau=None if self.view.tau_filter>0: dt_tau=self.view.dt/self.view.tau_filter try: data=self.data.get(start=self.view.current_tick,count=1,dt_tau=dt_tau)[0] except: return if dimension == 2: if self.image is None : self.image = BI(width, height, BI.TYPE_INT_ARGB) if self.counter != time_step: self.counter += 1 g.drawImage( self.image, self.border_left, self.border_top, None) else: self.counter = 0 # currently only for fixed grid grid_size = self.grid_size # step_size = width / grid_size coeffs=transpose(array([data])) basis = array(f(self.func_input, params)) value = transpose(dot(transpose(basis),coeffs)) maxv = max(value[0]) minv = min(value[0]) if maxv > self.max: self.max = maxv if minv < self.min: self.min = minv pvalue = (value - self.min) / (self.max - self.min) # normalized pixel value if color == 'g': ## gray pvalue = array(map(int, array(pvalue[0]) * 0xFF)) pvalue = pvalue * 0x10000 + pvalue * 0x100 + pvalue elif color == 'c': ##color pvalue = map(int, array(pvalue[0]) * 0xFF * 2) R = zeros(len(pvalue)) G = zeros(len(pvalue)) B = zeros(len(pvalue)) for i, v in enumerate(pvalue): if v < 0xFF: B[i] = 0xFF - v G[i] = v else: G[i] = 2*0xFF - v R[i] = v - 0xFF pvalue = R * 0x10000 + G * 0x100 + B pvalue = reshape(pvalue,[grid_size, grid_size]) rvalue = 0xFF000000 + pvalue # expand pixel value from grid to raster size # ratio = float(width) / grid_size # indeces = map(int, (floor(array(range(width)) / ratio))) ## Tooooooo slow here! # for i, ii in enumerate(indeces): # for j, jj in enumerate(indeces) : # rvalue[i,j] = pvalue[ii,jj] for zoom in range(2): zgrid_size = grid_size * (zoom + 1) rvalue = reshape(rvalue, [zgrid_size * zgrid_size, 1]) rvalue = concatenate([rvalue, rvalue], 1) rvalue = reshape(rvalue, [zgrid_size, zgrid_size* 2]) rvalue = repeat(rvalue, ones(zgrid_size) * 2) # draw image rvalue = reshape(rvalue, [1, width * height]) self.image.setRGB(0, 0, width, height, rvalue[0], 0, width) g.drawImage( self.image, self.border_left, self.border_top, None) elif dimension == 1: g.color=Color(0.8,0.8,0.8) g.drawRect(self.border_left,self.border_top+self.label_offset,width,height) g.color=Color.black txt='%4g'%maxx bounds=g.font.getStringBounds(txt,g.fontRenderContext) g.drawString(txt,self.size.width-self.border_right-bounds.width/2,self.size.height-self.border_bottom+bounds.height) txt='%4g'%minx bounds=g.font.getStringBounds(txt,g.fontRenderContext) g.drawString(txt,self.border_left-bounds.width/2,self.size.height-self.border_bottom+bounds.height) g.drawString('%6g'%maxy,0,10+self.border_top+self.label_offset) g.drawString('%6g'%miny,0,self.size.height-self.border_bottom) g.color=Color.black pdftemplate=getattr(self.view.area,'pdftemplate',None) if pdftemplate is not None: pdf,scale=pdftemplate pdf.setLineWidth(0.5) steps=100 dx=float(maxx-minx)/(width*steps) for i in range(width*steps): x=minx+i*dx value=sum([f(j,x)*d for j,d in enumerate(data)]) y=float((value-miny)*height/(maxy-miny)) xx=self.border_left+i/float(steps) yy=self.height-self.border_bottom-y if i==0: pdf.moveTo((self.x+xx)*scale,800-(self.y+yy)*scale) else: if 0<y<height: pdf.lineTo((self.x+xx)*scale,800-(self.y+yy)*scale) pdf.setRGBColorStroke(g.color.red,g.color.green,g.color.blue) pdf.stroke() else: dx=float(maxx-minx)/(width-1) px,py=None,None for i in range(width): x=minx+i*dx value=sum([f(j,x)*d for j,d in enumerate(data)]) y=int((value-miny)*height/(maxy-miny)) xx=self.border_left+i yy=self.height-self.border_bottom-y if px is not None and miny<value<maxy: g.drawLine(px,py,xx,yy) px,py=xx,yy
def _translation(output_png_path, rotation_x, rotation_y): buffered_image = _create_buffered_image(ImageIcon(output_png_path).getImage()) #buffered_image = ImageIO.read(new File()); width, height = buffered_image.getWidth(), buffered_image.getHeight() buffered_image_matrix = [[buffered_image.getRGB(i, j) for j in xrange(height)] for i in xrange(width)] buffered_image_matrix = _transpose_matrix(buffered_image_matrix) x_coords_list, y_coords_list = [], [] m, n = len(buffered_image_matrix), len(buffered_image_matrix[0]) for y in xrange(m): for x in xrange(n): pixel = buffered_image_matrix[y][x] if (pixel >> 24) != 0x00: x_coords_list.append(x) y_coords_list.append(y) _log.info("-" * 80) _log.info("Output path: {}".format(output_png_path)) _log.info("height, weight: ({}, {})".format(m, n)) start_x = min(x_coords_list) if len(x_coords_list) > 0 else 0 end_x = max(x_coords_list) if len(x_coords_list) > 0 else 0 start_y = min(y_coords_list) if len(y_coords_list) > 0 else 0 end_y = max(y_coords_list) if len(y_coords_list) > 0 else 0 _log.info("end y, end x: ({}, {})".format(end_y, end_x)) if start_x > rotation_x: start_x = rotation_x if end_x < rotation_x: end_x = rotation_x if start_y > rotation_y: start_y = rotation_y if end_y < rotation_y: end_y = rotation_y _log.info("end y, end x: ({}, {})".format(end_y, end_x)) _log.info("-" * 80) dst_new_width = end_x * 2 dst_new_height = end_y * 2 new_buffered_image = BufferedImage(dst_new_width, dst_new_height,BufferedImage.TYPE_INT_ARGB) g2d = new_buffered_image.createGraphics() g2d.setComposite(AlphaComposite.Clear) g2d.fillRect(0, 0, dst_new_width, dst_new_height) # Bottom Right for old_row_y, new_row_y in zip(xrange(rotation_y, end_y + 1),xrange(end_y, dst_new_height)): for old_column_x, new_column_x in zip(xrange(rotation_x, end_x + 1),xrange(end_x, dst_new_width)): if(old_row_y >= 0 and old_column_x >= 0 and old_row_y < buffered_image_matrix.length and old_column_x < buffered_image_matrix[old_row_y].length): new_buffered_image.setRGB(new_column_x,new_row_y, buffered_image_matrix[old_row_y][old_column_x]); # Upper Right for old_row_y, new_row_y in zip(xrange(rotation_y, start_y - 1, -1),xrange(end_y, -1, -1)): for old_column_x, new_column_x in zip(xrange(rotation_x, end_x + 1),xrange(end_x, dst_new_width)): if(old_row_y >= 0 and old_column_x >= 0 and old_row_y < buffered_image_matrix.length and old_column_x < buffered_image_matrix[old_row_y].length): new_buffered_image.setRGB(new_column_x,new_row_y, buffered_image_matrix[old_row_y][old_column_x]) # Upper Left for old_row_y, new_row_y in zip(xrange(rotation_y, start_y - 1, -1),xrange(end_y, -1, -1)): for old_column_x, new_column_x in zip(xrange(rotation_x, start_x - 1, -1),xrange(end_x, -1, -1)): if(old_row_y >= 0 and old_column_x >= 0 and old_row_y < buffered_image_matrix.length and old_column_x < buffered_image_matrix[old_row_y].length): new_buffered_image.setRGB(new_column_x,new_row_y, buffered_image_matrix[old_row_y][old_column_x]) # Bottom Left for old_row_y, new_row_y in zip(xrange(rotation_y, end_y + 1),xrange(end_y, dst_new_height)): for old_column_x, new_column_x in zip(xrange(rotation_x, start_x - 1, -1),xrange(end_x, -1, -1)): if(old_row_y >= 0 and old_column_x >= 0 and old_row_y < buffered_image_matrix.length and old_column_x < buffered_image_matrix[old_row_y].length): new_buffered_image.setRGB(new_column_x,new_row_y, buffered_image_matrix[old_row_y][old_column_x]) #color = Color.yellow #new_buffered_image.setRGB(end_x - 1, end_y - 1, color.getRGB()) return new_buffered_image