首页 > 编程语言 >COP3502 P2: RLE with Images Python

COP3502 P2: RLE with Images Python

时间:2024-10-22 18:43:53浏览次数:1  
标签:P2 10 15 string RLE image Python data

COP3502 P2: RLE with Images Python

Overview

In this project students will develop routines to encode and decode data for images using run-length encoding

RLE). Students will implement encoding and decoding of raw data, conversion between data and strings, anddisplay of information by creating procedures that canbecalled from within their programs and externally. Thisproject will give students practice with loops, strings, Python lists, methods, and type-casting.

Run-Length Encoding

RLE is a form of lossless compression used in many industry applications, including imaging. It is intended totake advantage of datasets where elements (such as bytesor characters) are repeated several times in a row inrtain types of data (such as pixel art in games). Black pixels often appear in long “runs” in some animationframes; instead of representing each black pixelindividually, the color is recorded once, following by the numberof instances.

For example, consider the first row of pixels from the pixel image of a gator

(shown in Figure 1). The color black is “0”, and green is “2”:

Flat (unencoded) data: 0 0 2 2 2 0 0 0 0 0 0 2 2 0

Run-length encoded data: 2 0 3 2 6 0 2 2

Figure 1 – Gator Pixel Image

he encoding for the entire image in RLE (in hexadecimal) – width, height, and pixels - is:1E|162032602220121F10721AF21092301210326032308250 \W/ \H/ \------------------------------------------PIXELS-----------------------------------------------/

Image Formatting

The images are stored in uncompressed / unencoded format natively. In addition, there are a few other rules to

make the project more tractable:

  1. Images are stored as a list of numbers, with the first two numbers holding image width and height.
  2. Pixels will be represented by a number between 0 and 15 (representing 16 unique colors).
  3. No run may be longer than 15 pixels; if any pixel runs longer, it should be broken into a new run.

For example, the chubby smiley image (Figure 2) would contain the data shown in Figure 3.

Figure 2

Figure 3 – Data for “Chubby Smiley”

NOTE: Students do not need to work with the image file format itself – they only need to work with lists and

encode or decode them. Information about image formatting is to provide context.Requirements

Student programs must present a menu when run in standalone mode and must also implement several methods,

defined below, during this assignment.

Standalone Mode (Menu)

When run as the program driver via the main() method, the program should:

  1. 1) Display welcome message
  2. 2) Display color test (test_rainbow)
  3. 3) Display the menu
  4. 4) Prompt for input

Note: for colors to properly display, it is highly recommended that student

install the “CS1” theme on the project page.

There are five ways to load data into the program that should be provided and four ways the program must be

able to display data to the user.

Loading a File

Accepts a filename from the user and invokes ConsoleGfx.load_file(filename):

Select a Menu Option: 1

Enter name of file to load: testfiles/uga.gfx

Loading the Test Image

Loads ConsoleGfx.test_image:

Select a Menu Option: 2_

Test image data loaded._

Reading RLE String

Reads RLE data from the user in hexadecimal notation with delimiters (smiley example):

Select a Menu Option: 3

Enter an RLE string to be decoded: 28:10:6B:10:10B:10:2B:10:12B:10:2B:10:5B:20:11B:10:6B:10

Reading RLE Hex String

Reads RLE data from the user in hexadecimal notation without delimiters (smiley example):

Select a Menu Option: 4

Enter the hex string holding RLE data: 28106B10AB102B10CB102B105B20BB106B10

Reading Flat Data Hex String

Reads raw (flat) data from the user in hexadecimal notation (smiley example):

Select a Menu Option: 5

Enter the hex string holding flat data:

880bbbbbb0bbbbbbbbbb0bb0bbbbbbbbbbbb0bb0bbbbb00bbbbbbbbbbb0bbbbbb0

Displaying the Image

Displays the current image by invoking the ConsoleGfx.display_image(image_data) method.

Displaying the RLE String

Converts the current data into a human-readable RLE representation (with delimiters):

Select a Menu Option: 7RLE representation: 28:10:6b:10:10b:10:2b:10:12b:10:2b:10:5b:20:11b:10:6b:10

