- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
所以我在做这个学校项目(我知道真正的基本编程,而 python 是我唯一知道的语言),我需要更改我的像素颜色以对图片中的消息进行编码,但 PIL 的 putpixel 似乎没有要工作,这是我的代码。
P.S.:我所有的 PIL 信息都是自学的,英语不是我的主要语言,所以如果你能讲得简单点,我将不胜感激
from PIL import Image
e=input('file and location? ')
img=Image.open(e)
pmap=img.load()
imy=img.height
imx=img.width
if int(input('1 for encoding, 2 for decoding '))==1:
a=input('Your message? ')
for i in range(len(a)):
r , g , b=img.getpixel((i+10,imy//2))
img.putpixel((i+10,imy//2),(ord(a[i]),g,b))
r,g,b=img.getpixel((len(a)+10,imy//2))
img.putpixel((len(a)+10,imy//2),(999,g,b)) #999 is the stop code in decoding
else:
r=u=0
m=''
while r!=999:
r , g , b=img.getpixel((10+u,imy//2))
m+=chr(r)
u+=1
print(m[:len(a)-1])
img.save(e)
请记住,我不想在视觉上产生差异,而且我已经完成了调试。也没有错误,但 putpixel 出于某种原因无法正常工作。正如我所说,我是编程新手,如果其中包含愚蠢的错误,我深表歉意。
最佳答案
在使用您的代码并在图像上进行尝试后,putpixel 会按预期工作。像素的变化很难看到,这可能就是您认为它不起作用的原因。相信我,它正在发挥作用,只是您看不到它。
但是,我发现您的代码存在两个问题:
999
无法在单个像素中编码。像素的最大值为 255
(范围为 0-255)。您需要选择不同的停止代码/序列。我建议将停止代码更改为 255
。
a
从未被定义您需要通过其他方式获取消息的长度。我建议使用计数器来执行此操作:
counter = 0
while something:
counter += 1
# do something with count here
总而言之,您的代码的工作版本如下所示:
e=input('file and location? ')
img=Image.open(e)
pmap=img.load()
imy=img.height
imx=img.width
if int(input('1 for encoding, 2 for decoding '))==1:
a=input('Your message? ')
for i in range(len(a)):
r , g , b= img.getpixel((i+10,imy//2))
img.putpixel((i+10,imy//2),(ord(a[i]),g,b))
r,g,b=img.getpixel((len(a)+10,imy//2))
img.putpixel((len(a)+10,imy//2),(255,g,b)) #255 is the stop code in decoding
else:
r=u=0
m=''
message_length=0
while r!=255:
message_length+=1
r , g , b=img.getpixel((10+u,imy//2))
m+=chr(r)
u+=1
print(m[:message_length-1])
img.save(e)
关于python 的 putpixel() 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36846755/
我的目标是为每个像素生成一种颜色以填充整个 Canvas ,但是生成的图像总是变成黑色,只有一个像素改变了颜色,我似乎无法弄清楚我做错了什么。 import random from PIL impor
我想使用 Bresenham 算法画线,但有问题。我不知道,我应该在代码中使用什么来代替 putPixel() 方法: public void Line(int x0, int y0, int x1,
我是 ImageJ 初学者,我尝试使用 putPixel 但没有取得太大成功。 ImagePlus ip = IJ.getImage(); HistogramWindow histo = ne
我要消除图像的“噪声”,主要是使用排序的椒盐噪声,然后将平均值分配给坐标。结果应该是基于之前的颜色,但是我得到的变成了RED.. 这个参数有问题吗?非常感谢! from PIL import Imag
所以我在做这个学校项目(我知道真正的基本编程,而 python 是我唯一知道的语言),我需要更改我的像素颜色以对图片中的消息进行编码,但 PIL 的 putpixel 似乎没有要工作,这是我的代码。
我在 w = 134 和 h = 454 的图像 (srcImage) 上使用 putpixel。 此处的代码获取字体的一部分 r、g、b 值,即 0,255,0(这是我通过调试使用打印选项找到的)。
我正在使用代码打印像素。 // C code #include #include int main() { int gd = DETECT, gm; initgraph(&gd,
我有兴趣将一些旧的分形成像程序从 Borland C 移植到 python。在 Borland C 中,putpixel 命令会将指定的颜色像素放置在光栅化图形字段中。在 matplotlib 中是否
我想在asm中画一条彩色线。我必须在 x86 intel linux 单元上使用 AT&T 语法。我已经了解了很多,但我想知道如何进入 VGA 模式或 X 模式,以及如何在屏幕上放置像素。标准 C 库
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 7 年前。 Improve this qu
我是一名优秀的程序员,十分优秀!