I'm currently trying to use a .png of a Tic Tac Toe board in Pygame so that the white part is transparent. However, I'm getting "libpng warning: sBIT: invalid" and the white is showing when I blit the board onto the background. The code is as follows:
background = pygame.image.load("blah.bmp")
board_surface = pygame.image.load("board.png")
board_surface.convert_alpha()
I read on the Pygame docs that only .bmp is guaranteed to work, and since image.get_extended() is returning a 1, I'm assuming that I simply don't have enough image processing support.
I've installed Pillows and imported the module, but I have no idea how to use this with Pygame. The tutorial shows how to load images as Pillow's Image class and so forth, but I need to represent images as Pygame's Surfaces.
I would appreciate any help with integrating these two things, or any other solutions on how to get my .png to work! For reference, I'm using Python3.3 and Pygame1.9.2
I have encountered this problem too. I solved it by switching to .tif (only with one f !). If you have Gimp installed use that to turn the white spaces into alpha=0 spaces (if you have problems doing that I can help with a more detailed explanation). After that import the image just as normal and use the .convert_alpha() method.
Related
i need code
How can a randomly generated 3dcube using just pygame be created from the top of the screen and then fall from the top to the bottom of the screen
please help me
using python pygame
As mentioned by #The_spider , pygame is a 2D Game engine. It cannot load 3D files or do any 3D stuff. A few recommended modules would be Panda3d and Ursina(which is a simplified version of Panda3d) install them via pip.
If you really need to use pygame, you'll have to save an image of the cube at every angle and Blit it on the screen using an extremely complicated algorithm. Moreover, it will be extremely slow. Thus, use a python 3D game engine.
So I'm trying to have my program detect a color of a pixel (with stated coordinates) and compare it with a HEX-given color. After it, it makes one of two moves. I want it to detect the color of pixel inside of a running program, so it is not image. Is there anyone who could've help me? What python library do I need for that?
I tried installing win32api and ctypes in pycharm but those seem outdated or not working.
How can I add image to my Turtle Screen using turtle graphics?
whenever I use the function addshape I keep getting errors.
does turtle graphics got any other way loading/importing images?
for example:
import turtle
screen = turtle.Screen()
image = r"C:\Users\myUser\Desktop\Python\rocketship.png"
screen.addshape(image)
turtle.shape(image)
The turtle module does have support for images, but only GIF images, not PNG or any other format. As the docs for addshape say:
name is the name of a gif-file and shape is None: Install the corresponding image shape.
And if you look at the source, they're serious about "gif-file": the way it decides whether you're trying to add an image or a polygon is by calling data.lower().endswith(".gif"), which obviously won't work for .png files.
And, even if you fix that, it will still only be able to handle the file formats that Tkinter supports out of the box, which includes some extra things like PPM/PGM/PBM, but still not PNG. If you want to support PNG files, you'll want to install Pillow as well.
At this point, you're getting beyond what people usually do with turtle. That might be worth pursuing (you'll learn a lot by doing so), but it may be simpler to use an image-converting program to convert the .png file to a .gif file so it will work with your existing code.
You can only use gif files with Python Turtle. Take any picture and convert/resize for free at ezgif.com.
Background:
win = turtle.Screen()
win.bgpic('background.gif')
Using shapes:
win.register_shape('pic1.gif')
sprite = turtle.Turtle()
sprite.shape('pic1.gif')
Example game:
https://www.youtube.com/watch?v=q49Xyo0LYDs
Hello all,
I am working on a program which determines the average colony size of yeast from a photograph, and it is working fine with the .bmp images I tested it on. The program uses pygame, and might use PIL later.
However, the camera/software combo we use in my lab will only save 16-bit grayscale tiff's, and pygame does not seem to be able to recognize 16-bit tiff's, only 8-bit. I have been reading up for the last few hours on easy ways around this, but even the Python Imaging Library does not seem to be able to work with 16-bit .tiff's, I've tried and I get "IOError: cannot identify image file".
import Image
img = Image.open("01 WT mm.tif")
My ultimate goal is to have this program be user-friendly and easy to install, so I'm trying to avoid adding additional modules or requiring people to install ImageMagick or something.
Does anyone know a simple workaround to this problem using freeware or pure python? I don't know too much about images: bit-depth manipulation is out of my scope. But I am fairly sure that I don't need all 16 bits, and that probably only around 8 actually have real data anyway. In fact, I once used ImageMagick to try to convert them, and this resulted in an all-white image: I've since read that I should use the command "-auto-levels" because the data does not actually encompass the 16-bit range.
I greatly appreciate your help, and apologize for my lack of knowledge.
P.S.: Does anyone have any tips on how to make my Python program easy for non-programmers to install? Is there a way, for example, to somehow bundle it with Python and pygame so it's only one install? Can this be done for both Windows and Mac? Thank you.
EDIT: I tried to open it in GIMP, and got 3 errors:
1) Incorrect count for field "DateTime" (27, expecting 20); tag trimmed
2) Sorry, can not handle images with 12-bit samples
3) Unsupported layout, no RGBA loader
What does this mean and how do I fit it?
py2exe is the way to go for packaging up your application if you are on a windows system.
Regarding the 16bit tiff issue:
This example http://ubuntuforums.org/showthread.php?t=1483265 shows how to convert for display using PIL.
Now for the unasked portion question: When doing image analysis, you want to maintain the highest dynamic range possible for as long as possible in your image manipulations - you lose less information that way. As you may or may not be aware, PIL provides you with many filters/transforms that would allow you enhance the contrast of an image, even out light levels, or perform edge detection. A future direction you might want to consider is displaying the original image (scaled to 8 bit of course) along side a scaled image that has been processed for edge detection.
Check out http://code.google.com/p/pyimp/wiki/screenshots for some more examples and sample code.
I would look at pylibtiff, which has a pure python tiff reader.
For bundling, your best bet is probably py2exe and py2app.
This is actually a 2 part question:
1) 16 bit image data mangling for Python - I usually use GDAL + Numpy. This might be a bit too much for your requirements, you can use PIL + Numpy instead.
2) Release engineering Python apps can get messy. Depending on how complex your app is you can get away with py2deb, py2app and py2exe. Learning distutils will help too.
I want to load a number of images from harddrive and place them on a larger white background. And I want to do it in Python. I am wondering what is the best way of doing that. I am using a windows machine and I can use any library I want. Any pointer to a webpage or a sample code that can point me to a good direction would be appreciated.
Something like this:
A very popular image processing library for Python is PIL. The official PIL tutorial might be useful, especially the section about "Cutting, Pasting and Merging Images".
PIL isn't enough. Try also with PIL the aggdraw library.
But aggdraw also isn't enough. It works poorly with transparency. I mean 0.5-1 gray pixel around opaque object over the transpparent area.