How to changed colours of some pixels of an image? - python

I'm trying to leave one third of the image stock, change all the black to yellow in the middle, and change the bottom third black to blue. I know how to change the colours, the problem I'm facing is I'm unaware of how I can select only one third of the pixels to manipulate them. Here s what I have..
def changeSpots1():
file = pickAFile()
picture = makePicture(file)
show(picture)
pix = getAllPixels(picture)
for p in pix:
intensity = (getRed(p) + getGreen(p) + getBlue(p))
c = getColor(p)
if (intensity < 150):
newColour = setColor(p, yellow)
repaint(picture)
I am using a program called JES to write this, incase you're wondering about commands like pickAFile.
Thank you for any help!

I know nothing about JES, but I'm going to guess that getAllPixels returns the pixels in the usual order: the first row, then the next row, then the next, etc.
If so:
pix = getAllPixels(picture)
third = len(pix) // 3
for p in pix[:third]:
# do top-third stuff
for p in pix[third:third*2]:
# do middle-third stuff
for p in pix[third*2:]:
# do bottom-third stuff
This does assume that the picture s divisible perfectly into thirds. If it's not, you will need to know the picture's width so you can round to the nearest complete row (because otherwise the top third might actually be 250 complete rows and the first 47 pixels of the 251st, which won't look very good). I don't know what function JES has to get the width, but I'm sure it's simple.

Related

Check if pictures are similar in Python

I'm facing the following problem:
I have N pictures (300x300pixels, anchor is always top left). Of these N pictures, some are are the same, just mirrored and/or rotated.
As you see, the first 2 parts are nearly the same, just mirrored, while part 3 is completely different.
def compare_Image(sourceIMGPath, compareIMGPath):
sourceIMG = Image.open(sourceIMGPath, "r")
compareIMG = Image.open(compareIMGPath, "r")
for rotation in [0, 1]:
if rotation == 1: compareIMG = ImageOps.mirror(compareIMG)
for angle in [0, 90, 180, 270]:
compareIMG = compareIMG.rotate(angle)
imgDiff =ImageChops.difference(sourceIMG, compareIMG)
if not imgDiff.getbbox():
compareIMG.show()
sourceIMG.show()
print(str(Path(sourceIMGPath).stem) + " " + str(Path(compareIMGPath).stem))
return True
return False
I tried to mirror and rotate the picture, but surely this doesn't work, since the picture will move to the side/bottom of the picture which will move the anchor and so the picture will never be the same.
So I need to find a way, in which I maybe can reset the anchor or something similar. Maybe someone has an idea.

Need help making a drawing using recursion

I need to make a code in python that draws the output below using recursion and a function named drawpoly. drawpoly() will take 6 arguments which include a turtle, the length of a side, the number of sides, red, green & blue. While the number of sides is > 2, drawpoly() will recursively call itself decrementing the number of sides by 1 each call. Red will increment by 10, green by 25 & blue by 35 each call. I need to set fillcolor and begin and end fill. I will also need will need to set the screen colormode to be able to use integers to make your colors.
[![enter image description here][1]][1]
her's the code I have so far It doesn't print out anything but its a rough outline of what I need to use in the program
wn = trtl.Screen()
def drawpoly(trtl, num_sides, side_length,color):
numOfSides=8
num_sides=8
side_length=100
color="blue"
while (numOfSides >2):
trtl.drawpoly(trtl,num_sides,side_length,color)
numOfSides -=1
def main():
return drawpoly
main()
[1]: https://i.stack.imgur.com/65dS4.png

How do i add extra dimensions to a screenshot?

I currently have the code below.. and it takes the screenshot just fine.. but it cuts off the left and right side of the screenshot
How do i add another 10 pixels all around possible?
print "Taking SS..."
focusWindow = App.focusedWindow()
regionINFO = capture(App.focusedWindow())
shutil.move(regionINFO, os.path.join(r'C:\Screenshots', 'Dummy1.png'))
print "SS Done..."
It looks like you're taking a screenshot of a specific app. Try the whole screen.
import shutil # once at beginning of script
img = capture(SCREEN)
shutil.move(img, 'path')
The answer is present within your code itself. :)
focusWindow = App.focusedWindow()
Here, focusWindow stores the region of the App which the focus is upon. Either like Eugene has said above, you can go ahead to take the whole screenshot or you can append to the current of focusWindow and capture the region.
For eg.: Say you want to add 10 pixels on all sides, it would be like:
focusWindow.x -= 10
focusWindow.y -= 10
focusWindow.w += 20
focusWindow.h += 20
regionINFO = capture(focusWindow)
shutil.move(regionINFO, os.path.join(r'C:\Screenshots', 'Dummy1.png'))
print "SS Done..."

Color of selected rows in PyQt

