943,722 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Unsolved
  • Views: 5162
  • Python RSS
Jun 6th, 2008
0

Overlaying graphics in Python?

Expand Post »
Is there a way to overlay graphics in Python? I'm looking for functionality similar to X-fire. That is, I want to be able to press a set of keys in a game and a GUI screen with some options (possibly a shell script) that doesn't cover up the game should come up without the need to alt+tab.

I would like to do this because the games I'm playing respond very negatively to alt+tabbing but X-Fire works fine in them. I've looked everywhere for a solution but have yet to find one.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Timotei is offline Offline
2 posts
since Jun 2008
Jun 7th, 2008
0

Re: Overlaying graphics in Python?

Here is an example using PIL ...
python Syntax (Toggle Plain Text)
  1. # generate a true opaque watermark with the Python Image Library (PIL)
  2. # free download from: http://www.pythonware.com/products/pil/
  3.  
  4. from PIL import Image, ImageEnhance
  5.  
  6. def reduce_opacity(im, opacity):
  7. """returns an image with reduced opacity"""
  8. assert opacity >= 0 and opacity <= 1
  9. if im.mode != 'RGBA':
  10. im = im.convert('RGBA')
  11. else:
  12. im = im.copy()
  13. alpha = im.split()[3]
  14. alpha = ImageEnhance.Brightness(alpha).enhance(opacity)
  15. im.putalpha(alpha)
  16. return im
  17.  
  18. def watermark(im, mark, position, opacity=1):
  19. """adds a watermark to an image"""
  20. if opacity < 1:
  21. mark = reduce_opacity(mark, opacity)
  22. if im.mode != 'RGBA':
  23. im = im.convert('RGBA')
  24. # create a transparent layer the size of the image and draw the
  25. # watermark in that layer.
  26. layer = Image.new('RGBA', im.size, (0,0,0,0))
  27. if position == 'tile':
  28. for y in range(0, im.size[1], mark.size[1]):
  29. for x in range(0, im.size[0], mark.size[0]):
  30. layer.paste(mark, (x, y))
  31. elif position == 'scale':
  32. # scale, but preserve the aspect ratio
  33. ratio = min(
  34. float(im.size[0]) / mark.size[0], float(im.size[1]) / mark.size[1])
  35. w = int(mark.size[0] * ratio)
  36. h = int(mark.size[1] * ratio)
  37. mark = mark.resize((w, h))
  38. layer.paste(mark, ((im.size[0] - w) / 2, (im.size[1] - h) / 2))
  39. else:
  40. layer.paste(mark, position)
  41. # composite the watermark with the layer
  42. return Image.composite(layer, im, layer)
  43.  
  44. def test():
  45. # pick images you have ...
  46. im = Image.open('Audi.jpg')
  47. mark = Image.open('Audi2.jpg')
  48. watermark(im, mark, 'tile', 0.3).show()
  49. # also try these ...
  50. #watermark(im, mark, 'scale', 0.3).show()
  51. #watermark(im, mark, (10, 10), 0.5).show()
  52.  
  53. if __name__ == '__main__':
  54. test()
I hope that is the sort of thing you meant.
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Jun 7th, 2008
0

Re: Overlaying graphics in Python?

Click to Expand / Collapse  Quote originally posted by vegaseat ...
Here is an example using PIL ...
python Syntax (Toggle Plain Text)
  1. # generate a true opaque watermark with the Python Image Library (PIL)
  2. # free download from: http://www.pythonware.com/products/pil/
  3.  
  4. from PIL import Image, ImageEnhance
  5.  
  6. def reduce_opacity(im, opacity):
  7. """returns an image with reduced opacity"""
  8. assert opacity >= 0 and opacity <= 1
  9. if im.mode != 'RGBA':
  10. im = im.convert('RGBA')
  11. else:
  12. im = im.copy()
  13. alpha = im.split()[3]
  14. alpha = ImageEnhance.Brightness(alpha).enhance(opacity)
  15. im.putalpha(alpha)
  16. return im
  17.  
  18. def watermark(im, mark, position, opacity=1):
  19. """adds a watermark to an image"""
  20. if opacity < 1:
  21. mark = reduce_opacity(mark, opacity)
  22. if im.mode != 'RGBA':
  23. im = im.convert('RGBA')
  24. # create a transparent layer the size of the image and draw the
  25. # watermark in that layer.
  26. layer = Image.new('RGBA', im.size, (0,0,0,0))
  27. if position == 'tile':
  28. for y in range(0, im.size[1], mark.size[1]):
  29. for x in range(0, im.size[0], mark.size[0]):
  30. layer.paste(mark, (x, y))
  31. elif position == 'scale':
  32. # scale, but preserve the aspect ratio
  33. ratio = min(
  34. float(im.size[0]) / mark.size[0], float(im.size[1]) / mark.size[1])
  35. w = int(mark.size[0] * ratio)
  36. h = int(mark.size[1] * ratio)
  37. mark = mark.resize((w, h))
  38. layer.paste(mark, ((im.size[0] - w) / 2, (im.size[1] - h) / 2))
  39. else:
  40. layer.paste(mark, position)
  41. # composite the watermark with the layer
  42. return Image.composite(layer, im, layer)
  43.  
  44. def test():
  45. # pick images you have ...
  46. im = Image.open('Audi.jpg')
  47. mark = Image.open('Audi2.jpg')
  48. watermark(im, mark, 'tile', 0.3).show()
  49. # also try these ...
  50. #watermark(im, mark, 'scale', 0.3).show()
  51. #watermark(im, mark, (10, 10), 0.5).show()
  52.  
  53. if __name__ == '__main__':
  54. test()
I hope that is the sort of thing you meant.
Thanks for the reply, that is KIND of what I meant but not similar enough to be useful.

I'm trying to create an overlay that will cover a full-screen game without minimizing it, allowing you to interact with a GUI that doesn't cover up your entire game or cause it to minimize. You should be able to type text in the GUI and click buttons without it interfering with the game in any way aside from intercepting mouse clicks and keyboard commands. My hope at the moment is to put a functioning python shell into the game (just for starters).

A program that does what I'm trying to accomplish is x-fire www.xfire.com
Last edited by Timotei; Jun 7th, 2008 at 7:22 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Timotei is offline Offline
2 posts
since Jun 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Python Beginner Help (Calculator)
Next Thread in Python Forum Timeline: File IO and pictures





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC