> For the complete documentation index, see [llms.txt](https://help.trikset.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://help.trikset.com/trik/programming-code/function-getphoto.md).

# Функция getPhoto

## Описание функции

Функция возвращает одномерный массив байт, в который записаны пиксели изображения в формате **`rgb32`**, снятого с камеры (размер изображения — 160x120). Данная функция может быть использована для решения задач распознавания маркеров.

## Вызов функции

{% tabs %}
{% tab title="Python" %}

```python
pic = getPhoto()
```

{% endtab %}

{% tab title="JavaScript" %}

```javascript
var pic = getPhoto();
```

{% endtab %}
{% endtabs %}

## Пример работы

Рассмотрим пример преобразования кадра, снятого на камеру, в цветовые форматы «оттенок серого» и «черно-белый».

Сделайте снимок и выведите результат на экран c помощью функции [**show**](/trik/programming-code/object-brick/class-display.md#show).

{% tabs %}
{% tab title="Python" %}

```python
# взять кадр в формате rgb32
pic = getPhoto()
brick.display().show(pic, total_height, total_width, "rgb32")
script.wait(2000)
```

{% endtab %}

{% tab title="JavaScript" %}

```javascript
// взять кадр в формате rgb32
var pic = getPhoto();
brick.display().show(pic, total_height, total_width, "rgb32");
script.wait(2000);
```

{% endtab %}
{% endtabs %}

Результат:

![](https://thumb.tildacdn.com/tild6535-6131-4638-b834-323461306536/-/resize/240x/-/format/webp/getphoto1.png)

## Реализация функции перевода изображения в оттенок серого

{% tabs %}
{% tab title="Python" %}

```python
def grayScale(sPic):
	bufPic = []
	for i in range(total_height):
		for j in range(total_width):
			x = i * total_width + j
			p = sPic[x]
			r = (p & 0xff0000) >> 16	# взять значение компоненты красного
			g = (p & 0xff00) >> 8 # взять значение компоненты зеленого
			b = (p & 0xff) # взять значение компоненты синего
			p = r * 0.299 + g * 0.587 + b * 0.114; # компонента Y из YUV
			bufPic.append(p)
	return bufPic
```

{% endtab %}

{% tab title="JavaScript" %}

```javascript
function grayScale(sPic){
	bufPic = [];
	for(var i = 0; i < total_height; i++)
		for(var j = 0; j < total_width; j++){
			var x = i * total_width + j;
			var p = sPic[x];
			var r = (p & 0xff0000) >> 16;	// взять значение компоненты красного
			var g = (p & 0xff00) >> 8;	// взять значение компоненты зеленого
			var b = (p & 0xff);		// взять значение компоненты синего
			p = r * 0.299 + g * 0.587 + b * 0.114; // компонента Y из YUV
			bufPic[x] = p; 
		}
	return bufPic;
}
```

{% endtab %}
{% endtabs %}

Перевод изображения в черно-белый формат и вывод на дисплей:

{% tabs %}
{% tab title="Python" %}

```python
pic = grayScale(pic)

brick.display().show(pic, total_height, total_width, "grayscale8")
script.wait(2000)
```

{% endtab %}

{% tab title="JavaScript" %}

```javascript
var pic = grayScale(pic);

brick.display().show(pic, total_height, total_width, "grayscale8");
script.wait(2000);
```

{% endtab %}
{% endtabs %}

Результат:

![](https://thumb.tildacdn.com/tild3136-3162-4264-b135-313732663734/-/resize/240x/-/format/webp/getphoto2.png)

## Реализация функции бинаризации изображения

{% tabs %}
{% tab title="Python" %}

```python
# Бинаризация -- перевод изображения в ЧБ
def binarization(treshold, sPic):
	bufPic = []
	for i in range(len(sPic)):
		bufPic.append(255 if sPic[i] > treshold else 0)
	return bufPic
```

{% endtab %}

{% tab title="JavaScript" %}

```javascript
// Бинаризация -- перевод изображения в ЧБ
function binarization(treshold, sPic){
	bufPic = []
	for(var i = 0; i < sPic.length; i++){
		bufPic[i] = sPic[i] > treshold ? 255 : 0;
	}
	return bufPic;
}
```

{% endtab %}
{% endtabs %}

Перевод изображения в черно-белого и вывод на дисплей:

{% tabs %}
{% tab title="Python" %}

```python
pic = binarization(80, pic)
brick.display().show(pic, total_height, total_width, "grayscale8")
script.wait(2000)
```

{% endtab %}

{% tab title="JavaScript" %}

```javascript
var pic = binarization(80, pic);
brick.display().show(pic, total_height, total_width, "grayscale8");
script.wait(2000);
```

{% endtab %}
{% endtabs %}

Результат:&#x20;

![](https://thumb.tildacdn.com/tild3430-3165-4933-b962-626438366662/-/resize/240x/-/format/webp/getphoto3.png)

{% hint style="info" %}
[Пример программы с использованием функции getPhoto](https://dl.trikset.com/trikset-help/getPhoto.js).
{% endhint %}

{% hint style="info" %}
Дополнительный материал: презентация Университета Иннополис «[Распознавание меток ARTag](https://drive.google.com/file/d/15jbnB8Yv2HZq51Hwwi1jAu7dPtxyN8TS/view)».
{% endhint %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://help.trikset.com/trik/programming-code/function-getphoto.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
