質問編集履歴

4

内容を追加しました

2022/12/15 06:43

投稿

chibineko
chibineko

スコア0

test CHANGED
File without changes
test CHANGED
@@ -259,6 +259,9 @@
259
259
 
260
260
  AttributeError: 'list' object has no attribute 'size'
261
261
  ```
262
+
263
+ image_file フォルダにあるjpgファイルをすべてrgbにコンバートしたい場合
264
+ for文でどのようにコードをおこせばいいでしょうか
262
265
  よろしくお願いします
263
266
 
264
267
 

3

#をつけた不要コードを削除しました

2022/12/15 01:03

投稿

chibineko
chibineko

スコア0

test CHANGED
File without changes
test CHANGED
@@ -202,27 +202,26 @@
202
202
 
203
203
  # 画像ファイルのパス
204
204
  image_path = glob.glob(r"C:\temp\PdfToImage\image_file\*.jpg")
205
- print("image_path",image_path)
206
205
  gazo = glob.glob(r"C:\temp\PdfToImage\gazo_file\*.jpg")
206
+
207
+ files = []
208
+ for i, page in enumerate(pages):
209
+ img_rgb = page.convert("RGB")
210
+ pixels = img_rgb.load()
207
211
 
208
- print("ここまでは動いている")
209
- files = []
210
-
211
- #for i, file in enumerate(files):
212
+ for i, file in enumerate(files):
212
- # img_rgb = files.convert("RGB")
213
+ img_rgb = files.convert("RGB")
213
- # print("ここまでは動いている",files)
214
-
215
- # pixels = img_rgb.load()
214
+   pixels = img_rgb.load()
216
- # print(img_rgb)
215
+
217
216
  # 原稿画像加工(黒っぽい色以外は白=255,255,255にする)
218
- c_max = 168
217
+    c_max = 168
219
- for j in range(image_path.size[1]):
218
+    for j in range(image_path.size[1]):
220
- for k in range(image_path.size[0]):
219
+    for k in range(image_path.size[0]):
221
- if (pixels[k, j][0] > c_max and pixels[k, j][1] > c_max and pixels[k, j][2] > c_max):
220
+     if (pixels[k, j][0] > c_max and pixels[k, j][1] > c_max and pixels[k, j][2] > c_max):
222
- image_pathimage_path.putpixel((k,j), (255, 255, 255))
221
+     image_pathimage_path.putpixel((k,j), (255, 255, 255))
223
- file_name = f'{image_path.stem}_{i + 1}.jpg'
222
+     file_name = f'{image_path.stem}_{i + 1}.jpg'
224
-
223
+
225
- files.append(fr'{image_dir}/{file_name}')
224
+    files.append(fr'{image_dir}/{file_name}')
226
225
  # jpegで保存
227
226
  image_path.save(str(files[i]), "jpg")
228
227
  path = r"C:\temp\PdfToImage\image_file"
@@ -241,10 +240,10 @@
241
240
    :
242
241
 
243
242
  以下元コードと同じ
244
-
243
+ ```
245
244
 
246
245
  エラーは以下のものです
247
-
246
+ ```エラー
248
247
  AttributeError Traceback (most recent call last)
249
248
  Cell In [1], line 40
250
249
  32 #for i, file in enumerate(files):

2

```をコードに追加しました

2022/12/15 00:31

投稿

chibineko
chibineko

スコア0

test CHANGED
File without changes
test CHANGED
@@ -174,9 +174,9 @@
174
174
  fp.close()
175
175
 
176
176
  print("処理を終了")
177
-
177
+ ```
178
178
  =========修正したコード(jpeg⇒テキスト)========
179
-
179
+ ```python
180
180
  import os
181
181
  from PIL import Image
182
182
  from pathlib import Path

1

コードの上に```追加しました

2022/12/15 00:28

投稿

chibineko
chibineko

スコア0

test CHANGED
File without changes
test CHANGED
@@ -5,6 +5,8 @@
5
5
  元のコードから修正しましたがうまくいきません
6
6
 
7
7
  元ソース(pdf⇒jpeg⇒テキスト)
8
+
9
+ ```python
8
10
  import os
9
11
  from PIL import Image
10
12
  from pathlib import Path
@@ -257,7 +259,7 @@
257
259
  42 if (pixels[k, j][0] > c_max and pixels[k, j][1] > c_max and pixels[k, j][2] > c_max):
258
260
 
259
261
  AttributeError: 'list' object has no attribute 'size'
260
-
262
+ ```
261
263
  よろしくお願いします
262
264
 
263
265