import json
import os
from PIL import Image, ImageDraw
import cv2
import numpy as np
def cv_imread(file_path):
cv_img = cv2.imdecode(np.fromfile(file_path, dtype=np.uint8), -1)
return cv_img
def cv_im_write(savePath, tem):
cv2.imencode('.png', tem)[1].tofile(savePath)
def change_black_to_transparent(image_path, output_path):
image = cv_imread(image_path)
rgba_image = cv2.cvtColor(image, cv2.COLOR_BGR2RGBA)
mask = np.zeros(rgba_image.shape[:2], dtype=np.uint8)
mask[(rgba_image[:, :, 0] < 10) & (rgba_image[:, :, 1] < 10)] = 255
rgba_image[:, :, 3] = mask
cv_im_write(output_path, rgba_image)
def change_black_to_white(image_path, output_path):
image = Image.open(image_path)
width, height = image.size
for x in range(width):
for y in range(height):
pixel = image.getpixel((x, y))
if pixel == (0, 0, 0):
image.putpixel((x, y), (230, 230, 230))
image.save(output_path)
def crop_img_from_source_img(source_img, out_put_img, polygon_points):
image1 = Image.open(source_img)
image2 = Image.new('RGB', (image1.size), (255, 255, 255))
mask = Image.new("L", image1.size, 0)
draw = ImageDraw.Draw(mask)
for polygon_point in polygon_points:
draw.polygon(polygon_point, fill=255)
cropped_image = Image.new("RGB", image1.size)
cropped_image.paste(image1, mask=mask)
image2.paste(cropped_image, (0, 0))
image2.save(out_put_img)
change_black_to_transparent(out_put_img, out_put_img)
change_black_to_white(out_put_img, out_put_img)
def pil_todo(img_path, points, _dst):
image = Image.open(img_path)
white_background = Image.new('RGB', image.size, (255, 255, 255))
for left, upper, right, lower in points:
width, height = right - left, lower - upper
region = Image.new('RGB', (width, height), (255, 255, 255))
region.paste(image.crop((left, upper, right, lower)), (0, 0))
white_background.paste(region, (left, upper))
white_background.save(_dst)
def cv_to_fill_color(img_path, rectangles, output_path):
import cv2
import numpy as np
image = cv_imread(img_path)
if image.dtype != np.uint8:
image = cv2.convertScaleAbs(image)
white_image = np.ones(image.shape, dtype=np.uint8) * 255
for rect in rectangles:
print(rect)
x, y, w, h = rect
cv2.rectangle(white_image, (x, y), (x + w, y + h), (255, 255, 255), cv2.FILLED)
mask = np.zeros(image.shape, dtype=np.uint8)
if mask is not None and mask.shape != image.shape:
mask = cv2.resize(mask, (image.shape[1], image.shape[0]))
for rect in rectangles:
print(rect)
x, y, w, h = rect
cv2.rectangle(mask, (x, y), (x + w, y + h), (255, 255, 255), cv2.FILLED)
result = cv2.bitwise_and(image, image, mask=mask)
result = cv2.bitwise_or(result, white_image)
cv_im_write(output_path, result)
path = r"C:\Users\DM\Desktop\test\a\01"
save_dir = r"C:\Users\DM\Desktop\test\a\03"
for item in os.listdir(path):
t = os.path.join(path, item)
if t.split(".")[-1] == "json":
with open(t, "r", encoding="utf-8") as f:
file_data = json.loads(f.read())
shapes = file_data.get("shapes")
points = []
for it in shapes:
_point = it.get("points")
_label = it.get("label")
if "$" not in _label:
continue
temp_list = []
print(_point)
polygon_points = np.array(_point, np.int32)
x, y, w, h = cv2.boundingRect(polygon_points)
print("boundingRect", x, y, w, h)
points.append((x, y, x + w, y + h))
img_path = t.replace(".json", ".png")
_dst = t.replace(path, save_dir).replace(".json", ".png")
pil_todo(img_path, points, _dst)