Convert an SVG to grayscale in python - python

Scenario
I'm trying to convert a colored example.svg file into a grayscaled example_gs.svg in python 3.6 in Anaconda 4.8.3 on a Windows 10 device.
Attempts
First I tried to apply a regex to convert the 'rgb(xxx,yyy,zzz)' to black, but that created a black rectangle losing the image in the process. Next I installed inkscape and ran a grayscale command which appeared to be working but did not modify the example.svg. The third attempt with pillow Image did not load the .svg.
MWE
# conda install -c conda-forge inkscape
# https://www.commandlinefu.com/commands/view/2009/convert-a-svg-file-to-grayscale
# inkscape -f file.svg --verb=org.inkscape.color.grayscale --verb=FileSave --verb=FileClose
import re
import os
import fileinput
from PIL import Image
import cv2
# Doesn't work, creates a black square. Perhaps set a threshold to not convert rgb white/bright colors
def convert_svg_to_grayscale(filepath):
# Read in the file
with open(filepath, 'r') as file :
filedata = file.read()
# Replace the target string
filedata = re.sub(r'rgb\(.*\)', 'black', filedata)
# Write the file out again
with open(filepath, 'w') as file:
file.write(filedata)
# opens inkscape, converts to grayscale but does not actually export to the output file again
def convert_svg_to_grayscale_inkscape(filepath):
command = f'inkscape -f {filepath} --verb=org.inkscape.color.grayscale --verb=FileSave --verb=FileClose'
os.system(f'cmd /k {command}')
# Pillow Image is not able to import .svg files
def grayscale(filepath):
image = Image.open(filepath)
cv2.imwrite(f'{filepath}', image.convert('L'))
# walks through png files and calls function to convert the png file to .svg
def main():
filepath = 'example.svg'
convert_svg_to_grayscale_inkscape(filepath)
convert_svg_to_grayscale(filepath)
grayscale(filepath)
if __name__ == '__main__':
main()
Question
How could I change a colored .svg file into a grayscaled image in python 3.6 on a windows device?

You have chosen the right tool for converting to grayscale. Your last attempt is good but you need to import cairosvg that provides the svg2png function. Then, load the png file with Pillow and convert it to an np.array and then you can easily load it with openCV and convert it to grayscale as you did. At last you can use svglib and reportlab to export the images in svg.
Use this snippet as an example:
https://stackoverflow.com/a/62345450/13605264

Related

Extract text from multiple images to CSV using OCR

I want to extract texts from thousand of images and put it into a CSV file. Can anyone tell me how to do that? I have images saved on my desktop.
sure.
install pytesseract module using this command:
pip install pytesseract
install tesseract engine executable from this urls:
tesseract cmd 32 bit
or
tesseract cmd 64 bit
create a python script called images_to_csv.py and paste this code:
import pytesseract
from PIL import Image # pip install Pillow
# set tesseract cmd to the be the path to your tesseract engine executable
# (where you installed tesseract from above urls)
# IMPORTANT: this should end with '...\tesseract.exe'
pytesseract.pytesseract.tesseract_cmd = <path_to_your_tesseract_cmd>
# and start doing it
# your saved images on desktop
list_with_many_images = [
"path1",
"path2"
# ...
"pathN"
]
# create a function that returns the text
def image_to_str(path):
""" return a string from image """
return pytesseract.image_to_string(Image.open(path))
# now pure action + csv part
with open("images_content.csv", "w+", encoding="utf-8") as file:
file.write("ImagePath, ImageText")
for image_path in list_with_many_images:
text = image_to_str(image_path)
line = f"{image_path}, {text}\n"
file.write(line)
this is all for beginning.
if you want to use module csv go ahead.
enjoy.

conveting bytes to image using tinytag and PIL

