Refactor download_image function and add print
statement for image OCR results
This commit is contained in:
		
							
								
								
									
										5
									
								
								main.py
									
									
									
									
									
								
							
							
						
						
									
										5
									
								
								main.py
									
									
									
									
									
								
							@@ -11,7 +11,7 @@ config = dotenv_values(".env")
 | 
				
			|||||||
ImageFile.LOAD_TRUNCATED_IMAGES = True
 | 
					ImageFile.LOAD_TRUNCATED_IMAGES = True
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# 下载图片(使用OSS下载)
 | 
					# 下载图片(使用OSS下载)
 | 
				
			||||||
def download_image(url:str) -> Image:
 | 
					def download_image(url:str) -> Image.Image:
 | 
				
			||||||
    if url.startswith('http://image.gameuiux.cn/') or url.startswith('https://image.gameuiux.cn/'):
 | 
					    if url.startswith('http://image.gameuiux.cn/') or url.startswith('https://image.gameuiux.cn/'):
 | 
				
			||||||
        try:
 | 
					        try:
 | 
				
			||||||
            url = url.replace('http://image.gameuiux.cn/', '').replace('https://image.gameuiux.cn/', '')
 | 
					            url = url.replace('http://image.gameuiux.cn/', '').replace('https://image.gameuiux.cn/', '')
 | 
				
			||||||
@@ -42,7 +42,6 @@ cursor.execute("SELECT id, content FROM web_images LIMIT 5")
 | 
				
			|||||||
# 获取查询结果
 | 
					# 获取查询结果
 | 
				
			||||||
rows = cursor.fetchall()
 | 
					rows = cursor.fetchall()
 | 
				
			||||||
for row in rows:
 | 
					for row in rows:
 | 
				
			||||||
    #print(row)
 | 
					 | 
				
			||||||
    image = download_image(row['content'])
 | 
					    image = download_image(row['content'])
 | 
				
			||||||
    if image is None:
 | 
					    if image is None:
 | 
				
			||||||
        print('图片下载失败,跳过')
 | 
					        print('图片下载失败,跳过')
 | 
				
			||||||
@@ -50,7 +49,7 @@ for row in rows:
 | 
				
			|||||||
    out = ocr.ocr(image)
 | 
					    out = ocr.ocr(image)
 | 
				
			||||||
    # 这段代码将只包含那些非空、不是纯数字且长度大于1的'text'值
 | 
					    # 这段代码将只包含那些非空、不是纯数字且长度大于1的'text'值
 | 
				
			||||||
    texts = [item['text'] for item in out if item['text'] and not item['text'].isdigit() and len(item['text']) > 1]
 | 
					    texts = [item['text'] for item in out if item['text'] and not item['text'].isdigit() and len(item['text']) > 1]
 | 
				
			||||||
    print(texts)
 | 
					    print(row['id'], texts)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# 关闭游标和连接
 | 
					# 关闭游标和连接
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user