Do you know good library (write on python) for converting dxf files to bmp?
I have a folder with "filename_number" dxf files.
I want read it and convert it to bmp. Also I want keep file's dimension size.
I got the answer from here:
import StringIO, base64, logging, operator
import dxfgrabber
import Image, ImageDraw, ImageFont #PIL
from homcoord import *
def rint(x):return int(round(x))
class BBox:
"""bounding box"""
def __init__(self,pt1=None,pt2=None):
self._corner1=None
self._corner2=None
if pt1: self+=pt1
if pt2: self+=pt2
def __iadd__(self,pt):
if isinstance(pt,BBox):
self+=pt._corner1
self+=pt._corner2
else:
if not self._corner1:
self._corner1=pt
else:
self._corner1=Pt(map(min,zip(self._corner1.xy,pt.xy)))
if not self._corner2:
self._corner2=pt
else:
self._corner2=Pt(map(max,zip(self._corner2.xy,pt.xy)))
return self
def __repr__(self):
return "BBox(%s,%s)"%(self._corner1,self._corner2)
def __call__(self):
""":return: list of flatten corners"""
l=list(self._corner1.xy)
l.extend(list(self._corner2.xy))
return l
def size(self):
""":return: Pt with xy sizes"""
return self._corner2-self._corner1
def center(self):
""":return: Pt center"""
res=self._corner2+self._corner1
return res/2
def trans(self,trans):
"""
:param trans: Xform
:return: BBox = self transformed by trans
"""
return BBox(trans(self._corner1),trans(self._corner2))
def cbox(c,r):
""" bounding box of a circle
:param c: Pt center
:param r: float radius
:return: BBox
"""
rr=Pt(r,r)
return BBox(c+rr,c-rr)
def Trans(scale=1, offset=[0,0], rotation=0):
res=Xform([[scale,0,offset[0]],[0,scale,offset[1]],[0,0,1]])
if rotation:
res=Xrotate(rotation*pi/180.)*res
return res
class DXF:
def __init__(self, file, layers=None, ignore=[]):
"""reads a .dxf file
:param file: string path to .dxf file to read
:param layers: list or dictionary of layers to handle. Empty = all layers
:param ignore: list of strings of entity types to ignore
"""
self.dxf=dxfgrabber.readfile(file)
self.layers=layers
self.ignore=ignore
def entities(self,ent=None):
"""iterator over dxf or block entities"""
if not ent:
ent=self.dxf.entities
for e in ent:
if self.layers and e.layer not in self.layers:
continue
elif e.dxftype in self.ignore:
continue
else:
yield e
def bbox(self):
""":return: :class:BBox dwg enclosing bounding box"""
box=BBox()
for e in self.entities():
if e.dxftype=='LINE':
box+=Pt(e.start[:2])
box+=Pt(e.end[:2])
elif e.dxftype == 'CIRCLE':
box+=cbox(Pt(e.center[:2]),e.radius)
elif e.dxftype == 'ARC':
c=Pt(e.center[:2])
a=e.endangle-e.startangle
if a>0:
start=e.startangle
else: #arc goes clockwise (step will be negative)
start=e.endangle
n=rint(abs(a)/10.) # number of points each 10° approximately
n=max(n,1)
step=a/n #angle between 2 points, might be negative
for i in range(n+1):
box+=c.radial(e.radius,radians(start+i*step))
elif e.dxftype=='POLYLINE':
for v in e.vertices:
box+=Pt(v.location[:2])
elif e.dxftype=='BLOCK':
pass #TODO ...
elif e.dxftype in ['TEXT','INSERT']:
box+=Pt(e.insert[:2])
else:
logging.warning('Unknown entity %s'%e)
return box
def _draw(self,draw,entities,trans,pen="black"):
for e in entities:
if e.dxftype=='LINE':
b=list(trans(Pt(e.start[:2])).xy)
b.extend(list(trans(Pt(e.end[:2])).xy))
draw.line(b,fill=pen)
elif e.dxftype=='CIRCLE':
b=cbox(Pt(e.center[:2]),e.radius)
b=b.trans(trans)
draw.ellipse(b(),outline=pen)
elif e.dxftype=='ARC':
c=Pt(e.center[:2])
b=cbox(c,e.radius)
b=b.trans(trans)
b=map(rint,b())
startangle=degrees(trans.angle(radians(e.startangle)))
endangle=degrees(trans.angle(radians(e.endangle)))
startangle,endangle=endangle,startangle #swap start/end because of Y symmetry
draw.arc(b,int(startangle),int(endangle),fill=pen)
elif e.dxftype=='POLYLINE':
b=[]
for v in e.vertices:
b.extend(list(trans(Pt(v.location[:2])).xy))
draw.line(b,fill=pen)
elif e.dxftype=='TEXT':
h=e.height*trans.mag()
pt=Pt(e.insert[:2])+Pt(0,e.height) #ACAD places texts by top left point...
font=None
try:
font = ImageFont.truetype("c:/windows/fonts/Courier New.ttf", rint(h))
except:
pass
if not font:
h=h*1.4 #magic factor ...
fh=[8,10,12,14,16,18,20,22,24,26,28,30,36,40,48,60]
i,h=min(enumerate(fh), key=lambda x: abs(x[1]-h)) #http://stackoverflow.com/questions/9706041/finding-index-of-an-item-closest-to-the-value-in-a-list-thats-not-entirely-sort
import os
path=os.path.realpath(__file__)
path=os.path.dirname(path)
font = ImageFont.load(path+'\\base_pil\\72\\Courier New_%s_72.pil'%h)
draw.text(trans(pt).xy,e.text,font=font,fill=pen)
elif e.dxftype=='INSERT':
t2=Trans(1,e.insert,e.rotation).compose(trans)
self._draw(draw,self.entities(self.dxf.blocks[e.name]._entities),t2,pen)
elif e.dxftype=='BLOCK':
pass # block definition is automatically stored in dxf.blocks dictionary
else:
logging.warning('Unknown entity %s'%e)
def img(self,size=[256,256],back="white",pen="black",border=5,antialias=1):
""":result: :class:`PIL:Image` rasterized image"""
box=self.bbox()
from Goulib.math2 import product
if not product(box.size().xy): # either x or y ==0
return None
s=map(operator.div,[float(x-border)*antialias if x else 1E9 for x in size ],box.size().xy)
trans=Trans(scale=min(s))
size=trans(box.size())+Pt(2*antialias*border,2*antialias*border) #add borders as an offset
offset=size/2-trans(box.center()) #offset in pixel coordinates
trans=trans*Trans(offset=offset.xy)
trans=trans*Xscale(1,-1) #invert y axis
trans=trans*Xlate(0,size.y) #origin is lower left corner
img = Image.new("RGB", map(rint,size.xy), back)
self._draw(ImageDraw.Draw(img), self.entities(), trans, pen)
if antialias>1:
size=size/antialias
img=img.resize(map(rint,size.xy), Image.ANTIALIAS)
return img
def img2base64(img,fmt='PNG'):
"""
:param img: :class:`PIL:Image`
:result: string base64 encoded image content in specified format
:see: http://stackoverflow.com/questions/14348442/django-how-do-i-display-a-pil-image-object-in-a-template
"""
output = StringIO.StringIO()
img.save(output, fmt)
output.seek(0)
output_s = output.read()
return base64.b64encode(output_s)
if __name__ == '__main__':
dxf=DXF("..\\tests\\FERRO_01.DXF")
img=dxf.img(size=[1280,None],border=50)
print img2base64(img)
img.save('..\\tests\\out.png')
extract all the details you need using ezdxf library.
using matplotlib library plot them as graph using matplotlib commands.
save the file as you need using same matplotlib library.
Related
I'm trying to add a high FPS screen recorder to my application.
I use Python 3.7 on Windows.
The modules and methods I've tried are mss (python-mss) and d3dshot, but I'm still only achieving 15-19 FPS for a long video (more than 20 seconds).
The resolution I'm recording at is 1920 x 1080.
What is the best way to optimize screen recording? I've tried to use the multiprocessing library, but it seems like it's still not fast enough. I'm not sure I'm using it in the optimal way, what are some ways I could use it to improve processing performance?
Using OBS Studio, I'm able to get 30 FPS, no matter how long the video is. My objective is to achieve the same results with my own code.
Here is what I've written so far:
from multiprocessing import Process, Queue
from time import sleep, time
import cv2
import d3dshot
import numpy as np
def grab(queue):
d = d3dshot.create(capture_output="numpy", frame_buffer_size=500)
d.capture()
sleep(0.1)
c=0
begin = time()
while time() - begin < 30:
starter = time()
frame = d.get_latest_frame()
queue.put(frame)
c+=1
ender = time()
sleep(max(0, 1/60 - (ender -starter)))
# Tell the other worker to stop
queue.put(None)
final=time()
print(c/(final-begin))
d.stop()
def save(queue):
SCREEN_SIZE = 1920, 1080
# Define the codec and create VideoWriter object
fourcc = cv2.VideoWriter_fourcc(*'DIVX') # In Windows: DIVX
out = cv2.VideoWriter(r"output.avi",fourcc, 30.0, (SCREEN_SIZE))
# type: (Queue) -> None
last_img = None
while "there are screenshots":
img = queue.get()
if img is None:
break
if img is last_img:
continue
out.write(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
last_img = img
if __name__ == "__main__":
# The screenshots queue
queue = Queue() # type: Queue
# 2 processes: one for grabing and one for saving PNG files
Process(target=grab, args=(queue,)).start()
Process(target=save, args=(queue,)).start()
The goal is to capture a game, while performing automated keyboard and mouse actions.
I have faced the same problem in trying to get high speed recording for games. This was the fastest solution I was able to find for Windows. The code is using raw buffer objects and leads to around ~27 FPS. I cannot find the original post on which this code is based, but if someone finds it I will add the reference.
Note that the framerate will significantly increase if you make the region smaller than 1920x1080.
"""
Alternative screen capture device, when there is no camera of webcam connected
to the desktop.
"""
import logging
import sys
import time
import cv2
import numpy as np
if sys.platform == 'win32':
import win32gui, win32ui, win32con, win32api
else:
logging.warning(f"Screen capture is not supported on platform: `{sys.platform}`")
from collections import namedtuple
class ScreenCapture:
"""
Captures a fixed region of the total screen. If no region is given
it will take the full screen size.
region_ltrb: Tuple[int, int, int, int]
Specific region that has to be taken from the screen using
the top left `x` and `y`, bottom right `x` and `y` (ltrb coordinates).
"""
__region = namedtuple('region', ('x', 'y', 'width', 'height'))
def __init__(self, region_ltrb=None):
self.region = region_ltrb
self.hwin = win32gui.GetDesktopWindow()
# Time management
self._time_start = time.time()
self._time_taken = 0
self._time_average = 0.04
def __getitem__(self, item):
return self.screenshot()
def __next__(self):
return self.screenshot()
def __enter__(self):
return self
def __exit__(self, exc_type, exc_val, exc_tb):
self.close()
if exc_type and isinstance(exc_val, StopIteration):
return True
return False
#staticmethod
def screen_dimensions():
""" Retrieve total screen dimensions. """
left = win32api.GetSystemMetrics(win32con.SM_XVIRTUALSCREEN)
top = win32api.GetSystemMetrics(win32con.SM_YVIRTUALSCREEN)
height = win32api.GetSystemMetrics(win32con.SM_CYVIRTUALSCREEN)
width = win32api.GetSystemMetrics(win32con.SM_CXVIRTUALSCREEN)
return left, top, height, width
#property
def fps(self):
return int(1 / self._time_average) * (self._time_average > 0)
#property
def region(self):
return self._region
#property
def size(self):
return self._region.width, self._region.height
#region.setter
def region(self, value):
if value is None:
self._region = self.__region(*self.screen_dimensions())
else:
assert len(value) == 4, f"Region requires 4 input, x, y of left top, and x, y of right bottom."
left, top, x2, y2 = value
width = x2 - left + 1
height = y2 - top + 1
self._region = self.__region(*list(map(int, (left, top, width, height))))
def screenshot(self, color=None):
"""
Takes a part of the screen, defined by the region.
:param color: cv2.COLOR_....2...
Converts the created BGRA image to the requested image output.
:return: np.ndarray
An image of the region in BGRA values.
"""
left, top, width, height = self._region
hwindc = win32gui.GetWindowDC(self.hwin)
srcdc = win32ui.CreateDCFromHandle(hwindc)
memdc = srcdc.CreateCompatibleDC()
bmp = win32ui.CreateBitmap()
bmp.CreateCompatibleBitmap(srcdc, width, height)
memdc.SelectObject(bmp)
memdc.BitBlt((0, 0), (width, height), srcdc, (left, top), win32con.SRCCOPY)
signed_ints_array = bmp.GetBitmapBits(True)
img = np.frombuffer(signed_ints_array, dtype='uint8')
img.shape = (height, width, 4)
srcdc.DeleteDC()
memdc.DeleteDC()
win32gui.ReleaseDC(self.hwin, hwindc)
win32gui.DeleteObject(bmp.GetHandle())
# This makes sure that the FPS are taken in comparison to screenshots rates and vary only slightly.
self._time_taken, self._time_start = time.time() - self._time_start, time.time()
self._time_average = self._time_average * 0.95 + self._time_taken * 0.05
if color is not None:
return cv2.cvtColor(img, color)
return img
def show(self, screenshot=None):
""" Displays an image to the screen. """
image = screenshot if screenshot is not None else self.screenshot()
cv2.imshow('Screenshot', image)
if cv2.waitKey(1) & 0xff == ord('q'):
raise StopIteration
return image
def close(self):
""" Needs to be called before exiting when `show` is used, otherwise an error will occur. """
cv2.destroyWindow('Screenshot')
def scale(self, src: np.ndarray, size: tuple):
return cv2.resize(src, size, interpolation=cv2.INTER_LINEAR_EXACT)
def save(self, path, screenshot=None):
""" Store the current screenshot in the provided path. Full path, with img name is required.) """
image = screenshot if screenshot is not None else self.screenshot
cv2.imwrite(filename=path, img=image)
if __name__ == '__main__':
# Example usage when displaying.
with ScreenCapture((0, 0, 1920, 1080)) as capture:
for _ in range(100):
capture.show()
print(f"\rCapture framerate: {capture.fps}", end='')
# Example usage as generator.
start_time = time.perf_counter()
for frame, screenshot in enumerate(ScreenCapture((0, 0, 1920, 1080)), start=1):
print(f"\rFPS: {frame / (time.perf_counter() - start_time):3.0f}", end='')
Edit
I noticed some small mistake in the window show function, and the self.screenshot calls in the __getitem__ and __next__ method. These have been resolved.
Next to the for example using the ScreenCapture as a context manager, I added an example of using it as a generator.
I'm trying to save both, the depth and color images of the Intel Realsense D435i camera in a list of 300 images. Then I will use multiprocessing to save this chunk of 300 images onto my disk. But every time I try, the program successfully appends 15 images in the list and then I get this error:
Frame didn't arrived within 5000
I made sure I had the 64 bit version on python 3.6 installed and the camera streams perfectly well when I do not try to save the images in a list. The real-sense viewer works great too. I also tried with different resolutions and frame rates but it doesn't seem to work either. What is interesting is if I only save the color images, I will not get the same error, instead I will get the same color image over and over in the list.
if __name__ == '__main__':
pipeline = rs.pipeline()
config = rs.config()
config.enable_stream(rs.stream.depth, 640, 480, rs.format.z16, 30)
config.enable_stream(rs.stream.color, 1280, 720, rs.format.bgr8, 30)
profile = pipeline.start(config)
depth_sensor = profile.get_device().first_depth_sensor()
depth_sensor.set_option(
rs.option.visual_preset, 3
) # Set high accuracy for depth sensor
depth_scale = depth_sensor.get_depth_scale()
align_to = rs.stream.color
align = rs.align(align_to)
# Init variables
im_count = 0
image_chunk = []
image_chunk2 = []
# sentinel = True
try:
while True:
# Wait for a coherent pair of frames: depth and color
frames = pipeline.wait_for_frames()
aligned_frames = align.process(frames)
aligned_depth_frame = aligned_frames.get_depth_frame()
color_frame = aligned_frames.get_color_frame()
if not aligned_depth_frame or not color_frame:
print("problem here")
raise RuntimeError("Could not acquire depth or color frames.")
depth_image = np.asanyarray(aligned_depth_frame.get_data())
color_image = np.asanyarray(color_frame.get_data())
image_chunk.append(color_image)
image_chunk2.append(depth_image)
except Exception as e:
print(e)
finally:
# Stop streaming
pipeline.stop()
I simply need it to save 300 images in a row, that's all, so I am quite troubled as to what is causing this issue.
Holding onto the frame locks the memory, and eventually it hits a limit, which prevents acquiring more images. Even though you are creating an image, the data is still from the frame. You need to clone the image after you create it to release the link to the frame's memory.
depth_image = np.asanyarray(aligned_depth_frame.get_data())
color_image = np.asanyarray(color_frame.get_data())
depth_image = depth_image.copy()
color_image = color_image.copy()
image_chunk.append(color_image)
image_chunk2.append(depth_image)
Read more on frames and memory management here:
https://dev.intelrealsense.com/docs/frame-management
I created a wrapper class to extract the various elements out of the frame set that can't be recreated later. It's a bit heavy, but shows some common operations that might be helpful for others:
colorizer = None
align_to_depth = None
align_to_color = None
pointcloud = rs.pointcloud()
class IntelD435ImagePacket:
"""
Class that contains image and associated processing data.
"""
#property
def frame_id(self):
return self._frame_id
#property
def timestamp(self):
return self._timestamp
#property
def image_color(self):
return self._image_color
#property
def image_depth(self):
return self._image_depth
#property
def image_color_aligned(self):
return self._image_color_aligned
#property
def image_depth_aligned(self):
return self._image_depth_aligned
#property
def image_depth_colorized(self):
if not self._image_depth_colorized:
self._image_depth_colorized = cv2.applyColorMap(self.image_depth, cv2.COLORMAP_JET);
return self._image_depth_colorized
#property
def intrinsics(self):
return self._intrinsics
#property
def pointcloud(self):
return self._pointcloud
#property
def pointcloud_texture(self):
return self._pointcloud_texture
def _rs_intrinsics_to_opencv_matrix(self, rs_intrinsics):
fx = rs_intrinsics.fx
fy = rs_intrinsics.fy
cx = rs_intrinsics.ppx
cy = rs_intrinsics.ppy
s = 0 # skew
return np.array([fx, s, cx,
0, fy, cy,
0, 0, 1]).reshape(3, 3)
def __init__(self, frame_set, frame_id=None, timestamp=None, *args, **kwargs):
global colorizer
if not colorizer:
colorizer = rs.colorizer()
colorizer.set_option(rs.option.color_scheme, 0)
global align_to_depth
if not align_to_depth:
align_to_depth = rs.align(rs.stream.depth)
global align_to_color
if not align_to_color:
align_to_color = rs.align(rs.stream.color)
global pointcloud
if not pointcloud:
pointcloud = rs.pointcloud()
# Get intrinsics
profile = frame_set.get_profile()
video_stream_profile = profile.as_video_stream_profile()
rs_intrinsics = video_stream_profile.get_intrinsics()
self._intrinsics = self._rs_intrinsics_to_opencv_matrix(rs_intrinsics)
# Get pointcloud
depth_frame = frame_set.get_depth_frame()
color_frame = frame_set.get_color_frame()
pointcloud.map_to(color_frame)
points = pointcloud.calculate(depth_frame)
vtx = np.asanyarray(points.get_vertices())
points_arr = vtx.view(np.float32).reshape(vtx.shape + (-1,)).copy()
self._pointcloud = points_arr
# Get pointcloud texture mapping
tex = np.asanyarray(points.get_texture_coordinates())
color_map_arr = tex.view(np.float32).reshape(tex.shape + (-1,)).copy()
self._pointcloud_texture = color_map_arr
# Extract color image
color_frame = frame_set.get_color_frame()
self._image_color = np.asanyarray(color_frame.get_data()).copy()
# Extract depth image
depth_frame = frame_set.get_depth_frame()
self._image_depth = np.asanyarray(depth_frame.get_data()).copy()
# Align the color frame to depth frame and extract color image
color_frame_aligned = align_to_depth.process(frame_set).get_color_frame()
self._image_color_aligned = np.asanyarray(color_frame_aligned.get_data()).copy()
# Align the depth frame to color frame and extract depth image
depth_frame_aligned = align_to_color.process(frame_set).get_depth_frame()
self._image_depth_aligned = np.asanyarray(depth_frame_aligned.get_data()).copy()
self._image_depth_colorized = None
if frame_id:
self._frame_id = frame_id
else:
self._frame_id = frame_set.frame_number
if timestamp:
self._timestamp = timestamp
else:
self._timestamp = frame_set.timestamp
self.__dict__.update(kwargs)
I have been battling for days to read coordinates LineString using fastkml library in python.
I have imported geometry module in my main program. I am reading like this.
My kml file is stored in string called doc
X = Geometry
Z=x._get,_coordinates(doc)
I got an error that says
the module object has no attributes find
from fastkml import kml
import re
from fastkml.geometry import Geometry
from lxml import etree
from xml.etree import ElementTree
from io import StringIO, BytesIO
def print_child_features(element):
if not getattr(element, 'features', None):
return
for feature in element.features():
y = feature.to_string()
return y
def print_child2_features(element):
""" Prints the name of every child node of the given element, recursively """
if not getattr(element, 'features', None):
return
for feature in element.features():
print feature.name
print_child2_features(feature)
def get_distance(coordinates_str):
"""gets distance of one path from coordinates string in form of:
14.81363432237944,53.57016581501523,0 14.81411766813742,53.56923005549378,0 14.81880340335202,53.56879451890311 ...
look at:
http://code.google.com/intl/pl-PL/apis/kml/documentation/kmlreference.html#coordinates
"""
sum_distance = 0.0
arr = []
coordinates = []
if ' ' in coordinates_str:
arr = coordinates_str.split(' ')
if len(arr) > 1:
for s in arr:
if ',' in s:
pt = s.split(',')
pos_latt = (float(pt[0].strip()), 0, 0)
pos_long = (float(pt[1].strip()), 0, 0)
position = (pos_latt, pos_long)
coordinates.append(position)
if coordinates:
for i in range(len(coordinates) - 1):
start = coordinates[i]
stop = coordinates[i + 1]
sum_distance += distance.points2distance(start, stop)
return sum_distance
if name == 'main':
#fname = input("Enter KML file name ")
fname = "DBN.kml"
k = kml.KML()
with open(fname) as kmlFile:
k.from_string(kmlFile.read())
x = Geometry()
doc = print_child_features(k)
z= x._get_coordinates(doc)
print z
length=0
if length <= 10:
print("Transciver is LR")
elif length > 10 and length <= 40:
print("Transciver is ER")
else:
print("Transciver is ER")
I did not get the code you have written. But I have written code to read coordinates from Linestrings and other Geometries using fastkml library, so I can help.
# import necessary modules
from fastkml import kml, geometry
k = kml.KML() # create fastkml object
k.from_string(doc.encode('utf-8')) # read doc string
document = list(k.features())
self.parse_placemarks(document)
# parse features throughout the KML File
def parse_placemarks(self, document):
for feature in document:
if isinstance(feature, kml.Placemark): # when there is no folder
placemark = feature
self.parse_geometries(placemark)
for feature in document:
if isinstance(feature, kml.Folder):
self.parse_placemarks(list(feature.features()))
if isinstance(feature, kml.Document):
self.parse_placemarks(list(feature.features()))
# parse geometry
def parse_geometries(self, placemark):
if hasattr(placemark, "geometry"):
if isinstance(placemark.geometry, geometry.LineString):
self.linestring(placemark)
# parse linestring
def linestring(self, line):
x, y = self.compute_coordinates(line.geometry)
# find coordinates
def compute_coordinates(self, geometry):
lons = []
lats = []
for coordinates in geometry.coords:
lons.append(coordinates[0])
lats.append(coordinates[1])
return (lons, lats)
You can find how to extract coordinates of other geometries and more about fastkml here : https://medium.com/#wwaryan/the-definite-only-guide-to-fastkml-58b8e19b8454
when i run the program I need my code to :
initialize the camera
take a picture
request user to enter paths for the current image to be stored and the image to be compared to
detect edges of currently taken picture and save in database
compare current edge image to 10 or more edge images in database
output as the edge image that has highest match percentage with current edge image
basically its like an object identification program ... can someone please help me out ?
here is the code i have done so far
from itertools import izip
import numpy as np
import cv2
from matplotlib import pyplot as plt
from PIL import Image
def take_and_save_picture(im_save):
'''Take a picture and save it
Args:
im_save: filepath where the image should be stored
'''
camera_port = 0
ramp_frames = 30
cap = cv2.VideoCapture(camera_port)
def get_image():
retval, im = cap.read()
return im
for i in xrange(ramp_frames):
temp = get_image()
print("Taking image...")
# Take the actual image we want to keep
camera_capture = get_image()
#im_save_tmp = im_save + '.jpg'
im_save_tmp = im_save
# A nice feature of the imwrite method is that it will automatically choose the
# correct format based on the file extension you provide. Convenient!
cv2.imwrite(im_save_tmp, camera_capture)
# You'll want to release the camera, otherwise you won't be able to create a new
# capture object until your script exits
# del(cap)
img1 = cv2.imread(im_save_tmp, 0)
edges = cv2.Canny(img1, 100, 200)
cv2.imwrite(im_save, edges)
cv2.waitKey(0)
cv2.destroyAllWindows()
#im1 = "/Users/Me/gop.jpg"
#im2 = "/Users/Me/aarthi.jpg"
im1 = input('enter the path of database file')
im2 = input('enter the path where captured image is to be saved')
#im1="/Users/Me/home1.png"
#im2="/Users/Me/home.png"
def compute_edges_diff(im1, im2):
'''Compute edges diff between to image files.
Args:
im1: filepath to the first image
im2: filepath to the second image
Returns:
float: percentage of difference between images
'''
#for no_file1 in range(0,10):
#template = cv2.imread('numbers1/{no_file}.png'.format(no_file=no_file1),0)
i1 = Image.open(im1)
i2 = Image.open(im2)
assert i1.mode == i2.mode, "Different kinds of images."
assert i1.size == i2.size, "Different sizes."
pairs = izip(i1.getdata(), i2.getdata())
if len(i1.getbands()) == 1:
# for gray-scale jpegs
dif = sum(abs(p1-p2) for p1,p2 in pairs)
else:
dif = sum(abs(c1-c2) for p1,p2 in pairs for c1,c2 in zip(p1,p2))
ncomponents = i1.size[0] * i1.size[1] * 3
diff = (dif / 255.0 * 100) / ncomponents
return diff
def main():
#capture_img = "/Users/Me/home1.png"
capture_img = input('enter path of the file from database')
#img_to_compare = "/Users/Me/Documents/python programs/compare/img2.jpg"
take_and_save_picture(capture_img)
diff = compute_edges_diff(im1, im2)
print "Difference (percentage):", diff
if diff > 0.5:
print im1
else :
print im2
if __name__ == '__main__':
main()
#del(cap)
this code works fine .. but i am able to compare only one image ... i need to compare the current taken images with all images in my database ...
In your main function, create a list to ask for the path for the image files, wrap the compare in a for loop:
def get_images_to_compare():
images_to_compare = []
while True:
comp_img = raw_input("Path of image to compare to: ")
if len(comp_img) <= 1:
# break if someone just hits enter
break
images_to_compare.append(comp_img)
return images_to_compare
def main():
#capture_img = "/Users/Me/home1.png"
capture_img = input('enter path of the file from database')
#img_to_compare = "/Users/Me/Documents/python programs/compare/img2.jpg"
take_and_save_picture(capture_img)
#### you have some odd var names here, basic gist, add a for loop
for comp_image in get_images_to_compare():
diff = compute_edges_diff(im1, im2)
print "Difference (percentage):", diff
if diff > 0.5:
print im1
else:
print im2
as a suggestion, avoid having global scope vars intermingled between functions, it makes code hard to read (referring to you setting im1 and im2 between two fn defs.
Code for doing the multiple compares:
def main(folder_path_to_search, files_to_compare_to, source_image_path):
#capture_img = "/Users/Me/home1.png"
capture_img = input('enter path of the file from database')
#img_to_compare = "/Users/Me/Documents/python programs/compare/img2.jpg"
take_and_save_picture(capture_img)
images_to_compare = [ os.path.join(folder_path_to_search,file_path) for file_path in os.listdir(folder_path_to_search) if file_path.endswith(files_to_compare_to) ]
for comp_image in get_images_to_compare():
diff = compute_edges_diff(source_image_path, comp_image)
print "Difference (percentage):", diff, "(", source_image_path, ":", comp_image, ")"
if __name__ == '__main__':
folder_path_to_search = raw_input("Enter folder path to search")
files_to_compare_to = raw_input("enter file extention to glob ex: '.jpg'")
source_image_path = raw_input("enter full file path of source image")
main(folder_path_to_search, files_to_compare_to, source_image_path)
I am trying to create an algorithm that blends the pixels of an image and I can bring the image as it was before, but I do not know do this.
I'm using python and pil, but I can use other libraries.
Exemple: to and back to
Thank you.
This should do it. There's no error handling, it doesn't follow pep8 standards, it uses slow PIL operations and it doesn't use an argument parsing library. I'm sure there are other bad things about it also.
It works by seeding python's random number generator with an invariant of the image under scrambling. The hash of the size is used. Since the size doesn't changed, a random sequence built on it will be the same for all images that share the same size. That sequence is used as a one-to-one mapping, therefore it's reversible.
The script may be invoked twice from a shell to create two images, "scrambled.png" and "unscrambled.png". "Qfhe3.png" is the source image.
python scramble.py scramble "./Qfhe3.png"
python scramble.py unscramble "./scrambled.png"
#scramble.py
from PIL import Image
import sys
import os
import random
def openImage():
return Image.open(sys.argv[2])
def operation():
return sys.argv[1]
def seed(img):
random.seed(hash(img.size))
def getPixels(img):
w, h = img.size
pxs = []
for x in range(w):
for y in range(h):
pxs.append(img.getpixel((x, y)))
return pxs
def scrambledIndex(pxs):
idx = list(range(len(pxs)))
random.shuffle(idx)
return idx
def scramblePixels(img):
seed(img)
pxs = getPixels(img)
idx = scrambledIndex(pxs)
out = []
for i in idx:
out.append(pxs[i])
return out
def unScramblePixels(img):
seed(img)
pxs = getPixels(img)
idx = scrambledIndex(pxs)
out = list(range(len(pxs)))
cur = 0
for i in idx:
out[i] = pxs[cur]
cur += 1
return out
def storePixels(name, size, pxs):
outImg = Image.new("RGB", size)
w, h = size
pxIter = iter(pxs)
for x in range(w):
for y in range(h):
outImg.putpixel((x, y), next(pxIter))
outImg.save(name)
def main():
img = openImage()
if operation() == "scramble":
pxs = scramblePixels(img)
storePixels("scrambled.png", img.size, pxs)
elif operation() == "unscramble":
pxs = unScramblePixels(img)
storePixels("unscrambled.png", img.size, pxs)
else:
sys.exit("Unsupported operation: " + operation())
if __name__ == "__main__":
main()