I am using tinytags module in python to get the cover art of a mp3 file and want to display or store it. The return type of the variable is showing to be bytes. I have tried fumbling around with PIL using frombytes but to no avail. Is there any method to convert the bytes to image?
from tinytag import TinyTag
tag = TinyTag.get("03. Me, Myself & I.mp3", image=True)
img = tag.get_image()
I actually got a PNG image when I called tag.get_image() but I guess you might get a JPEG. Either way, you can wrap it in a BytesIO and open it with PIL/Pillow or display it. Carrying on from your code:
from PIL import Image
import io
...
im = tag.get_image()
# Make a PIL Image
pi = Image.open(io.BytesIO(im))
# Save as PNG, or JPEG
pi.save('cover.png')
# Display
pi.show()
Note that you don't have to use PIL/Pillow. You could look at the first few bytes and if they are a PNG signature (\x89PNG) save data as binary with PNG extension. If the signature is JPEG (\xff \xd8) save data as binary with JPEG extension.

Python SVG converter creates empty file

I have some code below that is supposed to convert a SVG image to a PNG. It runs without errors but creates a PNG file that is blank instead of one with the same image as the original SVG. I did find that it is not an error with cairo but more one relating to rsvg, which I got here.
import cairo
import rsvg
img = cairo.ImageSurface(cairo.FORMAT_ARGB32, 640,480)
ctx = cairo.Context(img)
handle= rsvghandler.Handle('example.svg')
handle.render_cairo(ctx)
img.write_to_png("svg.png")
I am using Python 3.6 on Windows 10.
I can't for the life of me figure out why it isn't displaying the correct picture. Any help would be hugely appreciated.
If your goal is to convert from SVG to PNG, I would recommend using Wand, as in the following script:
from wand.api import library
import wand.color
import wand.image
with wand.image.Image() as image:
with wand.color.Color('transparent') as background_color:
library.MagickSetBackgroundColor(image.wand,
background_color.resource)
image.read(blob=NAMEOFTHEFILE.read(), format="svg")
png_image = image.make_blob("png32")
with open(NAMEOFTHENEWFILE, "wb") as out:
out.write(png_image)

Unable to get dicom image for display in python

