このモジュールには画像を読み込んだり保存したりする機能が含まれており、pygameで使用されるSurfaceを他のプログラムモジュールでも使用できる形式に変換することもできます。
pygameにはImageという名前のクラスはないので注意してください。;画像はSurfaceオブジェクトとして読み込まれます。この Surface クラスを使用することで、画像の描写でピクセル単位の編集、指定範囲の画像コピーといった様々な処理を行うことができます。
Pygameのバージョンにもよりますが、この画像処理モジュールでは既定の画像ファイル以外の読み込みもサポートしています。既定では非圧縮のBMP画像のみしか読み込むことができません。Pygameが画像読み込みをフルサポートしているバージョンの場合は、pygame.image.load命令で下記の画像形式を読み込むことができます.
画像の保存については、一部の形式しかサポートされていません。下記の画像形式で保存を行うことができます。
PNG形式、 JPEG形式での保存はpygameのバージョン1.8から新しく実装されました。
The image module contains functions for loading and saving pictures, as well as transferring Surfaces to formats usable by other packages.
Note that there is no Image class; an image is loaded as a Surface object. The Surface class allows manipulation (drawing lines, setting pixels, capturing regions, etc.).
The image module is a required dependency of Pygame, but it only optionally supports any extended file formats. By default it can only load uncompressed BMP images. When built with full image support, the pygame.image.load - load new image from a file function can support the following formats.
Saving images only supports a limited set of formats. You can save to the following formats.
PNG, JPEG saving new in pygame 1.8.
ソースとなるファイルから画像を読み込みます。引数には画像のファイル名か、Pythonの画像オブジェクトファイルを指定することができます。
Pygameは読み込んだ画像ファイルの形式(GIFやBitmapなど)を自動的に識別し、そのデータからSurfaceオブジェクトを新規に作成します。 場合によってはファイルの拡張子(GIF形式ならファイル名の後ろにつく".gif")を知っておく必要があります。プログラム内で作成した画像オブジェクトを引数として設定する場合、namehint引数に元ファイル名を設定したほうが管理がしやすいでしょう。
作成されたSurfaceは、読み込み元のファイルと同じカラー形式、透明色、透明度を持っています。引数を設定せずにSurface.convert命令を実行することにより、現在の描写環境に最適化された形式へ変換したコピーを作ることができます。
透明度について、png形式の画像は読み込んだ後にconvert_alpha()を使用することで、ピクセル単位の透明度を持つ画像へ変換することができます。
Pygameは全ての画像ファイルをサポートできるわけではありません。 少なくとも非圧縮形式のBMP形式はサポートしています。pygame.image.get_extended命令でTrueが返された場合は、(png形式、jpg形式、gif形式を含む)大抵のファイルは読み込むことができます。
使用環境による互換性を保つためにも、階層の情報をつなげる場合は os.path.join()命令を使用するようにしましょう。
dataフォルダ内にあるbla.pngファイルを読み込む例:
asurf = pygame.image.load(os.path.join('data', 'bla.png'))
Load an image from a file source. You can pass either a filename or a Python file-like object.
Pygame will automatically determine the image type (e.g., GIF or bitmap) and create a new Surface object from the data. In some cases it will need to know the file extension (e.g., GIF images should end in ".gif"). If you pass a raw file-like object, you may also want to pass the original filename as the namehint argument.
The returned Surface will contain the same color format, colorkey and alpha transparency as the file it came from. You will often want to call Surface.convert - change the pixel format of an image with no arguments, to create a copy that will draw more quickly on the screen.
For alpha transparency, like in .png images use the convert_alpha() method after loading so that the image has per pixel transparency.
Pygame may not always be built to support all image formats. At minimum it will support uncompressed BMP. If pygame.image.get_extended - test if extended image formats can be loaded returns 'True', you should be able to load most images( including png, jpg and gif ).
You should use os.path.join() for compatibility.
eg. asurf = pygame.image.load(os.path.join('data', 'bla.png'))
この命令によって、あなたが編集したSurfaceを BMP、 TGA、 PNG、JPEGといった形式の画像に保存することができます。ファイルの拡張子が指定されていなかったり間違っていた場合は、既定でTGA形式の画像に保存されます。TGA形式、BMP形式で保存する場合は、両方とも非圧縮状態のファイルで保存されます。
PNG、 JPEGでの保存はpygameのバージョン1.8から新たに実装されました。
This will save your Surface as either a BMP, TGA, PNG, or JPEG image. If the filename extension is unrecognized it will default to TGA. Both TGA, and BMP file formats create uncompressed files.
PNG, JPEG saving new in pygame 1.8.
このバージョンのpygameで既定の画像ファイル以外も読み込めるのであれば、この命令ではTrueが戻り値として返されます。どういった形式のファイルを読み込めるのかまでは判別できませんが、大抵の場合ほぼ全ての形式の画像ファイルを読み込むことができるでしょう。
If pygame is built with extended image formats this function will return True. It is still not possible to determine which formats will be available, but generally you will be able to load them all.
画像から文字列形式の画像情報を作成します。この文字列画像情報は他のPython画像処理モジュールで使用する際に、'fromstring'命令を使用して画像データに戻すことができます。Pythonの画像処理モジュールには画像データを下から上へ保存する形式のものがあります(PyOpenGLなど)。 flipped 引数にTrueを設定すると、文字列画像情報の保存形式を上下反転させることができます。
format引数には下記の文字の中から一つを選んで設定します。8bitのSurfaceでしか"P" を引数として設定できないので注意してください。他の引数は全てのSurfaceで設定することができます。 また、Python標準の画像処理命令ではPygameよりも多くの画像形式をサポートしていることも知っておいてください。
Creates a string that can be transferred with the 'fromstring' method in other Python imaging packages. Some Python image packages prefer their images in bottom-to-top format (PyOpenGL for example). If you pass True for the flipped argument, the string buffer will be vertically flipped.
The format argument is a string of one of the following values. Note that only 8bit Surfaces can use the "P" format. The other formats will work for any Surface. Also note that other Python image packages support more formats than Pygame.
この命令ではpygame.image.tostring命令とほぼ似たような流れで引数を設定します。size引数にはSurfaceの横幅と縦幅の情報を設定します。この命令でSurfaceを新規で作成したのであれば、元となった文字列形式のデータは破棄しても構わないでしょう。
size引数とformat引数には、文字列形式画像情報の元画像と同じサイズやファイル形式を設定しなければなりません。違う値を設定すると例外が発生してしまいます。
この命令よりも高速に画像変換処理ができるpygame.image.frombuffer命令にも目を通しておいてください。
This function takes arguments similar to pygame.image.tostring - transfer image to string buffer. The size argument is a pair of numbers representing the width and height. Once the new Surface is created you can destroy the string buffer.
The size and format image must compute the exact same size as the passed string buffer. Otherwise an exception will be raised.
See the pygame.image.frombuffer - create a new Surface that shares data inside a string buffer method for a potentially faster way to transfer images into Pygame.
文字列形式の画像情報から直接ピクセルデータを読み取り、Surfaceを新規に作成します。この命令は pygame.image.fromstringとほぼ同じ引数を設定しますが、ソース情報の上下反転は行うことができません。
Create a new Surface that shares pixel data directly from the string buffer. This method takes the same arguments as pygame.image.fromstring - create new Surface from a string buffer, but is unable to vertically flip the source data.