pytesseract not detecting numbers - python

The picture I am trying to read is outputting "ones". I don't know how it's getting that. My code:
left = 980
right = 1000
top = 237
bottom = 265
CroppedImage = cropimage.crop((left,top,right,bottom))
if os.path.isfile("Price.png"):
os.remove("Price.png")
CroppedImage.save('Price.png', 'PNG')
Check_Price = pytesseract.image_to_string(Image.open('Price.png'), lang='eng')
Check_Price = Check_Price[:-2]
if len(Check_Price) == 4:
Found_Price = True
print(Check_Price)
I have pytesseract properly installed and PIL. It's all working for 2 other ones that I have, but it just won't read this text.

You need to scale the image up, using this code i can get your number you need.
import pytesseract
from PIL import Image
pytesseract.pytesseract.tesseract_cmd = r'C:\\Program Files\\Tesseract-OCR\\tesseract.exe'
def read():
i = Image.open('./JA9JF.png')
scale = 2.1
i_width = i.size[1] * scale
i_height = i.size[0] * scale
i = i.resize((int(i_width), int(i_height)), Image.BILINEAR)
xconfig = "-c page_separator=''"
Check_Price = pytesseract.image_to_string(i, lang='eng', config=xconfig)
print(Check_Price)
read()
And so your code would be.
left = 980
right = 1000
top = 237
bottom = 265
CroppedImage = cropimage.crop((left,top,right,bottom))
if os.path.isfile("Price.png"):
os.remove("Price.png")
CroppedImage.save('Price.png', 'PNG')
i = Image.open('Price.png')
scale = 2.1
i_width = i.size[1] * scale
i_height = i.size[0] * scale
i = i.resize((int(i_width), int(i_height)), Image.BILINEAR)
xconfig = "-c page_separator=''"
Check_Price = pytesseract.image_to_string(i, lang='eng', config=xconfig)
Check_Price = Check_Price[:-2]
if len(Check_Price) == 4:
Found_Price = True
print(Check_Price)
i've added xconfig = "-c page_separator=''" because for me pytesseract add \n\x0c for some reason. I also don't know if this will work with other numbers, if it doesn't I'll have to fix it using cv2.

Related

Is there any way to fit an image in pptx without changing the aspect ratio of an image using python pptx package

I have a task to create water mark in images and create pptx using these images
and i should not change the aspect ratio of an image as per the rules
Image ratio = 4000x6016
Without changing the ratio, images are not fitting inside the pptx
Is there any way to fit an image in pptx without changing the aspect ratio of an image using python pptx package
Expected ouput:
current ouput
Code:
from wand.image import Image
from pptx import Presentation
from pptx.util import Inches
prs = Presentation()
blankSideLayout = prs.slide_layouts[4]
def makePPTX(path):
slide = prs.slides.add_slide(blankSideLayout)
slide.shapes.title.text = "sample title"
slide.placeholders[2].text = "Sample Sub Title"
slide.shapes.add_picture(path, Inches(1), Inches(3))
prs.save("slides.pptx")
logoImg = Image(filename='logo.jpg')
logoImg.transparentize(0.33)
img = Image(filename='img.jpg')
img.composite_channel("all_channels",logoImg,"dissolve",20,20)
img.save(filename='imgwatermark.jpg')
makePPTX('imgwatermark.jpg')
Yes. In my project (md2pptx) I do this.
Essentially you
Work out the dimensions of the graphic and the space you want to fit it in.
You figure out which dimension you need to scale by and by how much. Answers to 1. guide you in this.
You create the graphic scaling according to 2.
Here's code from the md2pptx repo:
def scalePicture(maxPicWidth, maxPicHeight, imageWidth, imageHeight):
heightIfWidthUsed = maxPicWidth * imageHeight / imageWidth
widthIfHeightUsed = maxPicHeight * imageWidth / imageHeight
if heightIfWidthUsed > maxPicHeight:
# Use the height to scale
usingHeightToScale = True
picWidth = widthIfHeightUsed
picHeight = maxPicHeight
else:
# Use the width to scale
usingHeightToScale = False
picWidth = maxPicWidth
picHeight = heightIfWidthUsed
return (picWidth, picHeight, usingHeightToScale)
The main difficulty is going to be figuring out the dimensions of the source graphic.
Here is some code I borrowed for that:
import imghdr, struct
def get_image_size(fname):
"""Determine the image type of fhandle and return its size.
from draco"""
try:
with open(fname, "rb") as fhandle:
head = fhandle.read(24)
if len(head) != 24:
return -1, -1
if imghdr.what(fname) == "png":
check = struct.unpack(">i", head[4:8])[0]
if check != 0x0D0A1A0A:
return
width, height = struct.unpack(">ii", head[16:24])
elif imghdr.what(fname) == "gif":
width, height = struct.unpack("<HH", head[6:10])
elif imghdr.what(fname) == "jpeg":
try:
fhandle.seek(0) # Read 0xff next
size = 2
ftype = 0
while not 0xC0 <= ftype <= 0xCF:
fhandle.seek(size, 1)
byte = fhandle.read(1)
while ord(byte) == 0xFF:
byte = fhandle.read(1)
ftype = ord(byte)
size = struct.unpack(">H", fhandle.read(2))[0] - 2
# We are at a SOFn block
fhandle.seek(1, 1) # Skip 'precision' byte.
height, width = struct.unpack(">HH", fhandle.read(4))
except Exception: # IGNORE:W0703
return
else:
return
return width, height
except EnvironmentError:
return -1, -1

