20211202 "Python Programming" Experiment 4 Python Comprehensive Practice Experiment Report

20211202 "Python Programming" Experiment 4 Python Comprehensive Practice Experiment Report

 

Course: "Python Programming"
Class: 2112
Name: Wang Jinshuo
Student ID: 20211202
Experimental teacher: Wang Zhiqiang
Experiment Date: May 24, 2022
Compulsory/Elective: Public Elective

 

1. Experimental content

 

1. Inspiration:

       One is that when watching a video, there will be special effects that the photos are composed of near and far into a large picture. .

 

2. Main content:

Spell out "Python" from pictures of school activities and print them out.

 

 

2. Experimental process and results

  

1. Experiment content

Python comprehensive application: crawler, data processing, visualization, machine learning, neural network, game, network security, etc.

What I do is a photo wall

 

2. Experimental process and results

Determine text and font size

 

Refer to the pygame module initialization to determine the font

 

Rendering fonts, aliasing and color determination

 

 

Get the width and height of the font

 

Get pixel color

 

According to pixel classification, make a two-dimensional list: add 1 to black, and add 0 to white.

Refer to the image processing module to create a picture

 

Initialize the image size according to 0, 1 texture.

Use the os module to obtain pictures, use the random module to randomly select, and modify the size of the pictures.

 

Strong image preservation

 

 

Experimental code:

import pygame

text = '233'
font_size = 10

pygame.init()
font = pygame.font.Font('simhei.ttf',font_size)
print(font)

font_text = font.render(text, True, (0, 0, 0,),(255, 255, 255))
print(font_text)

height = font_text.get_height()
width = font_text.get_width()
print('height: ',height)
print('width: ',width)

print(font_text.get_at((1,1))[0])

image_row_list = []
for x in  range(height):
    image_col_list = []
    for y in range(width):
        if font_text.get_at((y,x))[0] != 255:
            image_col_list.append(1)
        else:
            image_col_list.append(0)
        image_row_list.append(image_col_list)
    for row in image_row_list:
        print(row)

from PIL import Image

width_len = len(image_row_list[0])
height_len = len(image_row_list)

new_image = Image.new('RGB', (width_len * 100 , height_len * 100 ),(255,255,255))

import os
import random

img_size = 100

for row in range(height_len):
    for clo in range(width_len):
        if image_row_list[row][clo] == 1 :
            source_image = Image.open('images\\' + random.choice(os.listdir(r'images')))
            source_image = source_image.resize((img_size,img_size), Image.Resampling.LANCZOS)
            new_image.paste(source_image,(clo * img_size, row * img_size))


print('generating...')

new_image.save(text +'.jpg')

print('After saving, please search under the current file item')

 

Upload ESC server

 

 

 

 

 

 

 

 

 

 

 

 

 

 

3. Problems encountered during the experiment and the solution process

Problem 1: Unable to install pygame and pillow

Solution: Turn off the Chinese patch

Problem 2: The aspect ratio of the output text is out of balance

Solution: Check the information and ask the teacher to perform single-step debugging and modify the for loop

Problem 3: Unable to survive and save pictures

Solution: reduce image size and image size, replace text, replace font

Problem 4: ESC server cannot download pygame and pillow ( pip install xxxxx )

Solution: CSDN search, update pip. (CSDN yyds)

  ........................

(It’s really a lot of problems to do it. It’s a headache to change the code and check the data, but it’s really great to be able to finish it!)

 

4. Others (perception, thinking, etc.)

The python class is over, but my feeling about python is the same as the teacher's piece of code, "Life is too short, I use python".

 

The first time I came into programming was c++, which I learned in high school, and I became interested in programming. Teacher Wang took me to learn python, and taught me a lot of things, including a lot of materials and learning methods, which made me feel the beauty of different languages, and python is really very convenient to use. Of course, Mr. Wang took me into the hall of python, and we still have to rely on ourselves to learn more. Teachers can often achieve the purpose of learning through some examples in class. They not only learned the knowledge, but also completed the actual operation code. I like this method very much and the effect is also very good.

I am very happy to have chosen the course of python. Although I still do not fully understand some knowledge, there is no end to my learning. I hope I can maintain my passion for python, and I hope that I can exchange professional knowledge with teachers in the future.

 

Posted by Mhz2020 on Wed, 25 May 2022 17:54:30 +0300