メニュー

pygame.transform

Surfaceの編集・加工を行うためのpygameモジュール

このSurface編集命令では、ピクセルを操作して画像の移動やサイズ変更を行います。これらの命令では編集するSurfaceを引数に設定し、編集の加えられたSurfaceが新規に作成されて戻り値として返ります。

Surface編集命令の中にはピクセルデータの喪失が発生するものがあります。それらの命令によって作成されたSurfaceは、元Surfaceと比べてピクセルデータの一部が失われてます。簡単な例を挙げるとサイズの変更命令や回転命令などがこれに該当します。そのためSurfaceの再編集を行う場合は、一度編集したSurfaceではなくデータ喪失のない元Surfaceで編集処理を行ったほうがよいでしょう。(例えば、伸縮しながら跳ねるバネのアニメーションを作成するとします。一度編集した画像に更に変更を加えてサイズを変えていくと、詳細な画像データが失われてしまいます。そうした方法ではなく、常に元画像に編集を加えてバネの大きさを変えていくようにしましょう。)

pygame module to transform surfaces

A Surface transform is an operation that moves or resizes the pixels. All these functions take a Surface to operate on and return a new Surface with the results.

Some of the transforms are considered destructive. These means every time they are performed they lose pixel data. Common examples of this are resizing and rotating. For this reason, it is better to retransform the original surface than to keep transforming an image multiple times. (For example, suppose you are animating a bouncing spring which expands and contracts. If you applied the size changes incrementally to the previous images, you would lose detail. Instead, always begin with the original image and scale to the desired size.)


pygame.transform.flip

画像の上下、左右を反転させます。
pygame.transform.flip(Surface, xbool, ybool): return Surface

Surfaceの上下、左右、もしくはその両方を反転させることができます。反転したSurfaceではピクセルデータの喪失は起こらないので、新規に作成されたSurfaceの大きさは変わりません。

flip vertically and horizontally.
pygame.transform.flip(Surface, xbool, ybool): return Surface

This can flip a Surface either vertically, horizontally, or both. Flipping a Surface is nondestructive and returns a new Surface with the same dimensions.


pygame.transform.scale

画像のサイズを変更します。
pygame.transform.scale(Surface, (width, height), DestSurface = None): return Surface

Surfaceのサイズを変更します。この命令では変更後画像の見栄えは考慮されず、素早い変更処理が行われます。

新規にSurfaceを作成するのではなく、追加で指定できるDestSurface引数で画像の描写先Surfaceを設定することもできます。サイズ変更処理を繰り返し行う場合はこの方法を行ったほうが処理が速くなります。しかしdestination引数に設定するSurfaceのサイズは、(width, height)引数に設定する値と同じでなければいけません。また描写先Surfaceは元Surfaceと同じ形式でなければなりません。

resize to new resolution.
pygame.transform.scale(Surface, (width, height), DestSurface = None): return Surface

Resizes the Surface to a new resolution. This is a fast scale operation that does not sample the results.

An optional destination surface can be used, rather than have it create a new one. This is quicker if you want to repeatedly scale something. However the destination must be the same size as the (width, height) passed in. Also the destination surface must be the same format.


pygame.transform.rotate

画像を回転させます。
pygame.transform.rotate(Surface, angle): return Surface

変更後画像の見栄えは考慮されずに、反時計回りの回転処理を行います。angle引数は回転する角度を表し、浮動小数点を設定できます。マイナスの値を設定すると時計回りの回転を行います。

回転する角度が90の倍数でない場合は、画像は回転後の描写域を保持するために大きくなります。画像にピクセル単位の透過処理が行われている場合、回転によって広がった範囲は透明になります。ピクセル単位の透過でない場合は、Surfaceの透明色か左上のピクセルの色が該当範囲に設定されます。

rotate an image.
pygame.transform.rotate(Surface, angle): return Surface

Unfiltered counterclockwise rotation. The angle argument represents degrees and can be any floating point value. Negative angle amounts will rotate clockwise.

Unless rotating by 90 degree increments, the image will be padded larger to hold the new size. If the image has pixel alphas, the padded area will be transparent. Otherwise pygame will pick a color that matches the Surface colorkey or the topleft pixel value.


pygame.transform.rotozoom

Surfaceの変換を伴う画像の拡大縮小、回転を行います。
pygame.transform.rotozoom(Surface, angle, scale): return Surface