Note that each entry is 2-3 characters; the length is always in decimal, and the value in

hexadecimal! Displaying the RLE Hex Data

Converts the current data into RLE hexadecimal representation (without delimiters):

Select a Menu Option: 8

RLE hex values: 28106b10ab102b10cb102b105b20bb106b10

Displaying the Flat Hex DataDisplays the current raw 代 写COP3502 P2: RLE with Images Python (flat) data in hexadecimal representation (without delimiters):Select a Menu Option: 9 Flat hex values: 880bbbbbb0bbbbbbbbbb0bb0bbbbbbbbbbbb0bb0bbbbb00bbbbbbbbbbb0bbbbbb0

Class Methods Student classes are required to provide all of the following methods with defined behaviors. We recommendompleting them in the following order:

  1. to_hex_string(data)Translates data (RLE or raw) a hexadecimal string (without delimiters). This method can also aid debugging.x: to_hex_string([3, 15, 6, 4]) yields string "3f64".
  1. count_runs(flat_data)Returns number of runs of data in an image data set; double this result for length of encoded (RLE) list.Ex: count_runs([15, 15, 15, 4, 4, 4, 4, 4, 4]) yields integer 2.
  1. encode_rle(flat_data)Returns encoding (in RLE) of the raw data passed in; used to generate RLE representation of a data.

