当前位置: 首页>>代码示例>>Python>>正文


Python BufferedImage.setRGB方法代码示例

本文整理汇总了Python中java.awt.image.BufferedImage.setRGB方法的典型用法代码示例。如果您正苦于以下问题:Python BufferedImage.setRGB方法的具体用法?Python BufferedImage.setRGB怎么用?Python BufferedImage.setRGB使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在java.awt.image.BufferedImage的用法示例。


在下文中一共展示了BufferedImage.setRGB方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: _translation

# 需要导入模块: from java.awt.image import BufferedImage [as 别名]
# 或者: from java.awt.image.BufferedImage import setRGB [as 别名]
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
开发者ID:Catrobat,项目名称:ScratchToCatrobat,代码行数:67,代码来源:svgtopng.py

示例2: indices

# 需要导入模块: from java.awt.image import BufferedImage [as 别名]
# 或者: from java.awt.image.BufferedImage import setRGB [as 别名]
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
         
#.........这里部分代码省略.........
开发者ID:HenryStevens,项目名称:jes,代码行数:103,代码来源:image.py

示例3: FunctionRepresentation

# 需要导入模块: from java.awt.image import BufferedImage [as 别名]
# 或者: from java.awt.image.BufferedImage import setRGB [as 别名]

#.........这里部分代码省略.........
                
                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
开发者ID:hunse,项目名称:nengo_1.4,代码行数:104,代码来源:funcrep.py

示例4: _translation

# 需要导入模块: from java.awt.image import BufferedImage [as 别名]
# 或者: from java.awt.image.BufferedImage import setRGB [as 别名]
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
开发者ID:ReinhardHuetter,项目名称:ScratchToCatrobat,代码行数:78,代码来源:svgtopng.py


注:本文中的java.awt.image.BufferedImage.setRGB方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。