【问题标题】:How do I change image colors with JES programing?如何使用 JES 编程更改图像颜色?
【发布时间】:2013-03-19 14:31:32
【问题描述】:

我的目标是将图片大小翻倍然后将左半部分改为灰度,然后将右上半部分的绿色值和右下半部分的蓝色值更改。我在教科书中找到了灰度值,但我不确定这是否是我实际使用的值。而且我也不确定我是否使用 for 循环或只是使用不同的东西来编程每个不同的值

到目前为止,我的代码是:

 def crazyPic(newGreen,newBlue,pic,file):
      show(pic)
      newPic = makeEmptyPicture(getWidth(pic)*2,getHeight((pic)*2
           for x in range(width):
              for y in range(height):
                  for px in getPixel(pic,0,100):
                  nRed = getRed(px) * 0.299
                  nGreen = getGreen(px) * 0.587
                  nBlue = getBlue(px) * 0.114
                  luminance = nRed + nGreen + nBlue
                  setColor(px,makeColor(luminance,luminance,luminance)

【问题讨论】:

    标签: python for-loop jython jes


    【解决方案1】:

    我不应该给出完整的答案,因为 JES 是为学生设计的应用程序,但我认为三个月后可以给出一个完整的工作示例,可以作为其他人的参考...... em>

    这应该接近你试图做的事情:

    注意:您在 x 和 y 上的简单双循环方法是正确的。

    def crazyPic(pic, newRed, newGreen, newBlue):
    
        w = getWidth(pic)
        h = getHeight(pic)
        new_w = w * 2
        new_h = h * 2
        newPic = makeEmptyPicture(w * 2, h * 2)
    
        for x in range(new_w):
          for y in range(new_h):
              new_px = getPixel(newPic, x, y)
    
              # Top-left: B&W
              if (x < w) and (y < h):
                px = getPixel(pic, x, y)
                nRed = getRed(px) * newRed #0.299
                nGreen = getGreen(px) * newGreen #0.587
                nBlue = getBlue(px) * newBlue #0.114
                luminance = nRed + nGreen + nBlue
                new_col = makeColor(luminance, luminance, luminance)
    
              # Top-right
              elif (y < h):
                px = getPixel(pic, x - w, y)
                nRed = getRed(px) * newRed
                new_col = makeColor(nRed, getGreen(px), getBlue(px))
    
              # Bottom-left
              elif (x < w):
                px = getPixel(pic, x, y - h)
                nGreen = getGreen(px) * newGreen
                new_col = makeColor(getGreen(px), nGreen, getBlue(px))
    
              # Bottom-right
              else:
                px = getPixel(pic, x - w, y - h)
                nBlue = getBlue(px) * newBlue
                new_col = makeColor(getGreen(px), getBlue(px), nBlue)
    
              setColor(new_px, new_col)
    
        return newPic
    
    file = pickAFile()
    picture = makePicture(file)
    #picture = crazyPic(picture, 0.299, 0.587, 0.114)
    # Here, with my favorite r, g, b weights
    picture = crazyPic(picture, 0.21, 0.71, 0.07)
    
    writePictureTo(picture, "/home/quartered.jpg")
    
    show(picture)
    


    输出(Antoni Tapies 的绘画):


    ......来自......


    这里有更多关于灰度detailed thread


    【讨论】:

      猜你喜欢
      • 2023-03-31
      • 2016-02-23
      • 2011-01-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-22
      相关资源
      最近更新 更多