社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Mark Setchell  »  全部回复
回复总数  4
3 年前
回复了 Mark Setchell 创建的主题 » 找到‘。使用Python的dng图像分辨率

PIL 不读取DNG文件,也不喜欢每通道16位的RGB。我想你需要这样的东西 rawpy

import rawpy

path = '/Users/mark/Desktop/RAW_LEICA_M8.DNG'
with rawpy.imread(path) as raw:
    rgb = raw.postprocess()

print(rgb.shape)       # It's a Numpy array

三倍输出

Out[9]: (2638, 3920, 3)

另一个选择是 Exiftool ,您可以这样安装:

pip3 install PyExifTool

然后像这样使用:

import exiftool

with exiftool.ExifTool() as et:
    metadata = et.get_metadata('/path/to/RAW_LEICA_M8.DNG')

print(metadata)

{'SourceFile': '/path/to/RAW_LEICA_M8.DNG',
 'ExifTool:ExifToolVersion': 12.0,
 'File:FileName': 'RAW_LEICA_M8.DNG',
 'File:Directory': '/path/to',
 'File:FileSize': 10575296,
 'File:FileModifyDate': '2021:08:16 08:54:30+01:00',
 'File:FileAccessDate': '2021:08:16 09:56:33+01:00',
 'File:FileInodeChangeDate': '2021:08:16 09:56:31+01:00',
 'File:FilePermissions': 644,
 'File:FileType': 'DNG',
 'File:FileTypeExtension': 'DNG',
 'File:MIMEType': 'image/x-adobe-dng',
 'File:ExifByteOrder': 'II',
 'EXIF:Make': 'Leica Camera AG',
 'EXIF:Model': 'M8 Digital Camera',
 'EXIF:Orientation': 1,
 'EXIF:Software': 1.107,
 'EXIF:Artist': '',
 'EXIF:SubfileType': 0,
 'EXIF:ImageWidth': 3920,            <--- HERE IS THE WIDTH
 'EXIF:ImageHeight': 2638,           <--- HERE IS THE HEIGHT
 'EXIF:BitsPerSample': 8,
 'EXIF:Compression': 1,
 'EXIF:PhotometricInterpretation': 32803,
 'EXIF:StripOffsets': '(Binary data 1315 bytes, use -b option to extract)',
 'EXIF:SamplesPerPixel': 1,
 'EXIF:RowsPerStrip': 16,
 'EXIF:StripByteCounts': '(Binary data 989 bytes, use -b option to extract)',
 'EXIF:XResolution': 300,
 'EXIF:YResolution': 300,
 'EXIF:PlanarConfiguration': 1,
 'EXIF:ResolutionUnit': 2,
 'EXIF:CFARepeatPatternDim': '2 2',
 'EXIF:CFAPattern2': '0 1 1 2',
 'EXIF:LinearizationTable': '(Binary data 1244 bytes, use -b option to extract)',
 'EXIF:WhiteLevel': 16383,
 'EXIF:DefaultCropOrigin': '2 2',
 'EXIF:DefaultCropSize': '3916 2634',
 'EXIF:BayerGreenSplit': 500,
 'EXIF:AntiAliasStrength': 0,
 'EXIF:Copyright': '',
 'EXIF:ExposureTime': 12,
 'EXIF:ExposureProgram': 1,
 'EXIF:ISO': 160,
 'EXIF:ExifVersion': '0220',
 'EXIF:CreateDate': '2007:08:02 22:13:49',
 'EXIF:ShutterSpeedValue': 11.3137084989848,
 'EXIF:ExposureCompensation': 0,
 'EXIF:MaxApertureValue': 2,
 'EXIF:MeteringMode': 2,
 'EXIF:LightSource': 0,
 'EXIF:Flash': 0,
 'EXIF:FocalLength': 50,
 'EXIF:FileSource': 3,
 'EXIF:SceneType': 1,
 'EXIF:WhiteBalance': 0,
 'EXIF:DigitalZoomRatio': 0,
 'EXIF:FocalLengthIn35mmFormat': 67,
 'EXIF:SceneCaptureType': 0,
 'EXIF:ImageUniqueID': '00000000000000000000000000000147',
 'EXIF:SelfTimerMode': 0,
 'EXIF:DateTimeOriginal': '2007:08:02 22:13:49',
 'EXIF:FocalPlaneXResolution': 3729,
 'EXIF:FocalPlaneYResolution': 3764,
 'EXIF:FocalPlaneResolutionUnit': 2,
 'EXIF:TIFF-EPStandardID': '0 0 0 1',
 'EXIF:DNGVersion': '1 0 0 0',
 'EXIF:UniqueCameraModel': 'M8 Digital Camera',
 'EXIF:ColorMatrix1': '1.0469 -0.5314 0.128 -0.4326 1.2176 0.2419 -0.0886 0.2473 0.716',
 'EXIF:ColorMatrix2': '0.7675 -0.2195 -0.0305 -0.586 1.4118 0.1857 -0.2425 0.4007 0.6578',
 'EXIF:CameraCalibration1': '1 0 0 0 1 0 0 0 1',
 'EXIF:CameraCalibration2': '1 0 0 0 1 0 0 0 1',
 'EXIF:AsShotNeutral': '0.4750637903 1 0.7966159382',
 'EXIF:BaselineNoise': 1,
 'EXIF:BaselineSharpness': 1,
 'EXIF:CameraSerialNumber': 3106091,
 'EXIF:CalibrationIlluminant1': 17,
 'EXIF:CalibrationIlluminant2': 21,
 'MakerNotes:UserProfile': 1,
 'MakerNotes:SerialNumber': 3106091,
 'MakerNotes:WhiteBalance': 0,
 'MakerNotes:LensType': '33 3',
 'MakerNotes:ExternalSensorBrightnessValue': -5.16796875,
 'MakerNotes:MeasuredLV': -4.359375,
 'MakerNotes:ApproximateFNumber': 4,
 'MakerNotes:CameraTemperature': 19,
 'MakerNotes:ColorTemperature': 5515,
 'MakerNotes:UV-IRFilterCorrection': 0,
 'MakerNotes:CCDVersion': 0,
 'MakerNotes:CCDBoardVersion': 1,
 'MakerNotes:ControllerBoardVersion': 0,
 'MakerNotes:M16CVersion': 0,
 'MakerNotes:ImageIDNumber': 327,
 'Composite:CFAPattern': '2 2 0 1 1 2',
 'Composite:ImageSize': '3920 2638',
 'Composite:LensID': '33 3',
 'Composite:Megapixels': 10.34096,
 'Composite:ScaleFactor35efl': 1.34,
 'Composite:ShutterSpeed': 12,
 'Composite:CircleOfConfusion': 0.0224225825588557,
 'Composite:FOV': 30.0756572109854,
 'Composite:FocalLength35efl': 67}

