GMImage.SetCompressType
-----------------------

Sets the compression type.

| Component | Version | macOS | Windows | Linux | Server | iOS SDK |
|---|---|---|---|---|---|---|
| [GraphicsMagick](component_GraphicsMagick.md) | [2.0](newinversion20.md) | ✅ Yes | ✅ Yes | ✅ Yes | ✅ Yes | ✅ Yes |

MBS( "GMImage.SetCompressType"; ImageRef; CompressionType ) 

**MBS**( **"GMImage.SetCompressType";** /\* Sets the compression type. \*/  
**$ImageRef**; /\* The image reference number.e.g. 1 \*/   
**$CompressionType**) /\* UndefinedCompression = 0, NoCompression = 1, BZipCompression = 2, FaxCompression = 3, Group4Compression = 4, JPEGCompression = 5, LosslessJPEGCompression = 6, LZWCompression = 7, RLECompression = 8, ZipCompression = 9e.g. 1 \*/ 

### Parameters

| Parameter | Description | Example |
|---|---|---|
| ImageRef | The image reference number. | 1 |
| CompressionType | UndefinedCompression = 0, NoCompression = 1, BZipCompression = 2, FaxCompression = 3, Group4Compression = 4, JPEGCompression = 5, LosslessJPEGCompression = 6, LZWCompression = 7, RLECompression = 8, ZipCompression = 9 | 1 |

### Result

Returns "OK" on success.

### Description

Sets the compression type.  
CompressionType is used to express the desired compression type when encoding an image. Be aware that most image types only support a sub-set of the available compression types. If the compression type specified is incompatible with the image, GraphicsMagick selects a compression type compatible with the image type.  
GIF is always LZW compressed, PNG is always zip compressed.  
### Examples

Convert image to 1 bit TIFF:

 # Load from container   
Set Variable \[ $Image ; Value: MBS ("[GMImage.NewFromContainer](GMImageNewFromContainer.md)"; GraphicsMagick::image ) \]  
  
\# make black and white with threshold   
Set Variable \[ $Result ; Value: MBS ("[GMImage.Threshold](GMImageThreshold.md)";$Image ; 127) \]  
  
\# mark as monochrome image   
Set Variable \[ $Result ; Value: MBS ("[GMImage.SetMonochrome](GMImageSetMonochrome.md)";$Image ; 1) \]  
  
\# set type to 1 for black/white   
Set Variable \[ $Result ; Value: MBS ("[GMImage.SetType](GMImageSetType.md)";$Image ; 1) \]  
  
\# set to use TIFF for compression   
Set Variable \[ $Result ; Value: MBS ("[GMImage.SetMagick](GMImageSetMagick.md)";$Image ; "TIFF") \]  
  
\# set to use CCITT 4 Fax compression   
Set Variable \[ $Result ; Value: MBS ("GMImage.SetCompressType";$Image ; 4) \]  
  
\# and write to a file   
Set Variable \[ $Result ; Value: MBS ("[GMImage.WriteToFile](GMImageWriteToFile.md)"; $Image ; "/tmp/test.tif") \]  
  
\# cleanup   
Set Variable \[ $Error ; Value: MBS ("[GMImage.Release](GMImageRelease.md)";$Image ) \]  
### See also

- [GMImage.GetCompressType](GMImageGetCompressType.md)
- [GMImage.NewFromContainer](GMImageNewFromContainer.md)
- [GMImage.Release](GMImageRelease.md)
- [GMImage.SetClassType](GMImageSetClassType.md)
- [GMImage.SetMagick](GMImageSetMagick.md)
- [GMImage.SetMonochrome](GMImageSetMonochrome.md)
- [GMImage.SetType](GMImageSetType.md)
- [GMImage.Threshold](GMImageThreshold.md)
- [GMImage.WriteToFile](GMImageWriteToFile.md)
- [GMImage.WriteToTiffContainer](GMImageWriteToTiffContainer.md)

This function checks for a license.

Created 18th August 2014 , last changed 25th February 2026

  
[GMImage.SetCompose](GMImageSetCompose.md) - [GMImage.SetDebug](GMImageSetDebug.md)

[HTML Version](GMImageSetCompressType.shtml)