これは画像の拡大縮小と回転を同時に行う命令です。作成された画像は、見栄えの修正がされた32bitのSurfaceに変換されます。scale引数には、現在の解像度の倍数となる浮動小数点を設定します。angle引数には反時計回りで回転する角度となる浮動小数点を設定します。angle引数にマイナスの値を設定すると時計回りで回転が行われます。

filtered scale and rotation.
pygame.transform.rotozoom(Surface, angle, scale): return Surface

This is a combined scale and rotation transform. The resulting Surface will be a filtered 32-bit Surface. The scale argument is a floating point value that will be multiplied by the current resolution. The angle argument is a floating point value that represents the counterclockwise degrees to rotate. A negative rotation angle will rotate clockwise.


pygame.transform.scale2x

画像を二倍に拡大する専用の命令です。
pygame.transform.scale2x(Surface, DestSurface = None): Surface

元画像の二倍の大きさの画像を新規に作成し、戻り値として返します。この命令では、ビットマップ画像の輪郭部がギザギザになるのを抑えて拡大縮小を行うAdvanceMAME Scale2Xというアルゴリズムを使用します。

この命令は、単色のシンプルな画像でのみ効果を発揮します。写真やアンチエイリス処理の行われた画像では、見栄えが考慮されずに編集処理が行われます。

新規にSurfaceを作成するのではなく、追加で指定できるDestSurface引数で画像の描写先Surfaceを設定することもできます。サイズ変更処理を繰り返し行う場合はこの方法を行ったほうが処理が速くなります。しかしdestination引数に設定するSurfaceのサイズは、元Surfaceの二倍でなければなりません。また描写先Surfaceは元Surfaceと同じ形式でなければなりません。

specialized image doubler.
pygame.transform.scale2x(Surface, DestSurface = None): Surface

This will return a new image that is double the size of the original. It uses the AdvanceMAME Scale2X algorithm which does a 'jaggie-less' scale of bitmap graphics.

This really only has an effect on simple images with solid colors. On photographic and antialiased images it will look like a regular unfiltered scale.

An optional destination surface can be used, rather than have it create a new one. This is quicker if you want to repeatedly scale something. However the destination must be twice the size of the source surface passed in. Also the destination surface must be the same format.


pygame.transform.smoothscale

見栄えを考慮しながら、Surfaceを指定したサイズへ拡大縮小します。
pygame.transform.smoothscale(Surface, (width, height), DestSurface = None): return Surface

必要に応じて二つの異なるアルゴリズムを使用し、Surfaceの拡大縮小を行います。縮小処理を行う場合、出力された画像の各ピクセルの色は、それぞれがカーバーする範囲の平均となる色になります。 拡大処理を行う場合、バイリニアフィルタリングが使用されます。使用環境のCPUアーキテクチャがAMD64やi686の場合は、最適化されたMMXルーチンが搭載されているため他の環境よりも高速に動作するでしょう。変更するサイズは(width, height)の二つの数値で設定します。この命令では24ビットか32ビットのSurfaceのみに処理を行うことができます。引数として設定したSurfaceのビット深度が24未満の場合は例外は発生します。

この命令は pygame のバージョン1.8にて新しく実装されました。

scale a surface to an arbitrary size smoothly.
pygame.transform.smoothscale(Surface, (width, height), DestSurface = None): return Surface

Uses one of two different algorithms for scaling each dimension of the input surface as required. For shrinkage, the output pixels are area averages of the colors they cover. For expansion, a bilinear filter is used. For the amd64 and i686 architectures, optimized MMX routines are included and will run much faster than other machine types. The size is a 2 number sequence for (width, height). This function only works for 24-bit or 32-bit surfaces. An exception will be thrown if the input surface bit depth is less than 24.

New in pygame 1.8


pygame.transform.get_smoothscale_backend

スムーズな拡大縮小に使用されるフィルターの種類を戻り値として返します。: フィルターには'GENERIC'、'MMX'、'SSE'の三つがあります。
pygame.transform.get_smoothscale_backend(): return String

スムーズな拡大縮小を行う際にMMXSSEでの高速化が行われているかどうかを調べます。高速化が行われていない場合は、"GENERIC"が戻り値として返されます。x86プロセッサー環境では、使用される高速化のレベルはプログラム実行時に決定されます。

この命令はPygameで作成したプログラムのテストやデバッグを行う時に使用するものです。

return smoothscale filter version in use: 'GENERIC', 'MMX', or 'SSE'
pygame.transform.get_smoothscale_backend(): return String

Shows whether or not smoothscale is using MMX or SSE acceleration. If no acceleration is available then "GENERIC" is returned. For a x86 processor the level of acceleration to use is determined at runtime.