从下载的原始图像示例 Raw Samples .

5 年前
回复了 Mark Setchell 创建的主题 » 如何在python中读取.img文件?

如果您的图像是1024x 1024像素,那么如果数据是8位的,那么这将生成1048576字节。但是你的文件是2097268字节,这仅仅是预期大小的两倍多一点,所以我猜你的数据是16位的,即每像素2字节。这意味着文件中有2097268-(2*1024*1024),即116字节的其他垃圾。人们通常在文件的开头存储额外的内容。所以,我只取了文件的最后2097152字节,并假设它是一个16位的灰度图像,大小为1024x1024。

您可以使用ImageMagick在终端的命令行中执行此操作,如下所示:

magick -depth 16 -size 1024x1024+116 gray:frame_064_0000.img -auto-level result.png

enter image description here

在Python中,可以打开文件,从文件末尾向后查找2097152字节,然后将其读入uint16的1024x102np.array。

看起来像这样:

import numpy as np
from PIL import Image

filename = 'frame_064_0000.img' 

# set width and height 
w, h = 1024, 1024 

with open(filename, 'rb') as f: 
    # Seek backwards from end of file by 2 bytes per pixel 
    f.seek(-w*h*2, 2) 
    img = np.fromfile(f, dtype=np.uint16).reshape((h,w)) 

# Save as PNG, and retain 16-bit resolution
Image.fromarray(img).save('result.png')

# Alternative to line above - save as JPEG, but lose 16-bit resolution
Image.fromarray((img>>8).astype(np.uint8)).save('result.jpg') 
6 年前
回复了 Mark Setchell 创建的主题 » python/pip坏解释器:没有这样的文件或目录

两者之间的联系 ipython 它下面使用的python解释器和库是在安装时建立的(而不是每次运行它时动态地建立的),因此我认为如果您删除并重新安装,您的问题将得到解决 伊皮松 ,即:

brew rm ipython
brew install ipython

如果你想了解这个,看看你的 伊皮松 这样的脚本:

more $(which ipython)

直接回答你的问题时, y 轴在第一个 numpy 数组,后跟 x 轴,所以交换索引。


不那么直接,你会发现 for 在Python中循环非常慢,通常最好使用 麻木的 而是矢量化操作。而且,你会发现在 HSV colourspace .

让我们从HSL色轮开始:

enter image description here

假设你想把所有的绿色变成黑色。所以,从维基百科的页面上,绿色对应的色调是120度,这意味着你可以这样做:

#!/usr/local/bin/python3
import numpy as np
from PIL import Image

# Open image and make RGB and HSV versions
RGBim = Image.open("image.png").convert('RGB')
HSVim = RGBim.convert('HSV')

# Make numpy versions
RGBna = np.array(RGBim)
HSVna = np.array(HSVim)

# Extract Hue
H = HSVna[:,:,0]

# Find all green pixels, i.e. where 100 < Hue < 140
lo,hi = 100,140
# Rescale to 0-255, rather than 0-360 because we are using uint8
lo = int((lo * 255) / 360)
hi = int((hi * 255) / 360)
green = np.where((H>lo) & (H<hi))

# Make all green pixels black in original image
RGBna[green] = [0,0,0]

count = green[0].size
print("Pixels matched: {}".format(count))
Image.fromarray(RGBna).save('result.png')

它给出:

enter image description here


这里有一个稍微改进的版本,它保留了alpha/透明度,并匹配红色像素以获得额外的乐趣:

#!/usr/local/bin/python3
import numpy as np
from PIL import Image

# Open image and make RGB and HSV versions
im = Image.open("image.png")

# Save Alpha if present, then remove
if 'A' in im.getbands():
    savedAlpha = im.getchannel('A')
    im = im.convert('RGB')

# Make HSV version
HSVim = im.convert('HSV')

# Make numpy versions
RGBna = np.array(im)
HSVna = np.array(HSVim)

# Extract Hue
H = HSVna[:,:,0]

# Find all red pixels, i.e. where 340 < Hue < 20
lo,hi =  340,20
# Rescale to 0-255, rather than 0-360 because we are using uint8
lo = int((lo * 255) / 360)
hi = int((hi * 255) / 360)
red = np.where((H>lo) | (H<hi))

# Make all red pixels black in original image
RGBna[red] = [0,0,0]

count = red[0].size
print("Pixels matched: {}".format(count))

result=Image.fromarray(RGBna)

# Replace Alpha if originally present
if savedAlpha is not None:
    result.putalpha(savedAlpha)

result.save('result.png')

enter image description here