A python generates a sequence of random strings + writes to the picture
fromPILImportImage,imagedraw,imagefontImportNumPy as NPImportRandomImportstringImportCv2#Generate random string forIinchRange (1,10000): Strnum= Random.randint (2,10) Ran_str="". Join (Random.sample (string.ascii_letters +string.digits, Strnum)) Font= Imagefont.truetype ('D:\MULTIMEDIA\ROSEWOODSTD-REGULAR.OTF', 60# OTF and TTF are all available. Image= Image.new ("RGB", (300, 200), (255, 255, 255)) Draw=Imagedraw.draw (image)ifStrnum <=5:
# parameter One write position parameter two text parameter three font format fill for text color default to white Draw.text ((Ran_str, Font=font, fill='Black') Else: Draw.text ((Ran_str, Font=font, fill='Black') path="D:\AdobeVFR_release\\rosewood"+"\\rosewood_regular_"+str (i) +". JPEG"image.save (path)if(i%10==0):Print("Has already save%d images"%i
Question: Write to the text on the picture, how can adjust the spacing between the text (thesis model need)
Two use cv2 some image preprocessing functions
ImportCv2 fromSkimageImportData_dir,io,colorImportNumPy as NPImportRandomsigma= Random.uniform (2.5,3.5)
# Gaussian noise function, which is written here by Pixel Point Single processing, you can build a Gaussian random number matrixdefGaussiannoise (img, means =0,sigma =1.5): R=Img[:,:,0].flatten () G= img[:,:,1].flatten () b= img[:,:,2].flatten () forIinchRange (img.shape[0]*img.shape[1]): R[i]= r[i]+Random.gauss (0,sigma) g[i]= g[i]+Random.gauss (0,sigma) b[i]= b[i]+Random.gauss (0,sigma) img[:,:,0]= R.reshape ([img.shape[0],img.shape[1]]) img[:,:,1] = R.reshape ([img.shape[0],img.shape[1]]) img[:,:,2] = R.reshape ([img.shape[0],img.shape[1]]) returnimg
# Geometric change function, the main function is to distortdefgeometric_changes (image,width,height): Pts1= Np.float32 ([[50, 50], [200, 50], [50, 200]]) x= Random.randint (50,100) y= Random.randint (200,250) Z= Random.randint (10,50) Pts2= Np.float32 ([[Z, X], [200, 50], [x, Y]]) M=Cv2.getaffinetransform (Pts1, pts2) image_0=cv2.warpaffine (Image, M, (width, height))returnImage_0path='D:\AdobeVFR_release\sythetic'string= path+'/*.jpeg‘
"" "
Io. Imagecollrction to organize the picture path into a list
"" "Coll=io. ImageCollection (String) a=Np.array (coll) forIinchRange (95, Len (a)): Height=a[i].shape[0] Width= A[i].shape[1] Image_0=gaussiannoise (A[i]) Image_0= Cv2. Gaussianblur (Image_0, (5,5), Sigma) # Gaussian Blur Image_0=Geometric_changes (image_0, width, height)
# scaling function, Fx,fy for scaling factor interpolation there are five kinds,
#INTER_AREA The resampling image is reduced based on the local pixel, this method can avoid the ripple
#INTER_NEAREST nearest neighbor interpolation method for amplification
#INTER_LINEAR bilinear interpolation method Default
#INTER_CUBIC 3-time interpolation based on 4x4 Pixel neighborhood for amplification
#INTER_LANCZOS4-Lanczos interpolation based on 8x8 Pixel neighborhood
= Cv2.resize (Image_0, None, Fx=random.uniform (5/6, 7/6), Fy=1, interpolation=cv2. Inter_area)
" D:\AdobeVFR_release\sydata "+"\d"+str (i) +". JPEG" Cv2.imwrite (Path, image_0) if(i%10==0): print(" Has already%d image " % i)
Some image processing functions are updated later
PYTHON+OPENCV Image preprocessing