Pixels and images (ti_image): TI-84 Evo Python

Reading and writing single pixels, and drawing image files sent to the calculator. 6 functions in ti_image, each with a working example.

Try any of these in CalcPlex PRGM, a browser code editor, which sends your program to a calculator over USB.

ti_image.clear_image(

ti_image.clear_image() or ti_image.clear_image(x, y, w, h) or ti_image.clear_image(x, y, w, h, (r, g, b))

Clears the whole screen, or one rectangle, to white or to a color you choose.

clear_image() clears the whole screen to white.

clear_image(x, y, w, h) clears one rectangle to white, from (x, y) at the top left to (x + w, y + h) at the bottom right, both corners included. clear_image(50, 50, 20, 20) clears a 21 by 21 area. That is the same box ti_draw.draw_rect outlines with those numbers, border included, so it wipes a fill_rect completely.

Add a color tuple as a fifth argument, clear_image(x, y, w, h, (r, g, b)), to fill that rectangle with a color instead of white.

Watch out

Any clear_image call resets the drawing color to black, including the rectangle forms. If you picked a color with ti_draw.set_color, set it again after clear_image before you draw.

import ti_draw
import ti_image
import ti_system

# a blue rectangle
ti_image.clear_image(20, 20, 200, 150, (0, 120, 200))

# a white hole in the middle of it
ti_image.clear_image(60, 50, 100, 80)

ti_system.wait_key()

# the whole screen back to white
ti_image.clear_image()
ti_draw.set_color(0, 0, 0)
ti_draw.draw_text(10, 30, "All clear!")

ti_system.wait_key()

All three forms: fill a rectangle with a color, clear a rectangle to white, then clear everything.

See also: ti_draw.clear(, ti_draw.clear_rect(, ti_draw.fill_rect(

ti_image.get_pixel(

r, g, b = ti_image.get_pixel(x, y)

Reads the color of one pixel on the screen as (r, g, b).

Returns the red, green and blue values of the pixel at (x, y), each from 0 to 255. It reads whatever is on the screen right now, including shapes drawn with ti_draw. Nothing else on the calculator can read the screen back.

The values come back slightly rounded. The screen stores colors with less precision than you give it, so paint (200, 40, 40) and you read back (206, 40, 41). Each value can be off by up to 7, and very dark colors can come back as plain black. To check a color, compare with some slack, abs(r - 200) < 10, not r == 200.

x runs from 0 to 319 and y from 0 to 209.

Watch out

One pixel past the right or bottom edge is enough to crash the whole calculator: get_pixel(320, 100) gives ERROR: UNEXPECTED and needs a RAM reset. A loop that reaches x = 320 is the usual way this happens, so use range(320), never range(321).

import ti_draw
import ti_image
import ti_system

# paint a red square
ti_draw.set_color(200, 40, 40)
ti_draw.fill_rect(20, 20, 100, 100)

# read one pixel from inside it
r, g, b = ti_image.get_pixel(60, 60)

ti_draw.set_color(0, 0, 0)
ti_draw.draw_text(10, 170, "Red: " + str(r))

ti_system.wait_key()

Paints a red square and reads one pixel inside it. Red comes back as 206, not the 200 that was painted.

See also: ti_image.set_pixel(, ti_draw.get_screen_dim(, ti_draw.fill_rect(

ti_image.load_image(

ti_image.load_image("name")

Picks the image file that show_image() draws.

name is an image file (an image AppVar) that is already on the calculator, sent from a computer the same way as a program. A Python program can't make one itself.

load_image only picks the picture. Nothing appears until you call show_image(), and one load_image is enough to draw the same picture as many times as you like.

Watch out

If there is no image with that name on the calculator, load_image raises ValueError: missing image appvar.

import ti_image
import ti_system

# the image file BIRD has to be on the calculator already
ti_image.load_image("BIRD")

# draw it three times across the screen
for i in range(3):
    ti_image.show_image(40 + i * 90, 80)

ti_system.wait_key()

Picks the BIRD image once, then draws it three times in a row.

See also: ti_image.show_image(, ti_graphics.drawImage(

ti_image.set_pixel(

ti_image.set_pixel(x, y, (r, g, b))

Paints one pixel. The color goes in as one (r, g, b) tuple.

Sets the pixel at (x, y) to the color (r, g, b). The color is a single tuple with its own parentheses, which is different from ti_draw.set_color, which takes three separate numbers:

  • set_pixel(10, 20, (255, 0, 0)) paints a red pixel
  • set_pixel(10, 20, 255, 0, 0) is a TypeError

Pixels outside the screen are ignored, with no error.

import ti_image
import ti_system

# a diagonal line of blue pixels
for i in range(100):
    ti_image.set_pixel(50 + i, 50 + i, (0, 120, 200))

ti_system.wait_key()

Draws 100 blue pixels in a diagonal line. Notice the extra parentheses around the color.

See also: ti_image.get_pixel(, ti_draw.set_color(, ti_draw.plot_xy(

ti_image.show_image(

ti_image.show_image(x, y)

Draws the image picked with load_image(), with its top-left corner at (x, y).

(x, y) are ordinary ti_draw coordinates: show_image(0, 0) puts the picture in the top-left corner of the drawing area.

An image can have a see-through color, so it draws over any background without a box around it. Parts that go past the edge of the screen are cut off, with no error.

Watch out

Call load_image() first. Without it, show_image raises tiimageException: Image name not specified.

import ti_draw
import ti_image
import ti_system

# a dark green background
ti_draw.set_color(0, 100, 60)
ti_draw.fill_rect(-1, -1, 321, 211)

# the image file BIRD has to be on the calculator already
ti_image.load_image("BIRD")
ti_image.show_image(140, 90)

ti_system.wait_key()

The BIRD picture in the middle of a dark green screen, with no box around it.

See also: ti_image.load_image(, ti_graphics.drawImage(, ti_draw.fill_rect(

ti_image.show_screen(

ti_image.show_screen()

Pauses the program until CLEAR is pressed.

Freezes the program until CLEAR is pressed. No other key works, not even ENTER.

You don't need it to see your drawing. Shapes appear as soon as you draw them; this only holds the program so the shell doesn't replace the screen when the program ends. It does the same thing as ti_draw.show_draw(). To let ANY key continue, use ti_system.wait_key() instead.

import ti_draw
import ti_image
import ti_system

# a title screen
ti_draw.set_color(0, 0, 0)
ti_draw.draw_text(100, 80, "MY GAME")
ti_draw.draw_text(60, 120, "Press CLEAR to start")

# hold it until CLEAR
ti_image.show_screen()

ti_image.clear_image()
ti_draw.draw_text(10, 30, "Game started!")

ti_system.wait_key()

Shows a title screen, waits for CLEAR, then continues.

See also: ti_draw.show_draw(, ti_system.wait_key(, ti_image.clear_image(