【问题标题】:To change pixel's color and verify it改变像素的颜色并验证它
【发布时间】:2021-11-29 07:17:28
【问题描述】:

我需要更改像素的颜色并且必须对其进行验证。 为什么更改没有生效?

第一种方式提供输出:

Current color of (25,45) pixel is : (134, 105, 125)
New color of (25,45) pixel is : (64, 128, 196)
Current color of (25,45) pixel of new image is : (130, 107, 125)

第二种方式提供输出:

Red: 134, Green: 105, Blue: 125  #initial values
Red: 64, Green: 128, Blue: 196   #Setted values
Red: 130, Green: 107, Blue: 125  #Verified (incorrect) values
from PIL import Image
### 1st way
img1 = Image.open("randomImage01.jpg")
obj = img1.load()
print("Current color of (25,45) pixel is : " + str(obj[25, 45]))   #Getting current color
obj[25, 45] = (64, 128, 196)                                       #Changing pixel's color
print( "New color of (25,45) pixel is : " + str(obj[25, 45]))
img1.save("randomImage02.jpg")                                     #Saving
img2 = Image.open("randomImage02.jpg")                             #Verification
obj = img2.load()
print( "Current color of (25,45) pixel of new image is : " + str(obj[25, 45]))

### 2nd way

picture1 = Image.open("randomImage01.jpg")                          #Getting current color
r,g,b = picture1.getpixel((25,45))
print("Red: {0}, Green: {1}, Blue: {2}".format(r,g,b))
picture1.putpixel((25,45),(64,128,196))                            #Changing pixel's color
r1,g1,b1 = picture1.getpixel((25,45))
print("Red: {0}, Green: {1}, Blue: {2}".format(r1,g1,b1))
picture1.save("randomImage02.jpg")                                 #Saving
picture2 = Image.open("randomImage02.jpg")                         #Verification
r3,g3,b3 = picture2.getpixel( (25,45) )
print("Red: {0}, Green: {1}, Blue: {2}".format(r3,g3,b3))

【问题讨论】:

    标签: python image image-processing python-imaging-library


    【解决方案1】:

    JPEG 不是无损编解码器。使用 PNG 文件格式。

    from PIL import Image
    
    ### 2nd way
    
    picture1 = Image.open("randomImage01.jpg")                          #Getting current color
    r,g,b = picture1.getpixel((0,2))
    print("Red: {0}, Green: {1}, Blue: {2}".format(r,g,b))
    picture1.putpixel((0,2),(64,128,196))                            #Changing pixel's color
    r1,g1,b1 = picture1.getpixel((0,2))
    print("Red: {0}, Green: {1}, Blue: {2}".format(r1,g1,b1))
    picture1.save("randomImage02.png")                                 #Saving
    picture2 = Image.open("randomImage02.png")                         #Verification
    r3,g3,b3 = picture2.getpixel( (0,2) )
    print("Red: {0}, Green: {1}, Blue: {2}".format(r3,g3,b3))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多