问题
I'm writing a code in Jython, that will copy part of one picture into an empty picture, but I want it to copy (let's say) 10 pixels less with each next row. I don't think I make sense, let me explain with an example. A picture 100 pixels by 100 pixels, the program will copy the first row (100pixels) of pixels into the new picture, but for second row of pixels I want it to copy only 90 pixels, then for the third row 80 pixels, and so on.
Here I have a code that will copy part of a picture, but it copies a square. So what should I add to make it do what I want. I'm guessing I'm supposed to do something with the for x in range
but I don't know what.
def copyPic():
file=pickAFile()
oldPic=makePicture(file)
newPic=makeEmptyPicture(getWidth(oldPic),getHeight(oldPic))
xstart=getWidth(oldPic)/2
ystart=getHeight(oldPic)/2
for y in range(ystart,getHeight(oldPic)):
for x in range(xstart, (getWidth(oldPic))):
oldPixel=getPixel(oldPic,x,y)
colour=getColor(oldPixel)
newPixel=getPixel(newPic,x,y)
setColor(newPixel,colour)
explore(newPic)
回答1:
Your code definitely looks like it will copy the bottom right 1/4 of the image... to make a triangle-shaped piece (or just a piece that has an angle if I understand your question properly) of that section you need to reduce the X maximum value each time through... something like:
def copyPic():
file=pickAFile()
oldPic=makePicture(file)
newPic=makeEmptyPicture(getWidth(oldPic),getHeight(oldPic))
xstart=getWidth(oldPic)/2
ystart=getHeight(oldPic)/2
# The next line gets the max value x can be (width of pic)
xmax = getWidth(oldPic)
for y in range(ystart,getHeight(oldPic)):
# Now loop from the middle (xstart) to the end (xmax)
for x in range(xstart, xmax):
oldPixel=getPixel(oldPic,x,y)
colour=getColor(oldPixel)
newPixel=getPixel(newPic,x,y)
setColor(newPixel,colour)
# Now the x-loop has finished for this line (this value of y)
# so reduce xmax by 10 (or whatever value) ready for the next line
xmax = xmax - 10
# Then you should do some checking in your code to ensure
# xmax is not < xstart... here is something crude that should work
if xmax < xstart:
xmax = xstart
explore(newPic)
Your code I think will take an image like this:
+------------+
| 1 2 |
| |
| 3 4 |
| |
+------------+
and give this:
+-----+
| 4 |
| |
+-----+
because your X loop is always the same length
By reducing x each time as shown, you should get something like this:
+-----+
| 4 /
| /
+-
This is not very well coded, and I could rewrite the whole thing... but if you are just learning python then at least the modifications I made to your code should work well with what you already have, and should be easy to follow. I hope that it helps, and feel free to ask for clarification if you need it.
Cheers
PS: I see you asked this twice - you shouldn't ask the same question twice since it splits the answers, and makes it harder for people trying to find answers like this later on...
回答2:
One easy way to confuse a QR scanner is to replace the three positioning squares of a code with random cells. This has been done to image3.png
, which is in minimal form. Your function addSquares(smallPic)
will add the three positioning squares and the white cells separating them from the active cells. Then fixCodes()
will expand the resulting image and save it.
来源:https://stackoverflow.com/questions/14330705/when-copying-a-picture-in-jython-python-how-to-copy-x-pixels-less-with-every-ro