Yaz Hobooti commited on
Commit
c5725aa
·
1 Parent(s): 69863ad

Fix zxingcpp position handling: support non-iterable Position by reading corner attributes; prevent 'Position is not iterable'

Browse files
Files changed (1) hide show
  1. pdf_comparator.py +35 -6
pdf_comparator.py CHANGED
@@ -857,14 +857,43 @@ def _decode_zxing(pil: Image.Image) -> List[Dict[str,Any]]:
857
  arr = np.asarray(pil.convert("L"))
858
  out=[]
859
  for r in zxingcpp.read_barcodes(arr): # try_harder is default True in recent builds; otherwise supply options
860
- pts = r.position or []
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
861
  if pts:
862
- xs=[p.x for p in pts]; ys=[p.y for p in pts]
863
- x1,x2=int(min(xs)),int(max(xs)); y1,y2=int(min(ys)),int(max(ys))
 
864
  w,h=x2-x1,y2-y1
865
- else:
866
- x1=y1=w=h=0
867
- out.append({"type": str(r.format), "data": r.text or "", "left": x1, "top": y1, "width": w, "height": h})
 
 
 
 
 
868
  return out
869
 
870
  def _decode_zbar(pil: Image.Image) -> List[Dict[str,Any]]:
 
857
  arr = np.asarray(pil.convert("L"))
858
  out=[]
859
  for r in zxingcpp.read_barcodes(arr): # try_harder is default True in recent builds; otherwise supply options
860
+ # zxingcpp.Position may be iterable (sequence of points) or an object with corner attributes
861
+ x1=y1=x2=y2=w=h=0
862
+ pos = getattr(r, "position", None)
863
+ pts: List[Any] = []
864
+ if pos is not None:
865
+ try:
866
+ pts = list(pos) # works if iterable
867
+ except TypeError:
868
+ # Fall back to known corner attribute names across versions
869
+ corner_names = (
870
+ "top_left", "topLeft",
871
+ "top_right", "topRight",
872
+ "bottom_left", "bottomLeft",
873
+ "bottom_right", "bottomRight",
874
+ "point1", "point2", "point3", "point4",
875
+ )
876
+ seen=set()
877
+ for name in corner_names:
878
+ if hasattr(pos, name):
879
+ p = getattr(pos, name)
880
+ # avoid duplicates
881
+ if id(p) not in seen and hasattr(p, "x") and hasattr(p, "y"):
882
+ pts.append(p)
883
+ seen.add(id(p))
884
  if pts:
885
+ xs=[int(getattr(p, "x", 0)) for p in pts]
886
+ ys=[int(getattr(p, "y", 0)) for p in pts]
887
+ x1,x2=min(xs),max(xs); y1,y2=min(ys),max(ys)
888
  w,h=x2-x1,y2-y1
889
+ out.append({
890
+ "type": str(r.format),
891
+ "data": r.text or "",
892
+ "left": x1,
893
+ "top": y1,
894
+ "width": w,
895
+ "height": h,
896
+ })
897
  return out
898
 
899
  def _decode_zbar(pil: Image.Image) -> List[Dict[str,Any]]: