pytoshop.util

Miscellaneous utilities.

pytoshop.util.assert_is_list_of(value, cls, min=None, max=None)[source]

If value is not a list of cls instances, raises TypeError.

pytoshop.util.decode_unicode_string(data)[source]

Decode Photoshop’s definition of a Unicode String.

pytoshop.util.do_byteswap(arr)[source]

Return a copy of an array, byteswapped.

pytoshop.util.encode_unicode_string(s)[source]

Encode Photoshop’s definition of a Unicode String.

pytoshop.util.ensure_bigendian(arr)[source]

Ensure that a Numpy array is in big-endian order.

Returns a copy if the endianness needed to be changed.

pytoshop.util.ensure_native_endian(arr)[source]

Ensure that a Numpy array is in native-endian order.

Returns a copy if the endianness needed to be changed.

pytoshop.util.get_channel(color, color_mode, channels)[source]
pytoshop.util.log(msg, *args)[source]

Print a logging message if debugging is turned on.

pytoshop.util.needs_byteswap(arr)[source]

Returns True if the array needs to be byteswapped.

pytoshop.util.pack_bitflags(*values)[source]

Pack separate booleans back into a bit field.

pytoshop.util.pad(number, divisor)[source]

Pads an integer up to the given divisor.

pytoshop.util.pascal_string_length(value, padding=1)[source]

Calculates the total length of writing a UTF-8-encoded Pascal string to disk.

Parameters:value (str) – A unicode string value.
Returns:length – The length, in bytes.
Return type:int
pytoshop.util.read_pascal_string(fd, padding=1)[source]

Read a UTF-8-encoded Pascal string from a file.

Parameters:
  • fd (file-like object) – Must be opened for reading, seekable and in binary mode.
  • padding (int, optional) – If provided, additional pad bytes will be read until the total amount read is a multiple of padding.
Returns:

value – The unicode value of the string.

Return type:

str

pytoshop.util.read_unicode_string(fd)[source]

Read a UTF-16-BE-encoded Unicode string (with length) from a file.

Parameters:fd (file-like object) – Must be opened for reading, seekable and in binary mode.
Returns:value – The unicode value of the string.
Return type:str
pytoshop.util.read_value(fd, fmt, endian='>')[source]

Read a values from a file-like object.

Parameters:
  • fd (file-like object) – Must be opened for reading, in binary mode.
  • fmt (str) – A struct module format character string.
  • endian (str) – The endianness. Must be > or <. Default: >.
Returns:

value – The value(s) read from the file.

If a single value, it is returned alone. If multiple values, a tuple is returned.

Return type:

any

pytoshop.util.set_channel(color, channel, color_mode, channels)[source]
pytoshop.util.trace_read(func)[source]

Prints debugging information from a read or write method.

For internal use only.

pytoshop.util.trace_write(func)

Prints debugging information from a read or write method.

For internal use only.

pytoshop.util.unicode_string_length(value)[source]

Calculates the total length of writing a UTF-16-BE-encoded Unicode string (with length) to a file.

Parameters:value (str) – A unicode string value.
Returns:length – The length, in bytes.
Return type:int
pytoshop.util.unpack_bitflags(value, nbits)[source]

Unpack a bitfield into its constituent parts.

pytoshop.util.write_pascal_string(fd, value, padding=1)[source]

Write a UTF-8-encoded Pascal string to a file.

Parameters:
  • fd (file-like object) – Must be opened for writing and in binary mode.
  • value (str) – A unicode string value.
  • padding (int, optional) – If provided, additional pad bytes will be written until the total amount written is a multiple of padding.
pytoshop.util.write_unicode_string(fd, value)[source]

Write a UTF-16-BE-encoded Unicode string (with length) to a file.

Parameters:
  • fd (file-like object) – Must be opened for writing and in binary mode.
  • value (str) – A unicode string value.
pytoshop.util.write_value(fd, fmt, *value, **kwargs)[source]

Write a single binary value to a file-like object.

Parameters:
  • fd (file-like object) – Must be opened for writing, in binary mode.
  • fmt (str) – A struct module format character string.
  • value (any) – The value to encode and write to the file.
  • endian (str) – The endianness. Must be > or <. Default: >.