[Roller must read] LoveLive! Essential Skills: Python Pillow automatic processing, llerlovelive
Cause
Listen quietly to favorite songs. Those who like them can watch them far away. 30 days ago, it was March 14. I couldn't help but approached LoveLive! School idol festival, this is my first card (SEE ). The next day is July 22, March 15.
While (true) {open with PS; add guides; crop area; rotate positive image; sit, no, save ;}
No figure. You can say that !!!
Who said I didn't have a picture:
Def get_count_of_star (image): count = 0 positon_of_star = (33,109 0) # The initial position of the star, but the star is moving for I in range) stars have a maximum of 8 color = image. getpixel (positon_of_star) # Is the stars shining? Positon_of_star = (33, positon_of_star [1] + 55) # Move the coordinates down to the next star if color [2]! = 34: # only compare the third element count + = 1 # It is not dark, indicating that the star is a valid return count
The parameter image is an opened image and returns the number of bright stars.
4. How do I determine the level in the upper left corner?
In addition, because I do not recognize texts, the level characters in the upper left corner cannot be obtained, but it does not mean that this problem cannot be solved.
Looking at a large number of cards, we found a shortcut to the level, which is that the level corresponds to the number of stars:
Level (upper left corner) |
Number of Stars (lower left) |
N |
1, 2 |
R |
3, 4 |
SR |
5, 6 |
UR |
7, 8 |
Sometimes you only need to change the angle of thinking to solve the problem.
Code Design
(1) Working path
Import osos. getcwd () # view the current working directory OS. chdir ("/home/luoxu") # change the directory for root, dirs, files in OS. walk (dir): for name in files: print OS. path. join (root, name)
OS. listdir can list all files and directories in dir, but does not include contents in subdirectories.
OS. walk can traverse all the following directories, including subdirectories, return paths, directory tuples under the path, and the parent of the file.
(2) Obtain png image files under a directory
Glob is a python file operation module that can be used to find matching files.
import globglob.glob('/home/luoxu/*.png')
The parameter is a path string. The string can be an absolute or relative path, and the last part of the string indicates the matching file type.
*,? , [] Three wildcards, * represents 0 or multiple characters ,? Represents a character. [] matches a character in the specified range, for example, [0-9] matches a number.
This method returns the list of all matched file paths.
(3) Image Processing
Image processing uses the Pillow module, the official website has manual http://effbot.org/zone/pil-index.htm
From PIL import Imageim = Image. open ("lenna.jpg") # open the image box = (200, 0, 1720,108 0) im. crop (box) # crop the region im according to the diagonal coordinate of the box. transpose (Image. ROTATE_90) rotate against the time point to rotate 90 degrees im.save('newfilename.png ', "PNG") Save the new image file newfilename.png color = I. getpixel () # obtain the rgb value of a pixel
The script only uses these methods. For more details, read the document.
(4) description of some variables
box = (200, 0, 1720, 1080)
The source image is 1920x1080, And the cropped image is 1520x1080, diagonal (, 0)
POSITION = (920,1400)
The attribute used in the lower right corner is determined. The getpixel (POSITION) method provides three values:
attributes = {'Smile':(234,0,115,255), 'Pure' :(34,170,85,255), 'Cool' :(0,153,238,255) }
Ing table of stars and levels
grade = {1:'N', 2:'N', 3:'R', 4:'R', 5:'SR', 6:'SR', 7:'UR', 8:'UR' }
(5) stars
Go back and read the relevant sections above.
(6) Additional
I will provide two more images. If the reader is willing to use the test code, the difference is that there is no need to rotate and the cropping area is different.
1 #! /Usr/bin/env python3.5 2 #-*-coding: UTF-8-*-3 4 from PIL import Image 5 import glob, OS 6 7 box = (200, 0, 1720,108 0) # crop area 8 POSITION = (115,255) # Attribute coordinate 9 10 # Attribute-color ing 11 attributes = {'smile ), 12 'pure ':( 34,170, 85,255), 13 'cool' :( 0,153,238,255) 14} 15 # star number-level ing 16 grade = {1: 'n', 2: 'N', 17 3: 'R', 4: 'R', 18 5: 'sr', 6: 'sr', 19 7: 'ur ', 8: 'ur '20} 21 22 def image_processing (dirpath, dir Name): 23 OS. chdir (dirpath + '/' + dirname) 24 # print (dirpath + dirname) 25 s_cnt = p_cnt = c_cnt = 126 images = glob. glob ('*. png ') # obtain all png files 27 for image in images: 28 try: 29 im = Image. open (image) 30 new_im = im. crop (box ). transpose (Image. ROTATE_90) # crop and rotate 31 # filename, ext = OS. path. splitext (image) 32 # new_im.save (filename + "-new.png", "PNG") 33 color = new_im.getpixel (POSITION) # Attribute coordinate-> color tuples 34 cou Nt = get_count_of_star (new_im) # judge the number of stars 35 if color = attributes ['smile ']: # confirm that the color corresponds to the Smile attribute 36 new_im.save (dirname + "counter ". format (grade = grade [count], count = count, number = s_cnt), "PNG") 37 s_cnt ++ = 138 elif color = attributes ['pure ']: 39 new_im.save (dirname + "_pure_{grade1__{count}-{number#.png ". format (grade = grade [count], count = count, number = p_cnt), "PNG") 40 p_cnt + = 141 eli F color = attributes ['cool ']: 42 new_im.save (dirname + "_cool_1_grade1__{count}-{number#.png ". format (grade = grade [count], count = count, number = c_cnt), "PNG") 43 c_cnt ++ = 144 else: 45 new_im.save (dirname + "-new.png ", "PNG") 46 47 bytes t IOError: 48 print ('{}: io error! '. Format (image) 49 50 print (dirname + ': finished. ') 51 52 53 def get_count_of_star (image): 54 count = 055 positon_of_star = (33,109 0) # initial position of the star, however, the stars are 56 for I in range (0, 8): # [) the stars have a maximum of 8 57 color = image. getpixel (positon_of_star) # Is the stars shining? 58 positon_of_star = (33, positon_of_star [1] + 55) # Move the coordinates down to the next star 59 if color [2]! = 34: # Compare only the third element 60 count + = 1 # It is not dark, it indicates that stars are valid 61 return count62 63 64 if _ name _ = '_ main _': 65 for dirpath, dirnames, filenames in OS. walk (OS. getcwd (): 66 for dirname in dirnames: 67 # path = dirpath + dirname68 image_processing (dirpath, dirname)
Running result
Prepare before running, and put the folder to be processed:
After the script is executed, we can get the desired result:
From the above results, we can see that there are still some problems in the script: 1. It is used to compare the values of the color attribute (lower right corner) Too dead, and some pixel RGB values are a little different, the results cannot be identified, as I mentioned previously; 2. if you are not sure about Uniform Naming, the name-new causes the overwrite problem.
I also made dynamic images specially:
Pythonner Solution
I am not a Pythonner. I don't know how thinking is.
But I know how to do it, that is, do nothing, don't care about those black borders, huh, huh.
Last
Last time I posted my desktop, I heard that PyCharm and LoveLive are raining! More configuration!
Don't read it any more. My sister is here (I don't understand what the Japanese have invented emoji, but I don't know how to include LoveLive! )