Ex: encode_rle([15, 15, 15, 4, 4, 4, 4, 4, 4]) yields list [3, 15, 6, 4].

  1. get_decoded_length(rle_data)Returns decompressed size RLE data; used to generate flat data from RLE encoding. (Counterpart to #2)Ex: get_decoded_length([3, 15, 6, 4]) yields integer 9.
  1. decode_rle(rle_data)Returns the decoded data set from RLE encoded data. This decompresses RLE data for use. (Inverse of #3)

Ex: decode_rle([3, 15, 6, 4]) yields list [15, 15, 15, 4, 4, 4, 4, 4, 4].

  1. string_to_data(data_string)Translates a string in hexadecimal format into byte data (can be raw or RLE). (Inverse of #1)Ex: string_to_data ("3f64") yields list [3, 15, 6, 4].
  1. to_rle_string(rle_data)

Translates RLE data into a human-readable representation. For each run, in order, it should display the run

length in decimal (1-2 digits); the run value in hexadecimal (1 digit); and a delimiter, ‘:’, between runs. (See

examples in standalone section.)Ex: to_rle_string([15, 15, 6, 4]) yields string "15f:64".

  1. string_to_rle(rle_string)Translates a string in human-readable RLE format (with delimiters) into RLE byte data. (Inverse of #7)Ex: string_to_rle("15f:64") yields list [15, 15, 6, 4].Submissions

NOTE: Your output must match the example output *exactly*. If it does not, you will not receive full credit for your submission!File:Method:rle_program.pySubmit on ZyLabsDo not submit any other files!

Part A (5 points)

For part A of this assignment, students will set up the standalone menu alongside the 4 requirements listed on

page 2 of this document. In addition to this, students should also set up menu options 1 (loading an image), 2(loading specifically the test image), and 6 (displayingwhatever image was loaded) in order to help grasp thebigger picture of the projectThis involves correctly setting up the console_gfx.py file and utilizing its methods. You will useConsoleGfx.display_image(...) to display images. Notice how it takes in a decoded list. This is theformat in which you will locally (in your program) store any image data that you are working with. Whenthe document mentions that something is “loaded” it means that something is stored as a list of flat

(decoded) data.

Part B (60 points)

For part B of this assignment, students will complete the first 6 methods on page 3 of this document. Theymust match specifications and pass test cases on chapter 12.2 in Zybooks, which will be your means ofsubmission for this part of the assignment. Your grade will be the score received on Zybooks. To guaranteefunctionality moving forward to part C, it is expected that you will receive full marks for this section.

Part C (35 points)

For part C of this assignment, students will now complete the final 2 methods on page 3 of this document as wellas the remainder of the project involving the menu options and understanding how all the individual methods areintertwined with each other. You will submit yourwhole program including the 8 methods listed above and themain method in chapter 12.3 in Zybooks. We will only test your remaining 2 methods and the main method inpart C.

标签:P2,10,15,string,RLE,image,Python,data
From: https://www.cnblogs.com/goodlunn/p/18489189

相关文章

  • Python基础学习目录
    Python学习目录Python自动化第一周Python自动化第二周Python文件的操作Python函数的进阶Python装饰器Python函数基础Python深浅copyPython迭代器、生成器Python推导式Python内置函数及匿名函数Python递归及二分查找算法Python面向对象(基础篇)Pytho......
  • 『模拟赛』多校A层冲刺NOIP2024模拟赛11
    Rank考前不挂就是赢A.冒泡排序签,简单的有点格格不入。发现错误代码实质上是将原序列划分成了若干个连通块,并对每个连通块做一遍排序。并查集维护,\(\mathcal{O(n)}\)扫一遍合并连通块,然后按顺序输出即可。复杂度最坏\(\mathcal{O(n\logn)}\)。点击查看代码#include<b......
  • 004 Python数据类型
    1#int可以将纯整数构成的字符串转换成整型,若包含其它非整数符号则会报错2s='123'3res=int(s)4print(res,type(res))56#s='12.3'7#res=int(s)8#print(res,type(s))910#十进制与其它进制之间的相互转换11#十进制转其它进制12print......
  • [Python] Selenium监控网络请求
      Selenium监控网络有两种方式,第一种使用代理,第二张是使用CDP(ChromeDevToolsProtocol)协议,下面直接进入主题分别介绍如何使用代理和CDP协议监控网络请求。  一、使用Selenium-Wire设置代理拦截处理请求。  Selenium-Wire是基于Selenium开发的抓包工具,基本使用方式如下:fr......
  • python第六章课后习题
    点击查看代码print("学号:2023310143028")点击查看代码defprim(graph,start):num_nodes=len(graph)visited=[False]*num_nodesmin_heap=[(0,start,-1)]mst_cost=0mst_edges=[]whilemin_heap:......
  • Python 数据分析与可视化有什么区别
    在当今的数据驱动时代,Python已成为数据分析和数据可视化的重要工具。尽管这两个领域经常在数据科学项目中相互交织,但它们在功能和目的上存在本质区别。本文旨在详细探讨Python在数据分析和数据可视化方面的差异,包括它们的定义、使用的主要库、应用场景以及在实际项目中的作用。通......
  • python第四章课后习题
    点击查看代码importnumpyasnpimportcvxpyascpx=cp.Variable(6,pos=True)obj=cp.Minimize(x[5])a1=np.array([0.025,0.015,0.055,0.026])a2=np.array([0.05,0.27,0.19,0.185,0.185])a3=np.array([1,1.01,1.02,1.045,1.065])k=0.05;kk=[];qq=[]whil......
  • 多校A层冲刺NOIP2024模拟赛11
    多校A层冲刺NOIP2024模拟赛11\(T1\)A.冒泡排序\(100pts/100pts/100pts\)将循环\(j\)提到外面,本质上是对\(a_{j},a_{j+k},a_{j+2k},\dots,a_{j+xk}\)进行排序迭代的过程。按下标模\(k\)的余数分别排序即可。点击查看代码inta[1000010];vector<int>b[1000......
  • Python教程:Python父类方法重写
    在Python中,子类可以通过定义与父类同名的方法来重写(Override)父类的方法。这种机制允许子类提供特定的实现,以替代从父类继承的通用实现。重写父类方法时,子类方法将覆盖父类方法的行为,但子类仍然可以访问父类方法的原始实现(如果需要的话)。以下是一个简单的示例,展示了如何在Py......
  • 洛谷P2596 [ZJOI2006] 书架 题解 splay tree 模板题
    题目链接:https://www.luogu.com.cn/problem/P2596主要涉及的操作就是:找到某一个编号的点(这个操作可以不用splaytree维护)删除某个点将某一个点插入到最前面,最后面,或者某一个位置查询前序遍历为\(k\)的节点编号因为每次删除都会又把这个点加回去,所以可以复用\(n\)个......