I want to change color of selected rows in QTableWidget using python.
http://postimg.org/image/iyxb0wm4r/
I want that green part to be red.
If i try:
table = self.my_table
palette = QtGui.QPalette(table.palette())
palette.setColor(QtGui.QPalette.Highlight, Qt.red)
table.setPalette(palette)
it producies next picture. I need to double click, so the selected text is red.
http://postimg.org/image/rtc06mlil/
If I try:
table = self.my_table
table.setAutoFillBackground(True)
p = table.palette()
p.setColor(table.backgroundRole(), Qt.red)
table.setPalette(p)
it does this: (I can't post images and I can post only two links!) I try to put the link in comment. Anyway, it just set the color of grid to red.
So I found solution.
p = QtGui.QPalette(table.palette())
#This two for setting text color
p.setBrush(QtGui.QPalette.Active, QtGui.QPalette.HighlightedText,
QtGui.QBrush(QColor("red")))
p.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.HighlightedText,
QtGui.QBrush(QColor("red")))
#this two for setting background color
p.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Highlight,
QtGui.QBrush(QColor(255,0,0,127)))
p.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Highlight,
QtGui.QBrush(QColor("255,0,0,127")))
One thing - works bit differently on Linux and Windows, but for my purposes it is more than suitable.

Scrolling objects are jerky and sometimes jump to the right a few pixels [pygame]

To create an abstract, scrolling city skyline for a prototype, I created a class that generates random rectangles. These rects are added to a list and items are pulled from that list to be drawn on the screen. The rects begin off screen to the right and scroll across to the left until they leave the view plane and are trashed. The movement of the buildings is oddly jerky and they also shift to the right a few pixels at a specific point on the screen.
This video of the prototype is fairly accurate with very little video capture lag. Pay attention to the gaps between the buildings, as they get within the right most 3rd of the display area, the gap will suddenly shrink as if the building to the left of the gap is suddenly shifting right a few pixels. The smaller the gap, the more noticeable it is. The other anomalies in the video are from the recorder and are not present in the app. Here's a very short video that clearly shows this phenomenon: https://www.youtube.com/watch?v=0cdhrezjcY8
At about 1 second in you'll notice a very narrow gap between buildings in the rear layer. At :04 seconds in the gap is even with the blue player object, the left rectangle shifts and the gap vanishes. There's a second, larger gap to the right of that one that does the same thing but since the gap is larger it doesn't completely vanish. I've looked over the code numerous times but can't see what could be causing this anomaly. I'm hoping someone can tell me if it's something I did or a limitation I'm encountering.
I originally wrote this program linearly, without classes or functions. I rewrote it using a class that generates layer objects and handles all the generating and scrolling. In both cases the problem exists. It's driving me crazy trying to figure out why the buildings do not move smoothly. I've even written a version of this using png images instead of randomly generated rectangles. In that version the pngs scroll smoothly and seamlessly: https://www.youtube.com/watch?v=Uiw_giAvbOo (The video is a bit jerky, but the actual program plays smooth) So the issue is limited to these random rectangles.
Here's the code for the program: https://www.refheap.com/73079
Here's the class code by itself:
class Scroller():
def __init__(self, speed, color, heightMax):
# Speed of the layer scroll, the color of the layer and the maximum height for buildings
# set up the building parameters
self.buildingHeightMax = heightMax
self.buildingHeightMin = 100
self.buildingWidthMax = 125
self.buildingWidthMin = 75
self.buildings = []
self.layerspeed = speed
self.buildTime = True
self.buildCountdown = 10
self.color = color
def update(self):
# Check if it's time to build. If not, decrement counter
if self.buildTime == False:
self.buildCountdown -= 1
# If time is 0, time to build, reset counter to a new random time
if self.buildCountdown <= 0:
self.buildTime = True
self.buildCountdown = random.randint(3, self.layerspeed)
# create building if it's time
if self.buildTime:
# generate random width and height of building
buildingHeight = random.randint(self.buildingHeightMin, self.buildingHeightMax)
buildingWidth = random.randint(self.buildingWidthMin, self.buildingWidthMax)
buildingTop = WINDOWHEIGHT - buildingHeight
# This generates the building object from the above parameters
building = pygame.Rect(WINDOWWIDTH, buildingTop, buildingWidth, WINDOWHEIGHT)
self.buildTime = False
self.buildCountdown = random.randint(3, self.layerspeed * 5)
# add building to buildings list
self.buildings.append(building)
# move all buildings on layer at set speed
for building in self.buildings:
# if the building is off the screen, trash it. If not, move it to the
# right at the objects speed.
if building.right < 0:
self.buildings.remove(building)
else:
building.left -= self.layerspeed
# draw the Front buildings
for i in range(len(self.buildings)):
pygame.draw.rect(windowSurface, self.color, self.buildings[i])
Your problem most likely lies in:
# move all buildings on layer at set speed
for building in self.buildings:
# if the building is off the screen, trash it. If not, move it to the
# right at the objects speed.
if building.right < 0:
self.buildings.remove(building)
else:
building.left -= self.layerspeed
You're using remove on the same list you're iterating from, and this will make it skip the next building. So it's not the building to the right that's moving faster, it's the one to the left that has skipped moving.
You can see it yourself with this simple example:
a = [2, 3, 4, 1.5, 6, 8, 3.2]
for element in a:
if element == 4:
a.remove(element)
else:
print element
Try it and you'll see that not only 4 won't be printed, but also 1.5 will be skipped.
Possibly a good way to do it is to first iterate through all the buildings to see which ones need to be removed, then remove then all, and finally move all the ones that are left.
You might want to check this link for some good suggestions.
You're also updating the countdown twice, first on line 47 and then on line 58. Is there any reason for this?

Categories