Posts: 15
Threads: 5
Joined: Jun 2020
https://pastebin.com/z82Pv9Wm
Hello to all I hope you are all well.
I have not a super level in Python, surviving barely.
My program was working normally and I must have done something wrong,
the cutting is working without problem, its screenshots from Imac.
but the report on csv does not send data, I have always a page with only the titles, no data.
Everything seams normal???
argh!!!
Posts: 8,158
Threads: 160
Joined: Sep 2016
Jul-08-2021, 07:07 AM
(This post was last modified: Jul-08-2021, 07:07 AM by buran.)
You could not possibly expect that we will examine 556 lines of code to identify your problem.
Please, create minimal, complete and reproducible example that demonstrate your problem, then paste it here in python tags.
For start there is bunch of identical functions, that should be refactored
Posts: 15
Threads: 5
Joined: Jun 2020
(Jul-08-2021, 07:07 AM)buran Wrote: You could not possibly expect that we will examine 556 lines of code to identify your problem.
Please, create minimal, complete and reproducible example that demonstrate your problem, then paste it here in python tags.
For start there is bunch of identical functions, that should be refactored
Yes agreed,
I agree and I just realized my mistake.
The program work as a charm.
It have no error.
And I will think about your remark.
Again, thanks for you taking the time and giving a Franck advice.
For refactoring, I would love to,
yes.
I don't really know how.
Posts: 8,158
Threads: 160
Joined: Sep 2016
Jul-08-2021, 08:22 AM
(This post was last modified: Jul-08-2021, 08:23 AM by buran.)
Not tested, but basically, your program is this:
I didn't look into height/crop_areas mapping - there might be better approach, but even this is better than yours.
import os
from PIL import Image
import pandas as pd
CROPS = {4083:((656, 285, 1120, 730), (656, 845, 1545, 1272), (656, 1292, 1545, 1720), (656, 3120, 1545, 3555), (656, 3580, 1545, 3925)),
8166:((656, 285, 1120, 730), (656, 845, 1545, 1272), (656, 1292, 1545, 1720), (656, 2260, 1545, 2635), (656, 3120, 1545, 3460)),
3995:((656, 285, 1120, 730), (656, 845, 1545, 1270), (656, 1292, 1540, 1715), (656, 3115, 1545, 3560), (656, 3580, 1545, 3925)),
3996:((656, 285, 1120, 730), (656, 845, 1545, 1270), (656, 1292, 1540, 1715), (656, 3115, 1545, 3560), (656, 3580, 1545, 3925)),
3536:((656, 285, 1120, 730), (656, 845, 1545, 1270), (656, 1292, 1540, 1715), (656, 2655, 1545, 3100), (656, 3120, 1545, 3465)),
3537:((656, 285, 1120, 730), (656, 845, 1545, 1270), (656, 1292, 1540, 1715), (656, 2655, 1545, 3100), (656, 3120, 1545, 3465)),
3872:((656, 285, 1120, 730), (656, 845, 1545, 1270), (656, 1292, 1540, 1715), (656, 2995, 1545, 3430), (656, 3460, 1545, 3800)),
3873:((656, 285, 1120, 730), (656, 845, 1545, 1270), (656, 1292, 1540, 1715), (656, 2995, 1545, 3430), (656, 3460, 1545, 3800)),
4552:((656, 285, 1120, 730), (656, 845, 1545, 1270), (656, 1292, 1540, 1715), (656, 3675, 1545, 4115), (656, 4140, 1545, 4480)),
4312:((656, 285, 1120, 730), (656, 845, 1545, 1270), (656, 1292, 1540, 1715), (656, 3435, 1545, 3875), (656, 3900, 1545, 4240)),
4189:((656, 285, 1120, 730), (656, 845, 1545, 1270), (656, 1292, 1540, 1715), (656, 3310, 1545, 3750), (656, 3775, 1545, 4120)),
4432:((656, 285, 1120, 730), (656, 845, 1545, 1270), (656, 1292, 1540, 1715), (656, 3552, 1545, 3990), (656, 4015, 1545, 4360)),
4352:((656, 285, 1120, 730), (656, 845, 1545, 1270), (656, 1292, 1540, 1715), (656, 3475, 1545, 3915), (656, 3935, 1545, 4280)),
4472:((656, 285, 1120, 730), (656, 845, 1545, 1270), (656, 1292, 1540, 1715), (656, 3595, 1545, 4035), (656, 4055, 1545, 4400)),
4512:(656, 285, 1120, 730), (656, 845, 1545, 1270), (656, 1292, 1540, 1715), (656, 3635, 1545, 4075), (656, 4095, 1545, 4440)),
4392:(656, 285, 1120, 730), (656, 845, 1545, 1270), (656, 1292, 1540, 1715), (656, 3515, 1545, 3955), (656, 3975, 1545, 4320)),
3475:(656, 285, 1120, 730), (656, 845, 1545, 1270), (656, 1292, 1540, 1715), (656, 2600, 1545, 3035), (656, 3060, 1545, 3405)),
3352:((656, 285, 1120, 730), (656, 845, 1545, 1270), (656, 1292, 1540, 1715), (656, 2475, 1545, 2915), (656, 2935, 1545, 3280),
3304:((705, 240, 1080, 605), (705, 705, 1445, 1060), (705, 1080, 1445, 1425), (705, 2590, 1445, 2955). (705, 2975, 1445, 3260)),
3310:((705, 240, 1080, 605), (705, 705, 1445, 1060), (705, 1080, 1445, 1425), (705, 2590, 1445, 2955). (705, 2975, 1445, 3260))}
def crop(image, crop_areas):
basename_without_ext = os.path.splitext(os.path.basename(image.filename))[0]
print(basename_without_ext, image.height)
for idx, crop_area in enumerate(crop_areas, start=1):
im_crop = image.crop(crop_area)
im_crop.save(f'{basename_without_ext}crop{idx}.png', quality=100)
def export2csv(group):
df = pd.DataFrame(group)
df.to_csv ('NokoriList.csv', encoding='utf-8-sig')
if __name__ == '__main__':
pathtodirectory = 'E:/testimages/images'
nokori = []
for item in os.scandir(path):
if item.is_file:
im = Image.open(item.path)
width, height = im.size
crop_areas = CROPS.get(im.height)
if crop_areas:
crop(im, crop_areas)
else:
basename_without_ext = os.path.splitext(os.path.basename(im.filename))[0]
nokori.append({'name':basename_without_ext, 'height':height})
export2csv(nokori)
Posts: 1,838
Threads: 2
Joined: Apr 2017
Jul-08-2021, 06:11 PM
(This post was last modified: Jul-08-2021, 06:47 PM by ndc85430.)
(Jul-08-2021, 01:24 AM)tsurubaso Wrote: My program was working normally and I must have done something wrong,
There are a few things you can do in future that would help with these sorts of problems:
1. Keep track of the history of your changes using a version control system (e.g. Git or Mercurial). That way you can undo things and go back to earlier versions if necessary. When doing so, it's important to make each commit do one thing. By one thing, I don't mean you're restricted changing one line, one function or even one file. I mean a group of changes that make sense as a whole and no more - that could mean fixing a bug, or one part of introducing a new feature, for example. You don't want commits make lots of unrelated changes because if you do need to undo something, that's going to be harder. Yes, of course it takes practice to do this well.
2. Write tests - code that exercises the functionality in parts of your program to check it behaves as expected. The idea is that when you make a change to the code, you run all the tests and see whether that has caused anything else to break. You'll hear of different categorisations of tests - unit, integration, functional, acceptance, end to end are some commonly used groupings of tests and they differ in the scale they test at. Unit tests test at the smallest scale (e.g. a function or a particular class), whereas acceptance/end to end tests test at a large scale - often the application as a whole, or as close as you can get to that. Tests let you refactor safely - when I said, "when you make a change", I meant either in behaviour of the code or in the structure. A good test suite will give you good feedback about when things aren't working as they were. Python comes with a module in the standard library, unittest and there are third party libraries too, like pytest.
On refactoring specifically, there's a good book by Martin Fowler that discusses the techniques, called, unsurprisingly, Refactoring. I believe the latest edition is in JavaScript or something but the ideas are generally applicable across languages. Clean Code by Bob Martin is another good read as it gives you insights on the things that can cause code to be hard to read and understand and what you can do about them.
|