如何使用Python循环每第4行中的每第4个像素? [英] How do I loop through every 4th pixel in every 4th row, using Python?

查看:185
本文介绍了如何使用Python循环每第4行中的每第4个像素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编写一个名为listenToPicture的函数,它将一张图片作为参数。它首先显示了图片。 接下来,它将遍历每第4行中的每第4个像素并执行以下操作。它将计算像素的红色,绿色和蓝色级别的总和,除以9,然后将结果添加到24.该数字将是playNote播放的音符编号。
这意味着像素越暗,音符越低;像素越亮,音符越高。它将以全音量(127)播放该音符,持续十分之一秒(100毫秒)。每次移动到新行时,它都会在控制台上打印出行号(y值)。
您的主要功能将要求用户选择带图片的文件。它将打印要播放的音符数(这是图片中像素数除以16;为什么?)。然后它将调用listenToPicture函数。

Write a function called listenToPicture that takes one picture as an argument. It first shows the picture. Next , it will loop through every 4th pixel in every 4th row and do the following. It will compute the total of the red, green and blue levels of the pixel, divide that by 9, then add the result to 24. That number will be the note number played by playNote. That means that the darker the pixel, the lower the note; the lighter the pixel, the higher the note. It will play that note at full volume (127) for a tenth of a second (100 milliseconds). Every time it moves to a new row, it prints out the row number (y value) on the console. Your main function will ask the user to select a file with a picture. It will print the number of notes to be played (which is the number of pixels in the picture divided by 16; why?). It will then call the listenToPicture function.

这是我到目前为止所做的,而且我不确定如何在每第4行设置每4个像素的循环。任何帮助都将非常感激。

Here's what I have so far, and I'm not sure how to set up looping through every 4th pixels in every 4th row. Any help will be greatly appreciate.

def main():
    pic= makePicture( pickAFile())
    printNow (getPixels(pic)/16)
    listenToPicture(pic)

def listenToPicture(pic):
    show(pic)
    w=getWidth(pic)
    h=getHeight(pic)

    for px in getPixels(pic):        
        r= getRed(px)
        g= getGreen(px)
        b= getBlue(px)
        tot= (r+g+b)/9
        playNote= tot + 24


推荐答案

步入范围会想到范围(0,len(),4)但我不知道你的 pic 的类型。

Stepped ranges come to mind range(0, len(), 4) but I don't know the type of your pic.

这篇关于如何使用Python循环每第4行中的每第4个像素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