chohj06ms commited on
Commit
27fae9d
1 Parent(s): 1b60bb3

Delete edimg.py

Browse files
Files changed (1) hide show
  1. edimg.py +0 -234
edimg.py DELETED
@@ -1,234 +0,0 @@
1
- from PIL import Image, ImageDraw, ImageFont, ImageColor, ImageFilter, ImageSequence
2
- import numpy as np
3
- import qrcode
4
-
5
- def paste_img(img, img2, coordinate):
6
- if not isinstance(img, Image.Image):
7
- img = Image.open(img).convert("RGBA")
8
-
9
- if not isinstance(img2, Image.Image):
10
- img2 = Image.open(img2).convert("RGBA")
11
-
12
- img.paste(img2, coordinate, img2)
13
-
14
- return img
15
-
16
- def color(img, color):
17
- if not isinstance(img, Image.Image):
18
- img = Image.open(img).convert("RGBA")
19
- image_np = np.array(img)
20
- _, _, _, alpha = image_np.T
21
- mask = alpha > 0
22
- image_np[..., :-1][mask.T] = ImageColor.getcolor(color, "RGB")
23
- img = Image.fromarray(image_np)
24
- return img
25
-
26
- def txt(text, r, g, b, font, angle, rl, fonts = "font/Helvetica_Neue_LT_Std_75_Bold.otf", image_size = (400, 100), opacity = 255):
27
- # 이미지 크기와 글꼴 크기 설정
28
- font_size = font
29
-
30
- # 이미지와 드로잉 객체 생성
31
- image = Image.new('RGBA', image_size, (255, 255, 255, 0)) # 투명한 배경
32
- draw = ImageDraw.Draw(image)
33
-
34
- # 글꼴 설정
35
- font = ImageFont.truetype(fonts, font_size)
36
- text_color = (r, g, b, opacity)
37
-
38
- # 텍스트 그리기
39
- text_bbox = draw.textbbox((0, 0), text, font=font)
40
- text_width = text_bbox[2] - text_bbox[0]
41
- text_height = text_bbox[3] - text_bbox[1]
42
- match rl:
43
- case "r":
44
- text_x = image_size[0] - text_width # 오른쪽 정렬을 위해 수정
45
- text_y = (image_size[1] - text_height) / 2
46
- draw.text((text_x, text_y), text, fill=text_color, font=font)
47
- case "l":
48
- # 텍스트를 왼쪽으로 정렬하여 그리기
49
- text_x = 0 # 왼쪽 맞춤
50
- text_y = (image_size[1] - text_height) / 2
51
- draw.text((text_x, text_y), text, fill=text_color, font=font)
52
- # 이미지 회전
53
- if angle != 0:
54
- image = image.rotate(angle, expand=True)
55
- return image
56
-
57
-
58
-
59
-
60
- def serial(text, r, g, b, font_size):
61
- # 이미지 크기와 글꼴 크기 설정
62
- text = "#"+ text
63
- image_size = (400, 100) # 너비 x 높이
64
- fonts = "font/MatrixSSK Regular.ttf"
65
- spacing = 43 # 글자 간 간격
66
-
67
- # 이미지와 드로잉 객체 생성
68
- image = Image.new('RGBA', image_size, (255, 255, 255, 0)) # 투명한 배경
69
- draw = ImageDraw.Draw(image)
70
-
71
- # 글꼴 설정
72
- font = ImageFont.truetype(fonts, font_size)
73
- text_color = (r, g, b)
74
-
75
- # 텍스트 위치 초기화
76
- current_x = 0
77
- text_y = (image_size[1] - font_size) // 2 # 세로 중앙 정렬
78
-
79
- # 각 글자를 개별적으로 그리기
80
- for char in text:
81
- draw.text((current_x, text_y), char, fill=text_color, font=font)
82
- current_x += spacing
83
-
84
- # 이미지 회전
85
- rotated_image = image.rotate(270, expand=True)
86
-
87
- return rotated_image
88
-
89
-
90
- remove_image_path = f'resource/ai_remove.png' # Adjust the path according to your setup
91
- remove_img = Image.open(remove_image_path).convert("RGBA")
92
- remove_np = np.array(remove_img)
93
-
94
- # remove.png에서 r 채널이 255이고 알파 채널이 있는 픽셀 위치 찾기
95
- r_channel = remove_np[:, :, 0]
96
- alpha_channel = remove_np[:, :, 3]
97
- mask = (r_channel == 255) & (alpha_channel > 0)
98
-
99
-
100
- def color_ai(num):
101
- grimg = Image.open(f'grad2/gradient_{num}.png')
102
- # resized_image를 RGBA 형식으로 변환 (필요한 경우에만)
103
- grimg = grimg.convert("RGBA")
104
- grimg_np = np.array(grimg)
105
-
106
- # 해당 위치의 resized_image 픽셀을 투명하게 설정
107
- grimg_np[mask, :] = (0, 0, 0, 0) # RGBA 형식에 맞게 조정
108
-
109
- grimg = Image.fromarray(grimg_np)
110
- return grimg
111
-
112
- def color_ai_back(num):
113
- file_path = f'grad2/gradient_{num}.png'
114
- img = Image.open(file_path)
115
- # 잘라낼 부분 설정 (x < 126에 해당하는 부분)
116
- left = 125
117
- top = 0
118
- right = 1289 # x가 126보다 작은 부분까지
119
- bottom = 1800
120
-
121
- # 이미지 자르기
122
- cropped_img = img.crop((left, top, right, bottom))
123
- return cropped_img
124
-
125
-
126
- remove_image_path_rounded = 'resource/remove.png' # Adjust the path according to your setup
127
- remove_img_rounded = Image.open(remove_image_path_rounded).convert("RGBA")
128
- remove_np_rounded = np.array(remove_img_rounded)
129
-
130
- # remove.png에서 r 채널이 255이고 알파 채널이 있는 픽셀 위치 찾기
131
-
132
- r_channel_rounded = remove_np_rounded[:, :, 0]
133
- alpha_channel_rounded = remove_np_rounded[:, :, 3]
134
- mask_rounded = (r_channel_rounded == 255) & (alpha_channel_rounded > 0)
135
- def rounded(img):
136
- img = img.convert("RGBA")
137
- img_np = np.array(img)
138
-
139
- # 해당 위치의 resized_image 픽셀을 투명하게 설정
140
- img_np[mask_rounded, :] = (0, 0, 0, 0) # RGBA 형식에 맞게 조정
141
-
142
- img = Image.fromarray(img_np)
143
- return img
144
-
145
-
146
-
147
- def resize_img(image, target_width, target_height):
148
- if not isinstance(image, Image.Image):
149
- img = Image.open(image).convert("RGBA")
150
-
151
- else:
152
- img = image
153
- # 원본 이미지의 크기와 비율 계산
154
- original_width, original_height = img.size
155
- ratio = min(original_width / target_width, original_height / target_height)
156
-
157
- # 새로운 크기 계산
158
- new_width = int(target_width * ratio)
159
- new_height = int(target_height * ratio)
160
-
161
- # 이미지를 중앙에서 자름
162
- img = img.crop(((original_width - new_width) // 2,
163
- (original_height - new_height) // 2,
164
- (original_width + new_width) // 2,
165
- (original_height + new_height) // 2))
166
-
167
- # 최종적으로 목표 해상도로 리사이즈
168
- img = img.resize((target_width, target_height), Image.Resampling.LANCZOS)
169
-
170
- return img
171
-
172
-
173
- def calculate_size(text, font_size, font_path):
174
- # Create a dummy image just to create a draw object
175
- dummy_image = Image.new('RGB', (1100, 136))
176
- draw = ImageDraw.Draw(dummy_image)
177
-
178
- # Load the font
179
- font = ImageFont.truetype(font_path, font_size)
180
-
181
- # Calculate text width using the defined draw object
182
- text_width = draw.textlength(text, font=font)
183
-
184
- return int(text_width)
185
-
186
-
187
-
188
- def qr(url):
189
- # Generate QR code with a smaller border
190
- qr = qrcode.QRCode(
191
- version=1,
192
- error_correction=qrcode.constants.ERROR_CORRECT_H,
193
- box_size=10,
194
- border=2, # Reduce the border by the border_reduction amount
195
- )
196
- qr.add_data(url)
197
- qr.make(fit=True)
198
-
199
- # Create an image from the QR Code instance
200
- qr_img = qr.make_image(fill_color="black", back_color="white").convert('RGB')
201
-
202
- qr_img = qr_img.resize((335,335), Image.Resampling.LANCZOS)
203
-
204
- return qr_img
205
-
206
-
207
- def qr_icon(icon_path):
208
- img_icon = Image.open(icon_path)
209
- img_icon = resize_img(img_icon, 84,84)
210
- return img_icon
211
-
212
-
213
-
214
- def makegif(gif, back):
215
- # 결과 프레임들을 저장할 리스트
216
- frames = []
217
- durations = []
218
- x, y = gif.size
219
-
220
-
221
- # GIF의 각 프레임을 순회하며 처리합니다.
222
- for frame in ImageSequence.Iterator(gif):
223
- durations.append(frame.info['duration'])
224
- img = Image.new("RGB", (2 * x, y))
225
- img.paste(frame, (0,0))
226
-
227
- # 두 이미지를 합칩니다.
228
- img.paste(back, (x,0))
229
-
230
- # 결과 리스트에 추가합니다.
231
- frames.append(img)
232
-
233
- # 모든 프레임을 처리한 후, 새로운 GIF로 저장합니다.
234
- frames[1].save(f'exgif.gif', save_all=True, append_images=frames[1:], loop=0, duration=durations, disposal=0, optimize=True)