How to get the coordinates of the dots in hand image?

My image,
I've tried to get quite accurate palm from the hand image and used mediapipe(mp) library to do it. But the result is:
mp calculates and draws green strokes and red dots, but also give the coordinates of red dots. So I can get the coordinates of blue circles using given coordinates of red ones. But it can't be enough to get "accurate" palm. I need to get the black dots' coordinates.
following is my code:
import cv2
import mediapipe as mp
import utils
mp_drawing = mp.solutions.drawing_utils
mp_hands = mp.solutions.hands
def makeCircle(img,circle_y,circle_x,radius):
image_height, image_width, _ = img.shape
y = int(circle_y * image_height)
x = int(circle_x * image_width)
circle_coordinates = (x,y)
color = (255, 0, 0)
thickness = 2
return cv2.circle(img,circle_coordinates,radius,color,thickness)
# For static images:
IMAGE_FILES = [
"C:/Users/USER/workspace/palm/images/sample1.png",
"C:/Users/USER/workspace/palm/images/sample2.png",
"C:/Users/USER/workspace/palm/images/sample3.png",
"C:/Users/USER/workspace/palm/images/sample4.png"
]
with mp_hands.Hands(
static_image_mode=True,
max_num_hands=2,
min_detection_confidence=0.5) as hands:
for idx, file in enumerate(IMAGE_FILES):
# Read an image, flip it around y-axis for correct handedness output (see
# above).
image = cv2.flip(cv2.imread(file), 1)
image = utils.remove_bground(image)
# resize image's height 600 fixing the ratio
image = utils.ResizeWithAspectRatio(image,height=600)
# Convert the BGR image to RGB before processing.
results = hands.process(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
print("results:",results)
# Print handedness and draw hand landmarks on the image.
print('Handedness:', results.multi_handedness)
if not results.multi_hand_landmarks:
continue
image_height, image_width, _ = image.shape
annotated_image = image.copy()
for hand_landmarks in results.multi_hand_landmarks:
print('hand_landmarks:', hand_landmarks)
print(
f'Index finger tip coordinates: (',
f'{hand_landmarks.landmark[mp_hands.HandLandmark.INDEX_FINGER_TIP].x * image_width}, '
f'{hand_landmarks.landmark[mp_hands.HandLandmark.INDEX_FINGER_TIP].y * image_height})'
)
circle_coordinates = [
(
(hand_landmarks.landmark[mp_hands.HandLandmark.INDEX_FINGER_MCP].y + hand_landmarks.landmark[mp_hands.HandLandmark.INDEX_FINGER_PIP].y)/2,
(hand_landmarks.landmark[mp_hands.HandLandmark.INDEX_FINGER_MCP].x + hand_landmarks.landmark[mp_hands.HandLandmark.INDEX_FINGER_PIP].x)/2
),
(
(hand_landmarks.landmark[mp_hands.HandLandmark.MIDDLE_FINGER_MCP].y + hand_landmarks.landmark[mp_hands.HandLandmark.MIDDLE_FINGER_PIP].y)/2,
(hand_landmarks.landmark[mp_hands.HandLandmark.MIDDLE_FINGER_MCP].x + hand_landmarks.landmark[mp_hands.HandLandmark.MIDDLE_FINGER_PIP].x)/2
),
(
(hand_landmarks.landmark[mp_hands.HandLandmark.RING_FINGER_MCP].y + hand_landmarks.landmark[mp_hands.HandLandmark.RING_FINGER_PIP].y)/2,
(hand_landmarks.landmark[mp_hands.HandLandmark.RING_FINGER_MCP].x + hand_landmarks.landmark[mp_hands.HandLandmark.RING_FINGER_PIP].x)/2
),
(
(hand_landmarks.landmark[mp_hands.HandLandmark.PINKY_MCP].y + hand_landmarks.landmark[mp_hands.HandLandmark.PINKY_PIP].y)/2,
(hand_landmarks.landmark[mp_hands.HandLandmark.PINKY_MCP].x + hand_landmarks.landmark[mp_hands.HandLandmark.PINKY_PIP].x)/2
),
(
hand_landmarks.landmark[mp_hands.HandLandmark.WRIST].y,
hand_landmarks.landmark[mp_hands.HandLandmark.WRIST].x
),
(
hand_landmarks.landmark[mp_hands.HandLandmark.THUMB_CMC].y,
hand_landmarks.landmark[mp_hands.HandLandmark.THUMB_CMC].x
),
(
hand_landmarks.landmark[mp_hands.HandLandmark.THUMB_MCP].y,
hand_landmarks.landmark[mp_hands.HandLandmark.THUMB_MCP].x
),
]
for (circle_y,circle_x) in circle_coordinates:
annotated_image = makeCircle(annotated_image,circle_y,circle_x,10)
palm_center_y = (hand_landmarks.landmark[mp_hands.HandLandmark.THUMB_CMC].y +
hand_landmarks.landmark[mp_hands.HandLandmark.PINKY_MCP].y)/2
palm_center_x = (hand_landmarks.landmark[mp_hands.HandLandmark.THUMB_CMC].x +
hand_landmarks.landmark[mp_hands.HandLandmark.PINKY_MCP].x)/2
#
palm_range = 100
# annotated_image = makeCircle(annotated_image, palm_center_y,palm_center_x,palm_range)
mp_drawing.draw_landmarks(
annotated_image, hand_landmarks, mp_hands.HAND_CONNECTIONS)
hand_landmark_x = hand_landmarks.landmark[mp_hands.HandLandmark.INDEX_FINGER_TIP].x * image_width
hand_landmark_y = hand_landmarks.landmark[mp_hands.HandLandmark.INDEX_FINGER_TIP].y * image_height
cv2.imwrite(
'/tmp/annotated_image' + str(idx) + '.png', cv2.flip(annotated_image, 1))
cv2.imshow("img" + str(idx),cv2.flip(annotated_image,1))
cv2.waitKey(0)
How to get the coordinates of the dots in first hand image?
maybe you could use cv2.convexHull and cv2.convexityDefects to find that dots. I leave you a video tutorial that I think it could be usefull: 🖐️ CONTANDO DEDOS ✌️ (Usando defectos de convexidad) | Python - OpenCV

OSError: image file is truncated

When I am processing a bunch of images, on one of them I get this error
File "/home/tensorflowpython/firstmodel/yololoss.py", line 153, in data_generator
image, box = get_random_data(annotation_lines[i], input_shape, random=True)
File "/home/tensorflowpython/firstmodel/yololoss.py", line 226, in get_random_data
image = image.resize((nw,nh), Image.BICUBIC)
File "/home/tensorflowpython/kenv/lib/python3.6/site-packages/PIL/Image.py", line 1858, in resize
self.load()
File "/home/tensorflowpython/kenv/lib/python3.6/site-packages/PIL/ImageFile.py", line 247, in load
"(%d bytes not processed)" % len(b)
OSError: image file is truncated (25 bytes not processed)
I have already tried the solution suggested here but it doesn't work
my code looks like this
from PIL import Image
def get_random_data(annotation_line, input_shape, random=True, max_boxes=20, jitter=.3, hue=.1, sat=1.5, val=1.5, proc_img=True):
Image.LOAD_TRUNCATED_IMAGES = True
line = annotation_line.split()
image = Image.open(line[0])
iw, ih = image.size
h, w = input_shape
box = np.array([np.array(list(map(int,box.split(',')))) for box in line[1:]])
try:
image.load()
except IOError:
pass # You can always log it to logger
if not random:
# resize image
scale = min(w/iw, h/ih)
nw = int(iw*scale)
nh = int(ih*scale)
dx = (w-nw)//2
dy = (h-nh)//2
image_data=0
if proc_img:
image = image.resize((nw,nh), Image.BICUBIC)
new_image = Image.new('RGB', (w,h), (128,128,128))
new_image.paste(image, (dx, dy))
image_data = np.array(new_image)/255.
# correct boxes
box_data = np.zeros((max_boxes,5))
if len(box)>0:
np.random.shuffle(box)
if len(box)>max_boxes: box = box[:max_boxes]
box[:, [0,2]] = box[:, [0,2]]*scale + dx
box[:, [1,3]] = box[:, [1,3]]*scale + dy
box_data[:len(box)] = box
return image_data, box_data
# resize image
new_ar = w/h * rand(1-jitter,1+jitter)/rand(1-jitter,1+jitter)
scale = rand(.25, 2)
if new_ar < 1:
nh = int(scale*h)
nw = int(nh*new_ar)
else:
nw = int(scale*w)
nh = int(nw/new_ar)
image = image.resize((nw,nh), Image.BICUBIC) #error occurs here
The difference between my error and the previous solution is, mine says OS error and the solution is for IO error
EDIT: I have figured out the image that is causing this error, it can be downloaded from this link
I tried the solution that you linked with the truncated image and it worked. You made a slight mistake when trying to apply this solution: you have to set ImageFile.LOAD_TRUNCATED_IMAGES=True, not Image.LOAD_TRUNCATED_IMAGES.
LOAD_TRUNCATED_IMAGES does not originally exist in Image module, so when you do Image.LOAD_TRUNCATED_IMAGES=True you set a new variable which is not used by the library.
So I think you juste have to do that:
from PIL import ImageFile, Image
ImageFile.LOAD_TRUNCATED_IMAGES = True
image = Image.open("00090.jpg")
# resize now doesn't fail
image.resize((h, w), Image.BICUBIC)

Python + Pillow + Images2Gif - incorrect color of GIF frames

I'm using following code to add watermark to animated GIF images. My problem is that all GIF frames except the first one have incorrect colors in result. Would you know how to fix the color of frames? Thank you.
def add_watermark(in_file, watermark_file, watermark_position, watermark_ratio, out_file, quality=85):
img = Image.open(in_file)
watermark_layer = Image.new('RGBA', img.size, (0,0,0,0))
watermark_img = Image.open(watermark_file).convert('RGBA')
watermark_img.thumbnail((img.size[0]/watermark_ratio, 1000), Image.ANTIALIAS)
alpha = watermark_img.split()[3]
alpha = ImageEnhance.Brightness(alpha).enhance(0.95)
watermark_img.putalpha(alpha)
watermark_layer.paste(watermark_img, count_watermark_position(img, watermark_img, watermark_position))
frames = images2gif.readGifFromPIL(img, False)
frames_out = []
for frame in frames:
frames_out.append(Image.composite(watermark_layer, frame, watermark_layer))
images2gif.writeGif(out_file, frames_out, duration=0.5)
To complete example, i provide also code of helper function:
def count_watermark_position(img, watermark, position):
if position == 'right_bottom':
return img.size[0] - watermark.size[0], img.size[1] - watermark.size[1]
if position == 'center':
return (img.size[0] - watermark.size[0])/2, (img.size[1] - watermark.size[1])/2
if position == 'left_bottom':
return 0, img.size[1] - watermark.size[1]
if position == 'left_top':
return 0, 0
if position == 'right_top':
return img.size[0] - watermark.size[0], 0
raise AttributeError('Invalid position')
Source code of images2gif I 've used - I modified it a little bit to make it work with pillow. See comment at the begining of source code.

Basic a tiff processing with photoshop using python

I need to write a script that does the following:
# open a tiff
# get it's dpi, width, height and colorspace
# set the dpi, width, height and colorspace
# and then save the tiff out with no compression and no layers.
So far I've gotten:
from win32com.client.dynamic import Dispatch
ps = Dispatch( "Photoshop.Application" )
file_path = "C:\\Users\\me\\myImg.tif"
doc = ps.Open( file_path )
dpi = doc.Resolution
width = doc.Width # in cm
height = doc.Height # in cm
# up to here the code works, but then I try
doc.Resolution = 72
ps.ResizeImage( 120 , 120 )
ps.PsColorSpaceType( 3 ) # psSRGB
ps.TiffSaveOptions.ImageCompression = 1 # psNoTIFFCompression
ps.TiffSaveOptions.Layers = False
ps.Save()
# and this last section fails
Please help, any ideas, tips, soultions would be greatly appreciated :D
After a lot of googeling and some trial and error and then even more trial and error I've managed to come up with the code below.
Hope this can help someone else.
Code
file_path = "C:\\Users\\me\\myImg.tif"
color_settings = "North America General Purpose 2"
from win32com.client.dynamic import Dispatch
ps_app = Dispatch( "Photoshop.Application" )
# set photoshop to use pixels as dimensions
ps_app.Preferences.RulerUnits = 1 # 'for PsUnits --> 1 (psPixels)
ps_app.Preferences.TypeUnits = 1 # 'for PsTypeUnits --> 1 (psPixels)
doc = ps_app.Open( file_path ) # Open a file and store open file as doc
dpi = doc.Resolution
width = doc.Width
height = doc.Height
cor_res = 1024
ps_app.ChangeColorSettings( color_settings )
doc.ResizeImage( cor_res , cor_res , 72 )
options = Dispatch('Photoshop.TiffSaveOptions')
options.ImageCompression = 1 # ps_appNoTIFFCompression
options.Layers = False # no layers
doc.SaveAs( file_path , options ) # Save with specified options
doc.Close( 2 ) # psDoNotSaveChanges

Categories

Resources