This function is provided for Pygame testing and debugging.


pygame.transform.set_smoothscale_backend

スムーズな拡大縮小に使用されるフィルターの種類を設定します。: フィルターには'GENERIC'、'MMX'、'SSE'の三つがあります。
pygame.transform.get_smoothscale_backend(type): return None

スムーズな拡大縮小時に使用する高速化の種類を設定します。引数は文字列で設定します。'GENERIC'を引数に設定すると高速化の設定が解除されます。'MMX'を引数に設定しなければ、MMXでの高速化処理は使用できません。同様に、'SSE'を引数に設定しなければSSEでの高速化処理は使用できません。設定した高速化フィルターが使用するプロセッサー環境に認識されなかったりサポートされていない場合は、エラーが発生してしまいます。

この命令はPygameで作成したプログラムのテストやデバッグを行う時に使用するものです。スムーズな拡大縮小処理で「無効な命令」というエラーが発生した場合は、PygameやSDLの開発元に報告したほうがよいでしょう。そのエラーが修正されるまで一時的な対応として、この命令で高速化フィルターの切り替えを行います。

set smoothscale filter version to one of: 'GENERIC', 'MMX', or 'SSE'
pygame.transform.get_smoothscale_backend(type): return None

Sets smoothscale acceleration. Takes a string argument. A value of 'GENERIC' turns off acceleration. 'MMX' uses MMX instructions only. 'SSE' allows SSE extensions as well. A value error is raised if type is not recognized or not supported by the current processor.

This function is provided for Pygame testing and debugging. If smoothscale causes an invalid instruction error then it is a Pygame/SDL bug that should be reported. Use this function as a temporary fix only.


pygame.transform.chop

指定範囲を削除した状態の画像を、新規にコピー作成します。
pygame.transform.chop(Surface, rect): return Surface

画像の一部を削除します。指定した四角範囲だけでなく、四角範囲の水平方向の延長上にある全てのピクセルと、垂直方向の延長上にある全てのピクセルも削除されます。隅の範囲 (引数として指定した範囲の対角に位置する範囲) の画像が切り取られて戻り値として返されます。 (この命令によって元画像が変更されることはありません。)

NOTE:指定した範囲内の画像を切り取ってコピーしたい場合は、Surfaceのblit命令を使用して別のSurfceやsubSurfaceにコピー描写をするとよいでしょう。

gets a copy of an image with an interior area removed
pygame.transform.chop(Surface, rect): return Surface

Extracts a portion of an image. All vertical and horizontal pixels surrounding the given rectangle area are removed. The corner areas (diagonal to the rect) are then brought together. (The original image is not altered by this operation.)

NOTE: If you want a "crop" that returns the part of an image within a rect, you can blit with a rect to a new surface or copy a subsurface.


pygame.transform.laplacian

Surfaceの輪郭部分を抽出します。
pygame.transform.laplacian(Surface, DestSurface = None): return Surface

ラプラシアンアルゴリズムを使用してSurfaceの輪郭部分を抽出します。

この命令はpygameのバージョン1.8にて実装されました。

find edges in a surface
pygame.transform.laplacian(Surface, DestSurface = None): return Surface

Finds the edges in a surface using the laplacian algorithm.

New in pygame 1.8


pygame.transform.average_surfaces

指定した複数Surfaceを合成したSurfaceを作成します。
pygame.transform.average_surfaces(Surfaces, DestSurface = None, palette_colors = 1): return Surface

複数のSurfacesを引数として設定し、各Surfaceの持つ色の平均値を合成させたSurfaceを作成し、戻り値として返します。

palette_colors引数にTrueが設定されている場合はカラーパレットの情報を元にして色の平均値を取得し、そうでない場合はピクセルの色情報を直接参照して平均値を取得します。この命令はsurfaceがパレットカラー方式ではなくグレースケールカラー方式を使用している場合に効果的です。

現時点で、この命令ではsurfacesに設定されているカラーパレットを正しく処理することができないので気をつけてください。

この命令はpygameのバージョン1.8で実装され、palette_colors引数はpygameのバージョン1.9で実装されました。

find the average surface from many surfaces.
pygame.transform.average_surfaces(Surfaces, DestSurface = None, palette_colors = 1): return Surface

Takes a sequence of surfaces and returns a surface with average colors from each of the surfaces.

palette_colors - if true we average the colors in palette, otherwise we average the pixel values. This is useful if the surface is actually greyscale colors, and not palette colors.