I'm trying to display a DICOM image in opencv-python.I am using the pydicom library,And then adding API's to create a full fledged DICOM viewer with DOTNET, that runs python(C# calls python with process instance of course!!). I am unable to convert or see the uncompressed DICOM image. whenever i try to load or modify the pixel_array. I get error messges.
import dicom
import cv2
import numpy
df=dicom.read_file("IM-0001-0002.dcm")
df.pixel_array
Traceback (most recent call last):
File "<pyshell#4>", line 1, in <module>
df.pixel_array
File "C:\Python27\lib\site-packages\dicom\dataset.py", line 394, in pixel_array
return self._get_pixel_array()
File "C:\Python27\lib\site-packages\dicom\dataset.py", line 376, in _get_pixel_array
raise NotImplementedError("Pixel Data is compressed in a format pydicom does not yet handle. Cannot return array")
NotImplementedError: Pixel Data is compressed in a format pydicom does not yet handle. Cannot return array
Please suggest me good way to convert the image making cv2.imshow() function o display the image
Thanks in Advance!!
Try pydicom
One reason for the error can be: the .dcm file used may contain the format that is not supported (e.g. JPEG 2000 is not supported by pillow in case of pydicom). This issue can be solved. I was having the same issue (I am using pydicom instead of dicom) I guess you will get some direction from the solution that solved my problem:
1st Platforma Information:
I am using: pydicom to read .dcm files, Python 3.6, Anaconda and Ubuntu, 15 GB RAM
Solution:
Install pydicom using this command: pip install -U pydicom.
Information can be found here: (link: https://pydicom.github.io/pydicom/dev/getting_started.html)
Anaconda is necessary. Why?
Please check the official doc of pydicom (https://pydicom.github.io/pydicom/dev/getting_started.html) its mentioned "To install pydicom along with image handlers for compressed pixel data, we encourage you to use Miniconda or Anaconda"
If you are using Ubuntu directly open Terminal. If you are using Windows then on Anaconda Navigator go to Environment from here start terminal. Execute the following commands on it:
pip install -U git+https://github.com/pydicom/pydicom.git
conda install pydicom --channel conda-forge
conda install -c conda-forge gdcm
Cross Check:
Now restart the notebook and then try to execute your code using pydicom. It will display the output.
Also, you can use Matplotlib to display as follows:
import matplotlib.pyplot as plt
import pydicom
filename = 'IM-0001-0002.dcm'
ds = pydicom.dcmread(filename)
plt.imshow(ds.pixel_array, cmap=plt.cm.bone)
I hope it will help you.
Since pydicom do not support compressed dicom files, you will have to decompress it first.
You can use GDCM to do that.
You can use the python GDCM binding to decompress the file first, see for example here
you have to convert in RGB before, look at that for a monochrome dicom file:
https://github.com/twanmal/dicom_monochrome_to_opencv
# import the necessary packages
from imutils import contours
import scipy
from skimage import measure
import numpy as np # numeric library needed
import pandas as pd #for datafrome
import argparse # simple argparser
import imutils
import cv2 # for opencv image recognising tool
import dicom
filename = askopenfilename()
dicom_file = dicom.read_file(filename) ## original dicom File
#### a dicom monochrome-2 file has pixel value between approx -2000 and +2000, opencv doesn't work with it#####
#### in a first step we transform those pixel values in (R,G,B)
### to have gray in RGB, simply give the same values for R,G, and B,
####(0,0,0) will be black, (255,255,255) will be white,
## the threeshold to be automized with a proper quartile function of the pixel distribution
black_threeshold=0###pixel value below 0 will be black,
white_threeshold=1400###pixel value above 1400 will be white
wt=white_threeshold
bt=black_threeshold
###### function to transform a dicom to RGB for the use of opencv,
##to be strongly improved, as it takes to much time to run,
## and the linear process should be replaced with an adapted weighted arctan or an adapted spline interpolation.
def DicomtoRGB(dicomfile,bt,wt):
"""Create new image(numpy array) filled with certain color in RGB"""
# Create black blank image
image = np.zeros((dicomfile.Rows, dicomfile.Columns, 3), np.uint8)
#loops on image height and width
i=0
j=0
while i<dicomfile.Rows:
j=0
while j<dicomfile.Columns:
color = yaxpb(dicom_file.pixel_array[i][j],bt,wt) #linear transformation to be adapted
image[i][j] = (color,color,color)## same R,G, B value to obtain greyscale
j=j+1
i=i+1
return image
##linear transformation : from [bt < pxvalue < wt] linear to [0<pyvalue<255]: loss of information...
def yaxpb(pxvalue,bt,wt):
if pxvalue < bt:
y=0
elif pxvalue > wt:
y=255
else:
y=pxvalue*255/(wt-bt)-255*bt/(wt-bt)
return y
image=DicomtoRGB(dicom_file,bt=0,wt=1400)
## loading the RGB in a proper opencv format
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
## look at the gray file
cv2.imshow("gray", gray)
cv2.waitKey(0)
cv2.destroyWindow("gray")

How to read raw png from an array in python opencv?

I'm streaming a png image from my iPhone to my MacBook over tcp. The MacBook code is from http://docs.python.org/library/socketserver.html#requesthandler-objects. How can the image be converted for use with OpenCV? A png was selected because they are efficient, but other formats could be used.
I wrote a test program that reads the rawImage from a file, but not sure how to convert it:
# Read rawImage from a file, but in reality will have it from TCPServer
f = open('frame.png', "rb")
rawImage = f.read()
f.close()
# Not sure how to convert rawImage
npImage = np.array(rawImage)
matImage = cv2.imdecode(rawImage, 1)
#show it
cv.NamedWindow('display')
cv.MoveWindow('display', 10, 10)
cv.ShowImage('display', matImage)
cv. WaitKey(0)
#Andy Rosenblum's works, and it might be the best solution if using the outdated cv python API (vs. cv2).
However, because this question is equally interesting for users of the latest versions, I suggest the following solution. The sample code below may be better than the accepted solution because:
It is compatible with newer OpenCV python API (cv2 vs. cv). This solution is tested under opencv 3.0 and python 3.0. I believe only trivial modifications would be required for opencv 2.x and/or python 2.7x.
Fewer imports. This can all be done with numpy and opencv directly, no need for StringIO and PIL.
Here is how I create an opencv image decoded directly from a file object, or from a byte buffer read from a file object.
import cv2
import numpy as np
#read the data from the file
with open(somefile, 'rb') as infile:
buf = infile.read()
#use numpy to construct an array from the bytes
x = np.fromstring(buf, dtype='uint8')
#decode the array into an image
img = cv2.imdecode(x, cv2.IMREAD_UNCHANGED)
#show it
cv2.imshow("some window", img)
cv2.waitKey(0)
Note that in opencv 3.0, the naming convention for the various constants/flags changed, so if using opencv 2.x, you will need to change the flag cv2.IMREAD_UNCHANGED. This code sample also assumes you are loading in a standard 8-bit image, but if not, you can play with the dtype='...' flag in np.fromstring.
another way,
also in the case of a reading an actual file this will work for a unicode path (tested on windows)
with open(image_full_path, 'rb') as img_stream:
file_bytes = numpy.asarray(bytearray(img_stream.read()), dtype=numpy.uint8)
img_data_ndarray = cv2.imdecode(file_bytes, cv2.CV_LOAD_IMAGE_UNCHANGED)
img_data_cvmat = cv.fromarray(img_data_ndarray) # convert to old cvmat if needed
I figured it out:
# Read rawImage from a file, but in reality will have it from TCPServer
f = open('frame.png', "rb")
rawImage = f.read()
f.close()
# Convert rawImage to Mat
pilImage = Image.open(StringIO(rawImage));
npImage = np.array(pilImage)
matImage = cv.fromarray(npImage)
#show it
cv.NamedWindow('display')
cv.MoveWindow('display', 10, 10)
cv.ShowImage('display', matImage)
cv. WaitKey(0)
This works for me (these days):
import cv2
import numpy as np
data = open('016e263c726a.raw').read()
x = np.frombuffer(data, dtype='uint8').reshape(2048,2448)
cv2.imshow('x',x); cv2.waitKey(); cv2.destroyAllWindows()
But it reads a RAW image saved without any specific format.
(Your question seems to be tagged objective-c but you ask for Python and so is your example, so I'll use that.)
My first post on Stack Overflow!
The cv.LoadImageM method seems to be what you are looking for.
http://opencv.willowgarage.com/documentation/python/reading_and_writing_images_and_video.html
Example use:
http://opencv.willowgarage.com/wiki/PythonInterface/
LoadImage(filename, iscolor=CV_LOAD_IMAGE_COLOR) → None
Loads an image from a file as an IplImage.
Parameters:
filename (str) – Name of file to be loaded.
iscolor (int) –
Specific color type of the loaded image:
CV_LOAD_IMAGE_COLOR the loaded image is forced to be a 3-channel color image
CV_LOAD_IMAGE_GRAYSCALE the loaded image is forced to be grayscale
CV_LOAD_IMAGE_UNCHANGED the loaded image will be loaded as is.
The function cvLoadImage loads an image from the specified file and
returns the pointer to the loaded image. Currently the following file
formats are supported:
Windows bitmaps - BMP, DIB
JPEG files - JPEG, JPG, JPE
Portable Network Graphics - PNG
Portable image format - PBM, PGM, PPM
Sun rasters - SR, RAS
TIFF files - TIFF, TIF
Note that in the current implementation the alpha channel, if any, is
stripped from the output image, e.g. 4-channel RGBA image will be
loaded as RGB.
When you have to load from file, this simple solution does the job (tested with opencv-python-3.2.0.6):
import cv2
img = cv2.imread(somefile)

Categories

Resources