#!/usr/bin/env python# -*- coding: utf-8 -*-# Part of the PsychoPy library# Copyright (C) 2002-2018 Jonathan Peirce (C) 2019-2024 Open Science Tools Ltd.# Distributed under the terms of the GNU General Public License (GPL)."""Functions and classes related to image handling"""try:fromPILimportImageexceptImportError:importImageimportnumpyfrompsychopy.tools.typetoolsimportfloat_uint8
[docs]defarray2image(a):"""Takes an array and returns an image object (PIL)."""# fredrik lundh, october 1998## fredrik@pythonware.com# http://www.pythonware.com#ifa.dtype.kindin['u','I','B']:mode="L"elifa.dtype.kindin[numpy.float32,'f']:mode="F"else:raiseValueError("unsupported image mode")im=Image.frombytes(mode,(a.shape[1],a.shape[0]),a.tobytes())returnim
[docs]defimage2array(im):"""Takes an image object (PIL) and returns a numpy array. """# fredrik lundh, october 1998## fredrik@pythonware.com# http://www.pythonware.com#ifim.modenotin("L","F"):raiseValueError("can only convert single-layer images")imdata=im.tobytes()ifim.mode=="L":a=numpy.frombuffer(imdata,numpy.uint8)else:a=numpy.frombuffer(imdata,numpy.float32)a.shape=im.size[1],im.size[0]returna
[docs]defmakeImageAuto(inarray):"""Combines float_uint8 and image2array operations ie. scales a numeric array from -1:1 to 0:255 and converts to PIL image format. """returnimage2array(float_uint8(inarray))