Note, this function currently does not handle palette using surfaces correctly.

New in pygame 1.8 palette_colors argument new in pygame 1.9


pygame.transform.average_color

surfaceで使用されている色の平均値を取得します。
pygame.transform.average_color(Surface, Rect = None): return Color

surface全体、もしくはsurfaceの指定した範囲内で使用されている色の平均値を、Color型の戻り値として返します。

finds the average color of a surface.
pygame.transform.average_color(Surface, Rect = None): return Color

Finds the average color of a Surface or a region of a surface specified by a Rect, and returns it as a Color.


pygame.transform.threshold

surface上に、指定した閾値内の色を持つピクセルがどれくらいあるのか調べます。
pygame.transform.threshold(DestSurface, Surface1, color, threshold = (0,0,0,0), diff_color = (0,0,0,0), change_return = 1, Surface2 = None, inverse = False): return num_threshold_pixels

surface上に、指定した閾値内の色を持つピクセルがどれくらいあるのか調べます。

surface2引数が設定されていた場合、color引数に指定した色ではなくそのsurface2の同じピクセル位置にある色が閾値の基準として使用されます。surface1とsurface2の同一位置にあるピクセルを比較し、閾値内の色かどうかを調べます。

change_return引数に0が設定されていた場合は、Surface1上から閾値内の色を持つピクセルの数を数えて戻り値として返すだけの処理を行います。

change_return引数に1が設定されていた場合は、Surface1上から閾値内の色を持つピクセルを抽出して、color引数に設定した色へ変更してDestSurfaceへ描写します。

change_return引数に2が設定されていた場合は、Surface1上から閾値内の色を持つピクセルを抽出して、そのままDestSurfaceへ描写します。

閾値外の色のピクセルを抽出し、diff_color引数に設定した色に変更した上でDestSurfaceへ描写することもできます。追加設定できるinverse引数にTrueが設定されていた場合は、閾値内の色のピクセルがdiff_color引数に設定した色に変更されてDestSurfaceへ抽出描写されます。

threshold引数には(赤値,緑値,青値,透明度)のColor型値を設定し、赤値,緑値,青値にはそれぞれ異なった閾値を設定することができます。赤の閾値には40を設定し、青の閾値には2を設定するといった任意の使い方ができます。

この命令はpygameのバージョン1.8にて新たに実装されました。

※原文のままでは訳しづらいため、「It can set the destination surface where all of the pixels not within the threshold are changed to diff_color. If inverse is optionally set to True, the pixels that are within the threshold are instead changed to diff_color.(閾値外の色のピクセルを抽出し、diff_color引数に設定した色に変更した上でDestSurfaceへ描写することもできます。追加設定できるinverse引数にTrueが設定されていた場合は、閾値内の色のピクセルがdiff_color引数に設定した色に変更されてDestSurfaceへ抽出描写されます。)」の順番を後ろに持ってきて訳しています。

補足
 
色の閾値について

color引数には基準となる色を設定し、threshold引数には基準値との距離制限を設定します。
距離制限を指定できるのは赤値、緑値、青値の三つで、透明度については無視されます。
threshold引数に(10,10,10)が設定された場合は、それぞれの要素値の差が10以内のものが閾値内の色となります。

例えばcolor引数に(125,255,0)、threshold引数に(10,20,30)が設定された場合、
(115<=r<=135 , 235<=g<=255 , 0<=b<=30)に該当する色が閾値内の色となります。
finds which, and how many pixels in a surface are within a threshold of a color.
pygame.transform.threshold(DestSurface, Surface, color, threshold = (0,0,0,0), diff_color = (0,0,0,0), change_return = 1, Surface = None, inverse = False): return num_threshold_pixels

Finds which, and how many pixels in a surface are within a threshold of a color.

It can set the destination surface where all of the pixels not within the threshold are changed to diff_color. If inverse is optionally set to True, the pixels that are within the threshold are instead changed to diff_color.

If the optional second surface is given, it is used to threshold against rather than the specified color. That is, it will find each pixel in the first Surface that is within the threshold of the pixel at the same coordinates of the second Surface.

If change_return is set to 0, it can be used to just count the number of pixels within the threshold if you set

If change_return is set to 1, the pixels set in DestSurface will be those from the color.

If change_return is set to 2, the pixels set in DestSurface will be those from the first Surface.

You can use a threshold of (r,g,b,a) where the r,g,b can have different thresholds. So you could use an r threshold of 40 and a blue threshold of 2 if you like.

New in pygame 1.8